当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
- An online accident caused by improper use of thread pool
- The interface testing tool eolinker makes post request
- 为什么需要使用API管理平台
- Mongodb add delete modify query operation
- 给大家介绍下,这是我的流程图软件 —— draw.io
- Proficient in high concurrency and multithreading, but can't use ThreadLocal?
- Array acquaintance
- Opencv solves the problem of ippicv download failure_ 2019_ lnx_ intel64_ general_ 20180723.tgz offline Download
- [elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
猜你喜欢
Is parameter passing in go language transfer value or reference?
Looking for a small immutable dictionary with better performance
简明 VIM 练级攻略
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
给大家介绍下,这是我的流程图软件 —— draw.io
WordPress网站程序和数据库定时备份到七牛云图文教程
Solve the failure of go get download package
Summary of interface test case ideas
如果把编程语言当武功绝学!C++是九阴真经,那程序员呢?
Swagger介绍和应用
随机推荐
Suffix expression to infix expression
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
精通高并发与多线程,却不会用ThreadLocal?
Brief introduction of Integrated Architecture
VirtualBox install centos7
The minimum insertion times of palindrome
APReLU:跨界应用,用于机器故障检测的自适应ReLU | IEEE TIE 2020
快来看看!AQS 和 CountDownLatch 有怎么样的关系?
Flink series (0) -- Preparation (basic stream processing)
Newbe.ObjectVisitor 样例 1
新手入坑指南:工作原因“重启”Deepin系统,发现真的香啊
200 programmers interview experience, all here
动态规划答疑篇
Using fastai to develop and deploy image classifier application
MYCAT build
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
SQL quick query
Process thread coroutine
Introduction to latex