-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamble.java
More file actions
181 lines (146 loc) · 4.21 KB
/
examble.java
File metadata and controls
181 lines (146 loc) · 4.21 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
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
import java.util.ArrayList;
import java.util.List;
// Singleton: Ensures a single instance of the class
class Singleton {
private static Singleton instance;
private Singleton() {
// Private constructor to prevent instantiation
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void showMessage() {
System.out.println("Hello from Singleton!");
}
}
// Factory Method: Creates objects based on a common interface
interface Product {
void create();
}
class ConcreteProductA implements Product {
@Override
public void create() {
System.out.println("Created Product A");
}
}
class ConcreteProductB implements Product {
@Override
public void create() {
System.out.println("Created Product B");
}
}
interface Factory {
Product createProduct();
}
class ConcreteFactoryA implements Factory {
@Override
public Product createProduct() {
return new ConcreteProductA();
}
}
class ConcreteFactoryB implements Factory {
@Override
public Product createProduct() {
return new ConcreteProductB();
}
}
// Adapter: Allows objects with incompatible interfaces to work together
class LegacySystem {
void doLegacyStuff() {
System.out.println("Legacy system doing stuff.");
}
}
class AdapterLegacySystem implements Product {
private LegacySystem legacySystem;
AdapterLegacySystem(LegacySystem legacySystem) {
this.legacySystem = legacySystem;
}
@Override
public void create() {
legacySystem.doLegacyStuff();
}
}
// Composite: Represents part-whole hierarchies
abstract class Component {
abstract void operation();
}
class Leaf extends Component {
@Override
void operation() {
System.out.println("Leaf does its operation.");
}
}
class Composite extends Component {
private List<Component> children = new ArrayList<>();
void add(Component component) {
children.add(component);
}
@Override
void operation() {
System.out.println("Composite does its operation, and its children do theirs:");
for (Component child : children) {
child.operation();
}
}
}
// Observer: Defines a one-to-many dependency between objects
interface Observer {
void update(String message);
}
class ConcreteObserver implements Observer {
private String name;
ConcreteObserver(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received message: " + message);
}
}
class Subject {
private List<Observer> observers = new ArrayList<>();
void addObserver(Observer observer) {
observers.add(observer);
}
void removeObserver(Observer observer) {
observers.remove(observer);
}
void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
public class CombinedExample {
public static void main(String[] args) {
// Singleton Example
Singleton singleton = Singleton.getInstance();
singleton.showMessage();
// Factory Method Example
Factory factoryA = new ConcreteFactoryA();
Product productA = factoryA.createProduct();
productA.create();
Factory factoryB = new ConcreteFactoryB();
Product productB = factoryB.createProduct();
productB.create();
// Adapter Example
LegacySystem legacySystem = new LegacySystem();
Product adaptedSystem = new AdapterLegacySystem(legacySystem);
adaptedSystem.create();
// Composite Example
Component leaf = new Leaf();
Component composite = new Composite();
composite.add(leaf);
composite.operation();
// Observer Example
Subject subject = new Subject();
Observer observer1 = new ConcreteObserver("Observer 1");
Observer observer2 = new ConcreteObserver("Observer 2");
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notifyObservers("Hello Observers!");
}
}