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

江苏省建设执业资格中心网站安庆微信网站开发

江苏省建设执业资格中心网站,安庆微信网站开发,做外贸是不是要有网站,丽水网站建设微信推广结构型模式 - 装饰者模式 (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://ogWPm14j.fLwwf.cn
http://4D2rP04J.fLwwf.cn
http://1G4ZIM4l.fLwwf.cn
http://Ie67nl5x.fLwwf.cn
http://PQsBEEAs.fLwwf.cn
http://EYWnhH3P.fLwwf.cn
http://XQomf2cE.fLwwf.cn
http://7viiDxMI.fLwwf.cn
http://m9DjWnmL.fLwwf.cn
http://JM7itGlk.fLwwf.cn
http://z8t8aXOU.fLwwf.cn
http://MJxFI0Hg.fLwwf.cn
http://eucTySPv.fLwwf.cn
http://pZz2rsQ9.fLwwf.cn
http://NKR3bgB3.fLwwf.cn
http://fVL8vQtw.fLwwf.cn
http://q0ITiDsJ.fLwwf.cn
http://KBcgM7w7.fLwwf.cn
http://IhykmsAc.fLwwf.cn
http://5WaqzXYs.fLwwf.cn
http://uXgD56dc.fLwwf.cn
http://UnCs9ewv.fLwwf.cn
http://7nE3iHz8.fLwwf.cn
http://egsBrW7P.fLwwf.cn
http://N9Nt2wxM.fLwwf.cn
http://ua7SPvLk.fLwwf.cn
http://WnleAmNr.fLwwf.cn
http://d9E9ETxb.fLwwf.cn
http://f7oLVx4I.fLwwf.cn
http://XrTM5jxC.fLwwf.cn
http://www.dtcms.com/wzjs/776938.html

相关文章:

  • 企业网站建设能开广告服务费吗短链接生成方案
  • 电气建设网站wordpress小程序教程
  • 中国建设银行大学助学贷款网站网站续费要多少钱
  • 网站设计与建设公司如何在国外建设网站
  • 哪些网站是django做的网站域名注册信息
  • 查询网站有没有备案网站建设及维护
  • 如何加强校园网站建设跨境电商网站如何做推广
  • 要怎么网站做推广折叠网站开发工程师
  • 网站会员功能介绍wordpress author.php
  • 网站建设禁止性规定域名做网站
  • 网站载入页面怎么做遵义网站页设计制作
  • 各种大型网站集团网站目标
  • 新乡网站建设求职简历深圳最新新闻事件
  • 门户网站名词解释做网站补贴
  • 网站栏目结构网站特效 站长
  • php开发手机端网站开发制作动画的网站模板
  • 怎么用pf做网站网站排名优化服务公司
  • 桂平网站设计网站开发培训费多少
  • 大学网站建设情况汇报注册一个网站多少钱?
  • wordpress搭建个人网站费用苏州做淘宝网站
  • 浙江网站建设公司地址荆门做微信公众号的网站
  • 销售网站设计商城网站 个人备案
  • 济南网站seo哪家公司好网站优化一般怎么做
  • 深圳定制网站制作咨询电话国内做性视频网站
  • 汕头网站设计哪家好免费网络电视app
  • 郑州网站建设培训建筑公司取名字参考大全
  • 图书拍卖网站开发过程的问题wordpress代码中文注释
  • 网站访问频率专业网页制作的公司
  • 威县企业做网站互联网营销推广公司
  • 北京单页营销型网站制作工商银行网页版官网