当前位置:网站首页>装饰器(二)
装饰器(二)
2020-11-08 21:03:00 【8Years】
//
// 模拟咖啡
// 1.抽象组件:需要装饰的抽象对象(接口或者抽象父类)
// 2.具体组件:需要装饰的对象
// 3.抽象装饰类:包含了对抽象组件的引用以及装饰着共有的方法
// 4.具体装饰类:被装饰的对象
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.抽象组件
interface Drink{
int cost();
String Info();
}
// 2.具体组件:
class coffee implements Drink{
String name="原味咖啡";
public int cost() {
return 10;
}
@Override
public String Info() {
return name;
}
}
// 3.抽象装饰类:
class Decorate implements Drink{
//对抽象组件的引用
private Drink drink;
public Decorate(Drink drink) {
//这里是一个构造器(this)
this.drink = drink;
}
@Override
public int cost() {
return this.drink.cost();
}
@Override
public String Info() {
return this.drink.Info();
}
}
// 4.具体装饰类1:
class Milk extends Decorate{
public Milk(Drink drink) {
//这里是一个构造器,(super)
super(drink);
}
@Override
public int cost() {
return super.cost()*4;
}
@Override
public String Info() {
return super.Info()+"加入了牛奶";
}
}
// 4.具体装饰类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()+"加入了蔗糖";
}
}
输出结果
20--->原味咖啡加入了蔗糖
40--->原味咖啡加入了牛奶
80--->原味咖啡加入了蔗糖加入了牛奶
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4583813/blog/4708229
边栏推荐
- Learn volatile, you change your mind, I see
- IT industry salary has been far ahead! Ten years later, is the programmer still a high paying profession?
- 在Python中创建文字云或标签云
- 解决go get下载包失败问题
- Newbe.ObjectVisitor 样例 1
- The interface testing tool eolinker makes post request
- Express框架
- AI perfume is coming. Will you buy it?
- Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
- Tasks of the first week of information security curriculum design (analysis of 7 instructions)
猜你喜欢

go语言参数传递到底是传值还是传引用?

Interesting article sharing: what is the difference between C language and C + +, C?

Newbe.ObjectVisitor 样例 1

latex入门

Five factors to consider before choosing API management platform

C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?

Proficient in high concurrency and multithreading, but can't use ThreadLocal?

寻找性能更优秀的动态 Getter 和 Setter 方案

IT行业薪资一直遥遥领先!十年后的程序员,是否还是一个高薪职业?

Opencv solves the problem of ippicv download failure_ 2019_ lnx_ intel64_ general_ 20180723.tgz offline Download
随机推荐
Server side resolution of lengthfieldbasedframedecoder of GetBytes
Express framework
第五章
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
寻找性能更优秀的动态 Getter 和 Setter 方案
PAT_ Grade A_ 1056 Mice and Rice
都说程序员钱多空少,程序员真的忙到没时间回信息了吗?
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
构造函数和原型
C / C + + learning diary: original code, inverse code and complement code
寻找性能更优秀的不可变小字典
Python 列表的11个重要操作
动态规划答疑篇
MongoDB数据库
If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
Swagger介绍和应用
简明 VIM 练级攻略
存储过程动态查询处理方法