当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Django之简易用户系统(3)
- 构造函数和原型
- Why need to use API management platform
- MongoDB增删改查操作
- 使用Fastai开发和部署图像分类器应用
- Tasks of the first week of information security curriculum design (analysis of 7 instructions)
- C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
- To introduce to you, this is my flow chart software—— draw.io
- [cloud service] there are so many ECS instances on alicloud server, how to select the type? Best practice note
- 第一部分——第1章概述
猜你喜欢
随机推荐
Creating a text cloud or label cloud in Python
iptables从入门到掌握
VirtualBox安装centos7
Brief introduction of Integrated Architecture
用两个栈实现队列
【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
C/C++学习日记:原码、反码和补码
进程 线程 协程
Wechat applet related
Django之简易用户系统(3)
LeetCode 45 跳跃游戏II
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
Swagger介绍和应用
第五章
opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
Programmers should know the URI, a comprehensive understanding of the article
. net core cross platform resource monitoring library and dotnet tool
学会了volatile,你变心了,我看到了
都说程序员钱多空少,程序员真的忙到没时间回信息了吗?







