-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactoryTest.java
More file actions
146 lines (112 loc) · 3.08 KB
/
AbstractFactoryTest.java
File metadata and controls
146 lines (112 loc) · 3.08 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
/*
A Creational Pattern.
Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.
*/
public class AbstractFactoryTest {
public static void main(String[] args) throws Exception{
System.out.println("-------------- ABSTRACT_FACTORY ---------------");
Factory factory = new IBMFactory();
Computer computer = factory.createComputer();
computer.boot();
Software software = factory.createSoftware();
software.start();
factory = new AppleFactory();
computer = factory.createComputer();
computer.boot();
software = factory.createSoftware();
software.start();
}
};
// Factory
abstract class Factory {
public abstract Computer createComputer();
public abstract Software createSoftware();
};
// factory 1
class IBMFactory extends Factory {
public Computer createComputer(){
return new ThinkCenter("ThinkCenter Turbo 123", "256MB", "40GB", "3 Ghz");
}
public Software createSoftware(){
return new Websphere("Websphere Application Server 5.1.1", "12123-324-23434-54545");
}
};
// Concrete factory 2
class AppleFactory extends Factory {
public Computer createComputer(){
return new Machintosh("Machintosh Paranoid 77Z", "512MB", "60GB", "3.6 Ghz");
}
public Software createSoftware(){
return new MacOS("MacOS 7.2.1", "22-33-NMCNNJ-33290");
}
};
// Product
abstract class Computer {
String ram = null;
String hdd = null;
String speed = null;
String model = null;
public abstract void boot();
};
// Concrete product 1
class Machintosh extends Computer {
Machintosh(String m, String r, String h, String s){
model = m;
ram = r;
hdd = h;
speed = s;
}
public void boot(){
System.out.println("This is a Machintosh Computer by Apple ");
System.out.println("Model: "+model);
System.out.println("RAM: "+ram);
System.out.println("HDD: "+hdd);
System.out.println("CPU Speed: "+speed);
}
};
// Concrete product 2
class ThinkCenter extends Computer {
ThinkCenter(String m, String r, String h, String s){
model = m;
ram = r;
hdd = h;
speed = s;
}
public void boot(){
System.out.println(" This is a ThinkCenter by IBM ");
System.out.println("Model: "+model);
System.out.println("RAM: "+ram);
System.out.println("HDD: "+hdd);
System.out.println("CPU Speed: "+speed);
}
};
abstract class Software {
String name = null;
String license = null;
public abstract void start();
};
// Concrete product 3
class Websphere extends Software {
Websphere(String n, String l){
name = n;
license = l;
}
public void start(){
System.out.println("Websphere by IBM is starting up ...");
System.out.println("Name: "+name);
System.out.println("License: "+license);
}
};
// Concrete product 4
class MacOS extends Software {
MacOS(String n, String l){
name = n;
license = l;
}
public void start(){
System.out.println("MacOS by Apple is starting up ...");
System.out.println("Name: "+name);
System.out.println("License: "+license);
}
};