Skip to content

Commit 19ca14b

Browse files
committed
add decorator pattern , jvm
1 parent 295a956 commit 19ca14b

187 files changed

Lines changed: 3768 additions & 18 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:26
6+
* @Function Description :
7+
* 具体的 Decorator, 这里就是调味品
8+
*/
9+
10+
public class Chocolate extends Decorator {
11+
public Chocolate(Drink obj) {
12+
super(obj);
13+
setDes(" 巧克力 ");
14+
setPrice(3.0f); // 调味品 的价格
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:27
6+
* @Function Description :
7+
*/
8+
public class Coffee extends Drink {
9+
@Override
10+
public float cost() {
11+
// TODO Auto-generated method stub
12+
return super.getPrice();
13+
}
14+
15+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:27
6+
* @Function Description :
7+
*/
8+
public class CoffeeBar {
9+
public static void main(String[] args) {
10+
// TODO Auto-generated method stub
11+
// 装饰者模式下的订单:2 份巧克力+一份牛奶的 LongBlack
12+
// 1. 点一份 LongBlack
13+
Drink order = new LongBlack();
14+
System.out.println("费用 1=" + order.cost());
15+
System.out.println("描述=" + order.getDes());
16+
// 2. order 加入一份牛奶
17+
order = new Milk(order);
18+
System.out.println("order 加入一份牛奶 费用 =" + order.cost());
19+
System.out.println("order 加入一份牛奶 描述 = " + order.getDes());
20+
// 3. order 加入一份巧克力
21+
order = new Chocolate(order);
22+
System.out.println("order 加入一份牛奶 加入一份巧克力 费用 =" + order.cost());
23+
System.out.println("order 加入一份牛奶 加入一份巧克力 描述 = " + order.getDes());
24+
// 3. order 加入一份巧克力
25+
order = new Chocolate(order);
26+
System.out.println("order 加入一份牛奶 加入 2 份巧克力 费用 =" + order.cost());
27+
System.out.println("order 加入一份牛奶 加入 2 份巧克力 描述 = " + order.getDes());
28+
System.out.println("===========================");
29+
Drink order2 = new DeCaf();
30+
System.out.println("order2 无因咖啡 费用 =" + order2.cost());
31+
System.out.println("order2 无因咖啡 描述 = " + order2.getDes());
32+
order2 = new Milk(order2);
33+
System.out.println("order2 无因咖啡 加入一份牛奶 费用 =" + order2.cost());
34+
System.out.println("order2 无因咖啡 加入一份牛奶 描述 = " + order2.getDes());
35+
}
36+
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:28
6+
* @Function Description :
7+
*/
8+
public class DeCaf extends Coffee {
9+
public DeCaf() {
10+
setDes(" 无因咖啡 ");
11+
setPrice(1.0f);
12+
}
13+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:28
6+
* @Function Description :
7+
*/
8+
public class Decorator extends Drink {
9+
private Drink obj;
10+
11+
public Decorator(Drink obj) { //组合
12+
// TODO Auto-generated constructor stub
13+
this.obj = obj;
14+
}
15+
16+
@Override
17+
public float cost() {
18+
// TODO Auto-generated method stub
19+
// getPrice 自己价格
20+
return super.getPrice() + obj.cost();
21+
}
22+
23+
@Override
24+
public String getDes() {
25+
// TODO Auto-generated method stub
26+
// obj.getDes() 输出被装饰者的信息 des父类的
27+
return des + " " + getPrice() + " && " + obj.getDes();
28+
}
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:29
6+
* @Function Description :
7+
*/
8+
public abstract class Drink {
9+
public String des; // 描述
10+
private float price = 0.0f;
11+
12+
public String getDes() {
13+
return des;
14+
}
15+
public void setDes(String des) {
16+
this.des = des;
17+
}
18+
public float getPrice() {
19+
return price;
20+
}
21+
public void setPrice(float price) {
22+
this.price = price;
23+
}
24+
25+
//计算费用的抽象方法
26+
//子类来实现
27+
public abstract float cost();
28+
29+
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:30
6+
* @Function Description :
7+
*/
8+
public class Espresso extends Coffee {
9+
public Espresso() {
10+
setDes(" 意大利咖啡 ");
11+
setPrice(6.0f);
12+
}
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:31
6+
* @Function Description :
7+
*/
8+
public class LongBlack extends Coffee {
9+
public LongBlack() {
10+
setDes(" longblack ");
11+
setPrice(5.0f);
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:32
6+
* @Function Description :
7+
*/
8+
public class Milk extends Decorator {
9+
public Milk(Drink obj) {
10+
super(obj);
11+
// TODO Auto-generated constructor stub
12+
setDes(" 牛奶 ");
13+
setPrice(2.0f);
14+
}
15+
16+
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package decorator;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-05-13-15:33
6+
* @Function Description :
7+
*/
8+
public class ShortBlack extends Coffee {
9+
public ShortBlack() {
10+
setDes(" shortblack ");
11+
setPrice(4.0f);
12+
}
13+
14+
}

0 commit comments

Comments
 (0)