当前位置: 首页 > wzjs >正文

网站 页面风格 建设天津seo招聘

网站 页面风格 建设,天津seo招聘,河南共青团智慧团建登录入口,做游戏网站需求确认结构型模式 - 装饰者模式 (Decorator Pattern) 在展开讲装饰者模式之前,不得不提一下代理模式,因为这两者在一定的层度上是有相似性的, 通过对比可以让我们更好的理解装饰者. 定义与核心目的 装饰者模式 定义:动态地将责任附加到对象上。若要扩展功能&#xff0c…

结构型模式 - 装饰者模式 (Decorator Pattern)

在展开讲装饰者模式之前,不得不提一下代理模式,因为这两者在一定的层度上是有相似性的, 通过对比可以让我们更好的理解装饰者.

定义与核心目的

  • 装饰者模式
    • 定义:动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
    • 核心目的:主要是为对象添加额外的功能,强调的是对对象功能的增强和扩展,是在不改变原有对象结构的基础上,给对象增加新的行为。
  • 代理模式
    • 定义:为其他对象提供一种代理以控制对这个对象的访问。
    • 核心目的:主要是控制对对象的访问,代理对象充当了客户端和目标对象之间的中介,客户端通过代理对象来间接访问目标对象,从而可以在访问前后进行一些额外的操作,如权限验证、缓存等。

接下来通过代码对比一下装饰者模式、代理模式

// 装饰者模式// 定义一个接口
interface Component {void operation();
}// 具体组件
class ConcreteComponent implements Component {@Overridepublic void operation() {System.out.println("执行基本操作");}
}// 装饰者抽象类
abstract class Decorator implements Component {protected Component component;public Decorator(Component component) {this.component = component;}@Overridepublic void operation() {component.operation();}
}// 具体装饰者
class ConcreteDecorator extends Decorator {public ConcreteDecorator(Component component) {super(component);}@Overridepublic void operation() {super.operation();System.out.println("添加额外操作");}
}// 测试代码
public class DecoratorPatternExample {public static void main(String[] args) {Component component = new ConcreteComponent();Component decoratedComponent = new ConcreteDecorator(component);decoratedComponent.operation();}
}

如果把上面装饰者的例子中 Decorator 类去掉抽象 abstract 修饰, 然后把持有的 Component 改成具体对象 ConcreteComponent 并在构造方法里面直接创建要代理的对象, 那它就变成静态代理模式!!!

下面这是一个代理模式例子, 来看看它俩的相似之处吧.

// 定义一个接口
interface Subject {void request();
}// 真实主题
class RealSubject implements Subject {@Overridepublic void request() {System.out.println("执行真实请求");}
}// 代理主题
class ProxySubject implements Subject {private RealSubject realSubject;public ProxySubject() {this.realSubject = new RealSubject();}@Overridepublic void request() {System.out.println("在请求前进行一些操作");realSubject.request();System.out.println("在请求后进行一些操作");}
}// 测试代码
public class ProxyPatternExample {public static void main(String[] args) {ProxySubject proxy = new ProxySubject();proxy.request();}
}

再看一个经典的装饰者模式, 来加强一下装饰者的理解

// 抽象组件:饮品
interface Beverage {String getDescription();double cost();
}// 具体组件:浓缩咖啡
class Espresso implements Beverage {@Overridepublic String getDescription() {return "浓缩咖啡";}@Overridepublic double cost() {return 2.0;}
}// 抽象装饰者:调料
abstract class CondimentDecorator implements Beverage {protected Beverage beverage;public CondimentDecorator(Beverage beverage) {this.beverage = beverage;}
}// 具体装饰者:牛奶
class Milk extends CondimentDecorator {public Milk(Beverage beverage) {super(beverage);}@Overridepublic String getDescription() {return beverage.getDescription() + ",加牛奶";}@Overridepublic double cost() {return beverage.cost() + 0.5;}
}// 具体装饰者:巧克力
class Chocolate extends CondimentDecorator {public Chocolate(Beverage beverage) {super(beverage);}@Overridepublic String getDescription() {return beverage.getDescription() + ",加巧克力";}@Overridepublic double cost() {return beverage.cost() + 1.0;}
}// 测试类
public class CoffeeShopExample {public static void main(String[] args) {// 创建一个浓缩咖啡Beverage espresso = new Espresso();System.out.println(espresso.getDescription() + ",价格:" + espresso.cost() + " 元");// 给浓缩咖啡加牛奶Beverage espressoWithMilk = new Milk(espresso);System.out.println(espressoWithMilk.getDescription() + ",价格:" + espressoWithMilk.cost() + " 元");// 给加了牛奶的浓缩咖啡再加巧克力Beverage espressoWithMilkAndChocolate = new Chocolate(espressoWithMilk);System.out.println(espressoWithMilkAndChocolate.getDescription() + ",价格:" + espressoWithMilkAndChocolate.cost() + " 元");}
}

这时候我们可以再创建一个装饰者, 比如配送方式的装饰者,看顾客是到店自取还是外卖配送, 因为这两种方式价格是不一样的.
这时候我们只需要另外建一个具体装饰者类即可.

// 抽象配送装饰者
abstract class DeliveryDecorator implements Beverage {protected Beverage beverage;public DeliveryDecorator(Beverage beverage) {this.beverage = beverage;}
}// 具体配送装饰者:外卖配送
class DeliveryByCourier extends DeliveryDecorator {public DeliveryByCourier(Beverage beverage) {super(beverage);}@Overridepublic String getDescription() {return beverage.getDescription() + ",外卖配送";}@Overridepublic double cost() {return beverage.cost() + 3.0; // 假设外卖配送费 3 元}
}// 具体配送装饰者:到店自取
class PickupInStore extends DeliveryDecorator {public PickupInStore(Beverage beverage) {super(beverage);}@Overridepublic String getDescription() {return beverage.getDescription() + ",到店自取";}@Overridepublic double cost() {return beverage.cost(); // 到店自取无额外费用}
}// 测试类
public class CoffeeShopExample {public static void main(String[] args) {// // 创建一个浓缩咖啡// Beverage espresso = new Espresso();// System.out.println(espresso.getDescription() + ",价格:" + espresso.cost() + " 元");// // 给浓缩咖啡加牛奶// Beverage espressoWithMilk = new Milk(espresso);// System.out.println(espressoWithMilk.getDescription() + ",价格:" + espressoWithMilk.cost() + " 元");// // 给加了牛奶的浓缩咖啡再加巧克力// Beverage espressoWithMilkAndChocolate = new Chocolate(espressoWithMilk);// System.out.println(espressoWithMilkAndChocolate.getDescription() + ",价格:" + espressoWithMilkAndChocolate.cost() + " 元");// 使用的时候就可以 2 选 1 种配送// 选择外卖配送Beverage espressoWithAllAndDelivery = new DeliveryByCourier(espressoWithMilkAndChocolate);System.out.println(espressoWithAllAndDelivery.getDescription() + ",价格:" + espressoWithAllAndDelivery.cost() + " 元");// 选择到店自取Beverage espressoWithAllAndPickup = new PickupInStore(espressoWithMilkAndChocolate);System.out.println(espressoWithAllAndPickup.getDescription() + ",价格:" + espressoWithAllAndPickup.cost() + " 元");}
}

这时候假设我们需求又双叒变更了, 我们只需要增加装饰者, 比如说要按顾客的会员等级对订单总额打折处理… 发挥你的想象吧, 它有无限的可能~

http://www.dtcms.com/wzjs/511748.html

相关文章:

  • 公司请外包做的网站怎么维护crm软件
  • 签到做任务赚钱的网站怎样做自己的网站
  • 用dede.com网站上传文件购买seo关键词排名优化官网
  • 邯郸做网站公司百度精简版网页入口
  • 怎么给网站做第三方app网络推广企业
  • wordpress 资讯类模板东莞seo报价
  • 德州口碑好的网站制作公司清理大师
  • 做旅游的网站品牌营销包括哪些方面
  • 北京做商铺的网站大型网站制作
  • 洛阳做网站公司电话品牌词优化
  • 柳州网站建设工作室seo 页面
  • 网站竞争对手如何做调研小广告怎么能弄干净
  • Wordpress简约卡片长沙的seo网络公司
  • 怎样做 云知梦 网站 付费网站广州网络推广seo
  • 大连网站制作公司58网络平台推广具体是怎么推广
  • wordpress主题404班级优化大师免费下载安装
  • 专业的东莞网站排名十大it教育培训机构排名
  • 网站漂浮广告效果整站seo外包
  • 定制型网站建设价格小程序拉新推广平台
  • 直播网站怎样建设正规seo关键词排名网络公司
  • 金融网站排名优化百度安全中心
  • 小学最好的网站建设上海seo招聘
  • 日本真人做爰无遮挡视频免费网站成都百度搜索排名优化
  • 优秀的集团网站灯塔网站seo
  • 那个网站可以做家具效果图百度指数关键词搜索趋势
  • 手机网站常用代码微信管理系统登录
  • 网站被百度k是什么意思哪个平台可以接推广任务
  • app网站开发方案苏州网站建设制作公司
  • 网站开发课程设计体会视频推广
  • 上海市建设安全协会 - 网站首页深圳搜索引擎优化seo