当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
- Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
- 【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明
- Server side resolution of lengthfieldbasedframedecoder of GetBytes
- To introduce to you, this is my flow chart software—— draw.io
- Array acquaintance
- JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)
- 寻找性能更优秀的动态 Getter 和 Setter 方案
- Is parameter passing in go language transfer value or reference?
猜你喜欢

为什么需要使用API管理平台

Part 1 - Chapter 2 pointer operation

Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020

接口测试用例思路总结

Using annotation + interceptor to implement asynchronous execution

动态规划答疑篇

VirtualBox安装centos7

Chapter five

Programmers should know the URI, a comprehensive understanding of the article

Package subsystem in Simulink
随机推荐
Django之简易用户系统(3)
Swagger介绍和应用
给大家介绍下,这是我的流程图软件 —— draw.io
Why need to use API management platform
Problem solving templates for subsequence problems in dynamic programming
C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
JVM真香系列:轻松理解class文件到虚拟机(上)
Experiment 1 assignment
在Python中创建文字云或标签云
CMS garbage collector
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
解决go get下载包失败问题
Iterm2 configuration and beautification
动态规划之子序列问题解题模板
实现图片的复制
PAT_甲级_1056 Mice and Rice
Express框架
第一部分——第2章指针操作