-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgePatternEg.java
More file actions
148 lines (114 loc) · 3.09 KB
/
BridgePatternEg.java
File metadata and controls
148 lines (114 loc) · 3.09 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
package design_patterns.creational.bridge_pattern;
abstract class Variant {
String type;
Variant(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
}
class Modern extends Variant {
public Modern() {
super("Modern");
}
}
class Victorian extends Variant {
public Victorian() {
super("Victorian");
}
}
abstract class Product {
Variant variant;
String name;
Product(Variant variant, String name) {
this.variant = variant;
this.name = name;
}
boolean hasLegs() {
return !(this.variant.getType().equals("Modern"));
}
void displayLegsStatus() {
System.out.print(this.variant.getType() + " " + this.name + " ");
if (this.hasLegs()) {
System.out.println("has legs.");
} else {
System.out.println("does not have legs");
}
}
}
class Chair extends Product {
public Chair(Variant variant) {
super(variant, "Chair");
}
void sitOn() {
System.out.println("I am sitting on " + this.variant.getType() + " " + this.name);
}
}
class Sofa extends Product {
public Sofa(Variant variant) {
super(variant, "Sofa");
}
void sitOn() {
System.out.println("I am sitting on " + this.variant.getType() + " " + this.name);
}
}
class CoffeeTable extends Product {
public CoffeeTable(Variant variant) {
super(variant, "Coffee Table");
}
void keepOn() {
System.out.println(this.variant.getType() + " " + this.name + " is kept on floor");
}
}
/**
* Abstract Factory
* Product Family - Furniture
* Products - Chair, Sofa, and CoffeeTable
*/
class FurnitureFactory {
Variant variant;
FurnitureFactory(Variant variant) {
this.variant = variant;
}
Chair createChair() {
return new Chair(this.variant);
}
Sofa createSofa() {
return new Sofa(this.variant);
};
CoffeeTable createCoffeeTable() {
return new CoffeeTable(this.variant);
};
}
class Client {
private Chair chair;
private Sofa sofa;
private CoffeeTable coffeeTable;
Client(FurnitureFactory factory) {
this.chair = factory.createChair();
this.sofa = factory.createSofa();
this.coffeeTable = factory.createCoffeeTable();
}
void useFurniture() {
this.chair.sitOn();
this.sofa.sitOn();
this.coffeeTable.keepOn();
this.chair.displayLegsStatus();
this.sofa.displayLegsStatus();
this.coffeeTable.displayLegsStatus();
}
}
public class BridgePatternEg {
public static void main(String[] args) {
Variant victorian = new Victorian();
Variant modern = new Modern();
FurnitureFactory modernFactory = new FurnitureFactory(modern);
FurnitureFactory victorianFactory = new FurnitureFactory(victorian);
Client client1 = new Client(modernFactory);
Client client2 = new Client(victorianFactory);
client1.useFurniture();
System.out.println();
client2.useFurniture();
}
}