当前位置:网站首页>Decorator (2)
Decorator (2)
2020-11-08 21:03:00 【8Years】
//
// Simulation coffee
// 1. Abstract components : Abstract objects that need decoration ( Interface or abstract parent class )
// 2. Specific components : Objects that need decoration
// 3. Abstract decorator : Contains references to abstract components and decorates common methods
// 4. Concrete decoration : Objects to be decorated
public class TestDecoretorTwo {
public static void main(String[] args) {
Drink coffee = new coffee();
Drink suger = new Suger(coffee);
System.out.println(suger.cost() + "--->" + suger.Info());
Drink milk = new Milk(coffee);
System.out.println(milk.cost() + "--->" + milk.Info());
Drink milk1 = new Milk(suger);
System.out.println(milk1.cost() + "--->" + milk1.Info());
}
}
// 1. Abstract components
interface Drink{
int cost();
String Info();
}
// 2. Specific components :
class coffee implements Drink{
String name=" Original coffee ";
public int cost() {
return 10;
}
@Override
public String Info() {
return name;
}
}
// 3. Abstract decorator :
class Decorate implements Drink{
// References to abstract components
private Drink drink;
public Decorate(Drink drink) {
// Here's a constructor (this)
this.drink = drink;
}
@Override
public int cost() {
return this.drink.cost();
}
@Override
public String Info() {
return this.drink.Info();
}
}
// 4. Concrete decoration 1:
class Milk extends Decorate{
public Milk(Drink drink) {
// Here's a constructor ,(super)
super(drink);
}
@Override
public int cost() {
return super.cost()*4;
}
@Override
public String Info() {
return super.Info()+" With milk ";
}
}
// 4. Concrete decoration 2:
class Suger extends Decorate{
public Suger(Drink drink) {
super(drink);
}
@Override
public int cost() {
return super.cost()*2;
}
@Override
public String Info() {
return super.Info()+" Added sugar ";
}
}
Output results
20---> Original coffee with sugar
40---> Original coffee with milk
80---> Original coffee with sugar and milk
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
边栏推荐
- Deep copy
- go语言参数传递到底是传值还是传引用?
- Server side resolution of lengthfieldbasedframedecoder of GetBytes
- 【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明
- CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
- To introduce to you, this is my flow chart software—— draw.io
- 寻找性能更优秀的不可变小字典
- 接口测试工具Eolinker进行post请求
- Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
- 动态规划设计:最大子数组
猜你喜欢

Five design schemes of singleton mode

If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?

选择排序

Regular backup of WordPress website program and database to qiniu cloud

API生命周期的5个阶段

在Python中创建文字云或标签云

. net core cross platform resource monitoring library and dotnet tool

Part I - Chapter 1 Overview

装饰器(一)

JVM真香系列:轻松理解class文件到虚拟机(下)
随机推荐
Opencv solves the problem of ippicv download failure_ 2019_ lnx_ intel64_ general_ 20180723.tgz offline Download
都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
Package subsystem in Simulink
Five factors to consider before choosing API management platform
200人的程序员面试经验,都在这里了
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?
PAT_甲级_1056 Mice and Rice
动态规划设计:最大子数组
Summary of interface test case ideas
ITerm2 配置和美化
API生命周期的5个阶段
实现图片的复制
Dynamic planning
To introduce to you, this is my flow chart software—— draw.io
The interface testing tool eolinker makes post request
. net core cross platform resource monitoring library and dotnet tool
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
如何将PyTorch Lightning模型部署到生产中
【Elasticsearch 技术分享】—— 十张图带大家看懂 ES 原理 !明白为什么说:ES 是准实时的!