策略模式

[Musings]
继续昨天的中介模式来加上策略模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
public enum Strategy {
BEST_SELLING, NORMAL, SEASONAL, LUXURY
}

public abstract class SupplyStrategy {
Publisher publisher;

public SupplyStrategy(Publisher publisher) {
this.publisher = publisher;
}

abstract void supply(String itemName);
}

enum ConditionType {
PERCENT, FIXED
}

public class NormalStrategy extends SupplyStrategy {
public NormalStrategy( Publisher publisher) {
super( publisher);
}

@Override
public void supply(String itemName) {
System.out.println("NormalStrategy supply() invoked");
if ((publisher.getStorageMap().get(itemName) / publisher.getLimit() )< 0.1) {
publisher.updateItem(itemName, (int) (publisher.getLimit() * 0.5));
}
}
}

public class LuxuryStrategy extends SupplyStrategy {
public LuxuryStrategy(Publisher publisher) {
super(publisher);
}

@Override
public void supply(String itemName) {
System.out.println("LuxuryStrategy supply() invoked");
if (publisher.getStorageMap().get(itemName) < 5) {
publisher.updateItem(itemName, 10);
}
}
}

public class BestSellingProductStrategy extends SupplyStrategy {
private double trigger;

public BestSellingProductStrategy(double trigger, Publisher publisher) {
super(publisher);
this.trigger = trigger;
}

@Override
public void supply(String itemName) {
System.out.println("BestSellingProductStrategy supply() invoked");
if ((publisher.getStorageMap().get(itemName) / publisher.getLimit()) < this.trigger) {
publisher.updateItem(itemName, (int) (publisher.getLimit() * 0.8));
}
}
}

//稍微修改下中介者类
public class Mediator {
private HashMap<Publisher, ArrayList<Observer>> hashMap;

public Mediator() {
this.hashMap = new HashMap<>();
}

public void notify(Publisher publish, String itemName, int count) {
ArrayList<Observer> observers = hashMap.get(publish);
boolean supplyFlag = false;
// 添加逻辑基于count,itemName,也可以结合多个不同的item
if (observers != null && !observers.isEmpty()) {
for (Observer observer : observers) {
switch (observer.getRole()) {
case "Procure":
if (count > 5) {
observer.push("Procure货物充足!✅");
} else {
observer.push("Procure 货物不足❌,准备补充");
supplyFlag = true;
}
break;
case "Shop":
if (count > 5) {
observer.push("Shop 货物充足!✅");
} else {
observer.push("Shop 货物不足❌,准备下架");
supplyFlag = true;
}
break;
case "Delivery":
if (count > 5) {
observer.push("Delivery 货物充足,车辆平均分配!✅");
} else {
observer.push("Delivery 货物不足❌,准备帮忙采购");
supplyFlag = true;
}
break;
}
}
}

if (supplyFlag) supply(publish, itemName);
}

public void supply(Publisher publisher, String itemName) {
//Supply Strategy invoke
switch (publisher.getStrategy()) {
case LUXURY -> new LuxuryStrategy(publisher).supply(itemName);
case BEST_SELLING -> new BestSellingProductStrategy(0.2, publisher).supply(itemName);
default -> new NormalStrategy(publisher).supply(itemName);
}
}

public void subscribe(Publisher publisher, Observer observer) {
hashMap.computeIfAbsent(publisher, k -> new ArrayList<>()).add(observer);
}

public void unsubscribe(Publisher publisher, Observer observer) {
hashMap.computeIfPresent(publisher, (k, v) -> {
v.remove(observer);
return v.isEmpty() ? null : v;
});
}

}


public class Main {
public static void main(String[] args) {
Mediator mediator = new Mediator();
Storage storage = new Storage(100, new HashMap<>(), "联华超市仓库", mediator);
Procurement procurement = new Procurement("Procure");
ShunFenDelivery delivery = new ShunFenDelivery("Delivery");
Shop shop = new Shop("Shop");

mediator.subscribe(storage, procurement);
mediator.subscribe(storage, delivery);
mediator.subscribe(storage, shop);

storage.add("Crips", 7);
storage.reduce("Crips");
storage.reduce("Crips");
storage.reduce("Crips");
storage.reduce("Crips");
}
}

**Output log:**
Procure货物充足!✅
Delivery 货物充足,车辆平均分配!✅
Shop 货物充足!✅
Procure 货物不足❌,准备补充
Delivery 货物不足❌,准备帮忙采购
Shop 货物不足❌,准备下架
NormalStrategy supply() invoked
{Crips=55}
Procure货物充足!✅
Delivery 货物充足,车辆平均分配!✅
Shop 货物充足!✅
Procure货物充足!✅
Delivery 货物充足,车辆平均分配!✅
Shop 货物充足!✅

功能没问题了,然后我们来优化下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

public void supply(Publisher publisher, String itemName) {
//Supply Strategy invoke
switch (publisher.getStrategy()) {
case LUXURY -> new LuxuryStrategy(publisher).supply(itemName);
case BEST_SELLING -> new BestSellingProductStrategy(0.2, publisher).supply(itemName);
default -> new NormalStrategy(publisher).supply(itemName);
}
}
// 这里就可以用工厂模式:
public class SupplyStrategyFactory {
public static SupplyStrategy createStrategy( Publisher publisher) {
return switch (publisher.getStrategy()){
case LUXURY -> new LuxuryStrategy(publisher);
case BEST_SELLING -> new BestSellingProductStrategy(0.2,publisher);
default -> new NormalStrategy(publisher);
};
}

//然后调用的地方就可以写
public void supply(Publisher publisher, String itemName) {
SupplyStrategyFactory.createStrategy(publisher).supply(itemName);
}

}