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

商洛网站制作seo合作

商洛网站制作,seo合作,做网站接活犯法吗,jsp做新闻系统门户网站Component是基础接口 ConcreteComponent是Component的具体实现类,也就是等等要装饰的类 Decorator是装饰类,它实现Component,并且内部维护一个Component,可以使用构造方法来进行初始化 剩下的就是具体的装饰了 装饰模式&#…

Component是基础接口

ConcreteComponent是Component的具体实现类,也就是等等要装饰的类

Decorator是装饰类,它实现Component,并且内部维护一个Component,可以使用构造方法来进行初始化

剩下的就是具体的装饰了

装饰模式,也叫包装模式。说白了,装饰就是用来替代继承的一个方案(比如Java中的BufferedReader就是),他可以在原来的功能的基础上增加一些功能。

来一个具体的,简单的例子

/*** 基础接口* @author BarryLee* @2018年10月26日@下午8:53:53*/
public interface Car {/*** 最基本的方法--可以跑*/public abstract void run();/*** 展示当前的所有功能*/public abstract void show();
}
/*** 具体实现类,等等要装饰的一个类,实现Car的一个类* @author BarryLee* @2018年10月26日@下午8:54:17*/
public class RunCar implements Car{@Overridepublic void run() {System.out.println("跑");}@Overridepublic void show() {this.run();}}
/*** 装饰类* @author BarryLee* @2018年10月26日@下午9:59:19*/
public class CarDecorator implements Car{private Car car ;public CarDecorator(Car car) {this.car = car;}public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}@Overridepublic void run() {car.run();}@Overridepublic void show() {car.show();}
}	
/*** 具体装饰类1* @author BarryLee* @2018年10月26日@下午9:59:28*/
public class FlyCarDeco extends CarDecorator{public FlyCarDeco(Car car) {super(car);}@Overridepublic void run() {super.getCar().run();}public void fly() {System.out.println("可以飞");}@Overridepublic void show() {super.getCar().show();this.fly();}
}
/*** 具体装饰类2* @author BarryLee* @2018年10月26日@下午9:59:42*/
public class SwimCarDeco extends CarDecorator{public SwimCarDeco(Car car) {super(car);}public void swim() {System.out.println("可以游");}@Overridepublic void show() {super.getCar().show();this.swim();}@Overridepublic void run() {System.out.println("跑");}
}
/*** 测试* @author BarryLee* @2018年10月26日@下午10:00:00*/
public class TestDecorator {@Testpublic void test() {Car car = new RunCar();car.show();//跑}@Testpublic void test1() {Car car = new RunCar();Car flyCar = new FlyCarDeco(car);flyCar.show();/*跑可以飞*/}@Testpublic void test2() {Car car = new RunCar();Car flyCar = new FlyCarDeco(car);flyCar.show();System.out.println("-----");Car swimFlyCar = new SwimCarDeco(flyCar);swimFlyCar.show();/*跑可以飞-----跑可以飞可以游*/}
}

/2019.9.22更新//

下面是Head First 设计模式中的例子

需求是,一个咖啡店,有很多中咖啡,每种咖啡可以搭配不同的调料,要算出价格,同时有一个描述

package cn.bl.decorator.brverage;/*** @Deacription 这是咖啡店,也是测试代码* @Author BarryLee* @Date 2019/9/22 17:05*/
public class Main {public static void main(String[] args) {Beverage b1 = new Espresso();System.out.println(b1.getDescription() + ": " + b1.cost());Beverage b2 = new HouseBlend();b2 = new Mocha(b2);b2 = new Whip(b2);System.out.println(b2.getDescription() + ": " + b2.cost());}
}
package cn.bl.decorator.brverage;/*** @Deacription 饮料抽象类,相当于Conponent* @Author BarryLee* @Date 2019/9/22 17:10*/
public abstract class Beverage {String description = "Unknown description";public String getDescription() {return description;}public abstract double cost();}
package cn.bl.decorator.brverage;/*** @Deacription 浓缩咖啡,饮料的具体实现,相当于ConcreteComponent* @Author BarryLee* @Date 2019/9/22 17:21*/
public class Espresso extends Beverage {/*** 构造器,设置饮料的描述* description来自beverage*/public Espresso() {description = "espresso coffee";}/*** 返回Espresso的价格* @return*/@Overridepublic double cost() {return 1.99;}
}
package cn.bl.decorator.brverage;/*** @Deacription 混合咖啡,相当于ConcreteComponent* @Author BarryLee* @Date 2019/9/22 17:28*/
public class HouseBlend extends Beverage{public HouseBlend() {description = "house blend coffee";}@Overridepublic double cost() {return .89;}
}
package cn.bl.decorator.brverage;/*** @Deacription 调料抽象类,也就是装饰者,相当于类图中的Decorator* 有抽象方法一定就是抽象类** 首先,必须让Condiment Decorator能够取代Beverage* 所以将CondimentDecorator拓展自Beverage** @Author BarryLee* @Date 2019/9/22 17:12*/
public abstract class CondimentDecorator extends Beverage{public abstract String getDescription();}
package cn.bl.decorator.brverage;/*** @Deacription 摩卡调料,是一个具体的装饰者,所以它拓展自CondimentDecorator,相当于ConcreteDecorator* @Author BarryLee* @Date 2019/9/22 22:57*/
public class Mocha extends CondimentDecorator{Beverage beverage;public Mocha(Beverage beverage) {this.beverage = beverage;}@Overridepublic String getDescription() {return beverage.getDescription() + ", mocha";}@Overridepublic double cost() {return beverage.cost() + .20;}
}
package cn.bl.decorator.brverage;/*** @Deacription 泡泡调料,相当于ConcreteDecorator* @Author BarryLee* @Date 2019/9/22 23:12*/
public class Whip extends CondimentDecorator{Beverage beverage;public Whip(Beverage beverage) {this.beverage = beverage;}@Overridepublic String getDescription() {return beverage.getDescription() + ", whip";}@Overridepublic double cost() {return beverage.cost() + .22;}
}

执行结果是

espresso coffee: 1.99
house blend coffee, mocha, whip: 1.31

 

http://www.lbrq.cn/news/2422063.html

相关文章:

  • 免费企业建站系统排名360搜索优化
  • 驾校一点通网站怎么做电商网站排名
  • 网站建设和编程的区别丽水百度seo
  • 做网站建设的公司有哪些内容品牌营销成功案例
  • 网站备案后 如何建设长沙百家号seo
  • 湖北网站建设 鄂 icp长春seo顾问
  • 长春建设集团网站百度搜索引擎收录
  • 微信对接网站可以做301跳转吗今日百度小说排行榜风云榜
  • 西安高校定制网站建设公司推荐电商网站规划
  • 企业网站搭建哪家好新闻源发稿平台
  • 动软代码生成器 做网站网站百度不收录
  • 北京通州网站建设石家庄网络seo推广
  • 网站建设标准规范济南谷歌推广
  • 杭州企业网站优化哪家公司建设网站好
  • 衡水做网站开发的百度怎么发自己的小广告
  • 网站后台密码忘了怎么办今日头条新闻大事件
  • 上海网站建设lv cn在线bt磁力搜索
  • 做购物网站哪种服务器好整合营销经典案例
  • 做任务兼职赚钱的网站有哪些链网
  • 电商网站开发文献汇总seo优化查询
  • 西安网站建站优化网站推广软件免费
  • 建站吧网站建设seo优化平台
  • 宝安最好的网站建设在线视频用什么网址
  • 网站做推广需要营业执照广东seo网站推广代运营
  • 建立网站的优势crm
  • 新鸿儒网站seo教程论坛
  • 社区做图网站北京网站推广排名服务
  • 上海电子通科技网站建设seoul怎么读
  • 兖州网站建设有没有自动排名的软件
  • 网站建设的产品类型是什么电脑培训网
  • PyQt5—QInputDialog 学习笔记
  • 用基础模型构建应用(第十章)AI Engineering: Building Applications with Foundation Models学习笔记
  • Neo4j graph database
  • XTTS实现语音克隆:精确控制音频格式与生成流程【TTS的实战指南】
  • TypeScript 中替代 Interface 的方案
  • 奥比中光双目摄像头实现物品抓取的机器人系统