网站设计费用志设计素材网站p开头的
- 作者: 五速梦信息网
- 时间: 2026年03月21日 07:29
当前位置: 首页 > news >正文
网站设计费用志,设计素材网站p开头的,做网站上饶,景点介绍网站开发设计策略模式#xff08;Strategy Pattern#xff09; 策略模式 (Strategy Pattern): 定义一系列算法#xff0c;将每个算法都封装起来#xff0c;并使它们之间可以互换。例如#xff1a;java.util.Comparator 以下是一个简单的Java策略模式的例子#xff0c;涉及一个商品的… 策略模式Strategy Pattern 策略模式 (Strategy Pattern): 定义一系列算法将每个算法都封装起来并使它们之间可以互换。例如java.util.Comparator 以下是一个简单的Java策略模式的例子涉及一个商品的价格计算场景。我们将定义一个PriceCalculator类它使用不同的价格计算策略来计算商品的最终价格。 import java.util.HashMap; import java.util.Map;// 策略接口 interface PricingStrategy {double calculatePrice(double originalPrice); }// 具体策略1打折策略 class DiscountPricingStrategy implements PricingStrategy {private double discountRate;public DiscountPricingStrategy(double discountRate) {this.discountRate discountRate;}Overridepublic double calculatePrice(double originalPrice) {return originalPrice * (1 - discountRate);} }// 具体策略2满减策略 class FullReductionPricingStrategy implements PricingStrategy {private double threshold;private double reductionAmount;public FullReductionPricingStrategy(double threshold, double reductionAmount) {this.threshold threshold;this.reductionAmount reductionAmount;}Overridepublic double calculatePrice(double originalPrice) {return originalPrice threshold ? originalPrice - reductionAmount : originalPrice;} }// 上下文类用于设置和执行策略 class PriceCalculator {private PricingStrategy pricingStrategy;public void setPricingStrategy(PricingStrategy pricingStrategy) {this.pricingStrategy pricingStrategy;}public double calculateFinalPrice(double originalPrice) {if (pricingStrategy null) {throw new IllegalStateException(Pricing strategy not set);}return pricingStrategy.calculatePrice(originalPrice);} }public class StrategyPatternExample {public static void main(String[] args) {// 创建商品价格计算器PriceCalculator priceCalculator new PriceCalculator();// 商品原始价格double originalPrice 100.0;// 使用打折策略PricingStrategy discountStrategy new DiscountPricingStrategy(0.2);priceCalculator.setPricingStrategy(discountStrategy);double discountedPrice priceCalculator.calculateFinalPrice(originalPrice);System.out.println(Discounted Price: discountedPrice);// 使用满减策略PricingStrategy fullReductionStrategy new FullReductionPricingStrategy(80.0, 10.0);priceCalculator.setPricingStrategy(fullReductionStrategy);double reducedPrice priceCalculator.calculateFinalPrice(originalPrice);System.out.println(Reduced Price: reducedPrice);} }这个例子中PricingStrategy是一个策略接口具体的打折策略和满减策略分别实现了这个接口。PriceCalculator是上下文类用于设置和执行不同的策略。在main方法中我们创建了一个商品价格计算器分别使用了打折策略和满减策略来计算最终价格。你可以运行这个例子看到不同策略下的最终价格计算结果。 观察者模式Observer Pattern 定义了一种一对多的依赖关系使得当一个对象改变状态时所有依赖于它的对象都会得到通知并自动更新。例如java.util.Observer 和 java.util.Observable 当涉及观察者模式时通常会有一个主题对象Subject它会维护一个观察者Observer列表并通知这些观察者当其状态发生改变。下面是一个简单的 Java 观察者模式示例模拟了一个简单的新闻发布系统。 import java.util.ArrayList; import java.util.List;// 主题接口 interface Subject {void registerObserver(Observer observer);void removeObserver(Observer observer);void notifyObservers(String news); }// 具体主题类 class NewsPublisher implements Subject {private ListObserver observers;private String latestNews;public NewsPublisher() {this.observers new ArrayList();}Overridepublic void registerObserver(Observer observer) {observers.add(observer);}Overridepublic void removeObserver(Observer observer) {observers.remove(observer);}Overridepublic void notifyObservers(String news) {for (Observer observer : observers) {observer.update(news);}}// 模拟新闻发布public void publishNews(String news) {this.latestNews news;notifyObservers(news);} }// 观察者接口 interface Observer {void update(String news); }// 具体观察者类 class NewsSubscriber implements Observer {private String subscriberName;public NewsSubscriber(String subscriberName) {this.subscriberName subscriberName;}Overridepublic void update(String news) {System.out.println(subscriberName received news: news);} }public class ObserverPatternExample {public static void main(String[] args) {// 创建新闻发布者NewsPublisher publisher new NewsPublisher();// 创建订阅者Observer subscriber1 new NewsSubscriber(Subscriber 1);Observer subscriber2 new NewsSubscriber(Subscriber 2);// 订阅新闻publisher.registerObserver(subscriber1);publisher.registerObserver(subscriber2);// 发布新闻publisher.publishNews(Breaking News: Observers pattern example!);// 移除一个订阅者publisher.removeObserver(subscriber2);// 再次发布新闻publisher.publishNews(Second Edition: More updates!);} }这个示例中NewsPublisher 是主题类负责管理观察者列表并通知它们有关最新新闻的更新。NewsSubscriber 是具体的观察者类实现了 Observer 接口。在 main 方法中创建了一个新闻发布者并添加了两个订阅者。当发布者发布新闻时所有订阅者都会收到相应的新闻更新。可以运行这个示例来查看观察者模式的效果。 模板方法模式Template Method Pattern 定义一个算法的骨架将一些步骤延迟到子类中。例如java.util.Collections#sort() 模板方法模式通常涉及一个抽象类定义了一个算法的骨架将一些步骤延迟到子类中。以下是一个简单的 Java 模板方法模式的例子模拟了一种饮料制作的过程。 // 模板方法抽象类 abstract class BeverageTemplate {// 模板方法定义了饮料制作的步骤public final void makeBeverage() {boilWater();brew();pourInCup();addCondiments();System.out.println(Beverage is ready!);}// 具体步骤1烧水private void boilWater() {System.out.println(Boiling water);}// 具体步骤2冲泡protected abstract void brew();// 具体步骤3倒入杯中private void pourInCup() {System.out.println(Pouring into cup);}// 具体步骤4加调料protected abstract void addCondiments(); }// 具体类1制作咖啡 class Coffee extends BeverageTemplate {Overrideprotected void brew() {System.out.println(Brewing coffee grounds);}Overrideprotected void addCondiments() {System.out.println(Adding sugar and milk);} }// 具体类2制作茶 class Tea extends BeverageTemplate {Overrideprotected void brew() {System.out.println(Steeping the tea);}Overrideprotected void addCondiments() {System.out.println(Adding lemon);} }public class TemplateMethodPatternExample {public static void main(String[] args) {// 制作咖啡BeverageTemplate coffee new Coffee();System.out.println(Making coffee…);coffee.makeBeverage();System.out.println();// 制作茶BeverageTemplate tea new Tea();System.out.println(Making tea…);tea.makeBeverage();} }在这个例子中BeverageTemplate 是模板方法抽象类它定义了一个 makeBeverage 模板方法其中包含了制作饮料的通用步骤如烧水、冲泡、倒入杯中和加调料。Coffee 和 Tea 是具体的子类分别实现了 brew 和 addCondiments 方法以定制制作咖啡和茶的具体步骤。 在 main 方法中创建了一个咖啡对象和一个茶对象并分别调用它们的 makeBeverage 方法。你可以运行这个示例来查看模板方法模式的实际应用。
- 上一篇: 网站设计费网站设计与建设
- 下一篇: 网站设计服务费英文上海红蚂蚁装潢设计有限公司官网
相关文章
-
网站设计费网站设计与建设
网站设计费网站设计与建设
- 技术栈
- 2026年03月21日
-
网站设计方案书ppt买虚机送网站建设
网站设计方案书ppt买虚机送网站建设
- 技术栈
- 2026年03月21日
-
网站设计的主要机构有哪些?网页设计主要做什么
网站设计的主要机构有哪些?网页设计主要做什么
- 技术栈
- 2026年03月21日
-
网站设计服务费英文上海红蚂蚁装潢设计有限公司官网
网站设计服务费英文上海红蚂蚁装潢设计有限公司官网
- 技术栈
- 2026年03月21日
-
网站设计服务费做什么费用网站建设的文案
网站设计服务费做什么费用网站建设的文案
- 技术栈
- 2026年03月21日
-
网站设计工程师是it行业吗打开网站后直接做跳转页面
网站设计工程师是it行业吗打开网站后直接做跳转页面
- 技术栈
- 2026年03月21日
