当前位置:网站首页>装饰器(二)
装饰器(二)
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
边栏推荐
- Iptables from introduction to mastery
- Classical dynamic programming: longest common subsequence
- Experiment 1 assignment
- JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)
- 简明 VIM 练级攻略
- Infix expression to suffix expression
- Express框架
- Swagger介绍和应用
- JVM真香系列:轻松理解class文件到虚拟机(下)
- abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
猜你喜欢
选择排序
构造回文的最小插入次数
Django之简易用户系统(3)
IT industry salary has been far ahead! Ten years later, is the programmer still a high paying profession?
Part 1 - Chapter 2 pointer operation
11 important operations of Python list
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
PAT_甲级_1056 Mice and Rice
随机推荐
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
Package subsystem in Simulink
Flink系列(0)——准备篇(流处理基础)
. net core cross platform resource monitoring library and dotnet tool
Regular backup of WordPress website program and database to qiniu cloud
Wechat applet related
综合架构的简述
Learn volatile, you change your mind, I see
Suffix expression to infix expression
ITerm2 配置和美化
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
MongoDB数据库
Chapter 2 programming exercises
Proficient in high concurrency and multithreading, but can't use ThreadLocal?
Mongodb database
解决go get下载包失败问题
构造回文的最小插入次数
Creating a text cloud or label cloud in Python
Constructors and prototypes
VirtualBox install centos7