-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesignPattern.java
210 lines (168 loc) · 5.02 KB
/
DesignPattern.java
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Singleton
// only one instance
public class ParkingLot {
private ParkingLot() {}
private static class SingletonParkingLot {
static final ParkingLot _instance = new ParkingLot();
}
public static synchronized ParkingLot getInstance() {
return SingletonParkingLot._instance;
}
}
// State
// class methods will change state by using setState()
// different logics on different states
public class VendingMachine {
private State state;
private HasSelectionState hasSelectionState;
private NoSelectionState noSelectionState;
private SoldState soldState;
private SoldOutState soldOutState;
public void setState(State s) {
state = s;
}
}
public interface State {
public float selectItem(String selection);
public void insertCoins(List<Coin> coins);
public Pair<Item, List<Coin>> executeTransaction();
public List<Coin> cancelTransaction();
}
public class HasSelectionState implements State {
VendingMachine vendingMachine = null;
public HasSelectionState(VendingMachine machine) {
vendingMachine = machine;
}
public float selectItem(String selection) {}
public void insertCoins(List<Coin> coins) {}
public Pair<Item, List<Coin>> executeTransaction() {}
public List<Coin> cancelTransaction() {}
}
public class NoSelectionState implements State {
VendingMachine vendingMachine = null;
public NoSelectionState(VendingMachine machine) {
vendingMachine = machine;
}
public float selectItem(String selection) {}
public void insertCoins(List<Coin> coins) {}
public Pair<Item, List<Coin>> executeTransaction() {}
public List<Coin> cancelTransaction() {}
}
public class SoldState implements State {
VendingMachine vendingMachine = null;
public SoldState(VendingMachine machine) {
vendingMachine = machine;
}
public float selectItem(String selection) {}
public void insertCoins(List<Coin> coins) {}
public Pair<Item, List<Coin>> executeTransaction() {}
public List<Coin> cancelTransaction() {}
}
public class SoldOutState implements State {
VendingMachine vendingMachine = null;
public SoldOutState(VendingMachine machine) {
vendingMachine = machine;
}
public float selectItem(String selection) {}
public void insertCoins(List<Coin> coins) {}
public Pair<Item, List<Coin>> executeTransaction() {}
public List<Coin> cancelTransaction() {}
}
// Adapter
// convert coin's integer value to a string ItemName
public class CoinAdapter implements Item {
private Coin coin;
public CoinAdapter(Coin coin) {
this.coin = coin;
}
public String getItemName() {
return new String(coin.getValue());
}
}
// Strategy
// behavior
public interface Strategy {
public void processPayment(Payment payment);
}
public class PaypalStrategy implements Strategy {
public void processPayment(Payment payment) {}
}
public class CreditCardStrategy implements Strategy {
public void processPayment(Payment payment) {}
}
// Factory
// creation
public class StrategyFactory {
public Strategy createStrategy(Payment payment){
if (payment.getMethod().equals("paypal"))
strategy = new PaypalStrategy();
else if (payment.getMethod().equals("credit card"))
strategy = new CreditCardStrategy();
}
public void pay(Payment payment) {
strategy = createStrategy(payment);
strategy.processPayment(payment);
}
}
// Decorator
public interface Coffee {
public double getCost();
public String getIngredients();
}
public class SimpleCoffee implements Coffee {
@Override
public double getCost() {
return 1;
}
@Override
public String getIngredients() {
return "Coffee";
}
}
public abstract class CoffeeDecorator implements Coffee {
protected final Coffee decoratedCoffee;
public CoffeeDecorator(Coffee c) {
this.decoratedCoffee = c;
}
public double getCost() {
return decoratedCoffee.getCost();
}
public String getIngredients() {
return decoratedCoffee.getIngredients();
}
}
class WithMilk extends CoffeeDecorator {
public WithMilk(Coffee c) {
super(c);
}
public double getCost() {
return super.getCost() + 0.5
}
public String getIngredients() {
return super.getIngredients() + ", Milk";
}
}
class WithSprinkles extends CoffeeDecorator {
public WithSprinkles(Coffee c) {
super(c);
}
public double getCost() {
return super.getCost() + 0.2
}
public String getIngredients() {
return super.getIngredients() + ", Sprinkles";
}
}
public class CoffeeMaker {
public static void printInfo(Coffee c) {
System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
}
public static void main(String[] args) {
Coffee c = new SimpleCoffee();
printInfo(c);
c = new WithMilk(c);
printInfo(c);
c = new WithSprinkles(c);
printInfo(c);
}
}