Repository navigation
Expand file tree
/
Copy pathDesign_patterns.java
More file actions
162 lines (129 loc) · 3.32 KB
/
Copy pathDesign_patterns.java
File metadata and controls
162 lines (129 loc) · 3.32 KB
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
public class Design_patterns {
}
public class Singleton {
private static Singleton instance;
private Singleton() {
// private constructor to prevent instantiation from outside
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public abstract class Vehicle {
public abstract void start();
}
public class Car extends Vehicle {
@Override
public void start() {
System.out.println("Car started");
}
}
public class Motorcycle extends Vehicle {
@Override
public void start() {
System.out.println("Motorcycle started");
}
}
public interface VehicleFactory {
Vehicle createVehicle();
}
public class CarFactory implements VehicleFactory {
@Override
public Vehicle createVehicle() {
return new Car();
}
}
public class MotorcycleFactory implements VehicleFactory {
@Override
public Vehicle createVehicle() {
return new Motorcycle();
}
}
public interface Observer {
void update(String message);
}
public interface Subject {
void attach(Observer observer);
void detach(Observer observer);
void notifyObservers(String message);
}
public class MessagePublisher implements Subject {
private List<Observer> observers = new ArrayList<>();
@Override
public void attach(Observer observer) {
observers.add(observer);
}
@Override
public void detach(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
public class MessageSubscriberOne implements Observer {
@Override
public void update(String message) {
System.out.println("Subscriber One received: " + message);
}
}
public class MessageSubscriberTwo implements Observer {
@Override
public void update(String message) {
System.out.println("Subscriber Two received: " + message);
}
}
public interface Coffee {
double getCost();
String getDescription();
}
public class SimpleCoffee implements Coffee {
@Override
public double getCost() {
return 1;
}
@Override
public String getDescription() {
return "Simple coffee";
}
}
public abstract class CoffeeDecorator implements Coffee {
protected final Coffee decoratedCoffee;
public CoffeeDecorator(Coffee decoratedCoffee) {
this.decoratedCoffee = decoratedCoffee;
}
public double getCost() {
return decoratedCoffee.getCost();
}
public String getDescription() {
return decoratedCoffee.getDescription();
}
}
public class Milk extends CoffeeDecorator {
public Milk(Coffee decoratedCoffee) {
super(decoratedCoffee);
}
public double getCost() {
return super.getCost() + 0.5;
}
public String getDescription() {
return super.getDescription() + ", milk";
}
}
public class Whip extends CoffeeDecorator {
public Whip(Coffee decoratedCoffee) {
super(decoratedCoffee);
}
public double getCost() {
return super.getCost() + 0.7;
}
public String getDescription() {
return super.getDescription() + ", whip";
}
}