-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
355 lines (284 loc) · 16.2 KB
/
Main.java
File metadata and controls
355 lines (284 loc) · 16.2 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// This is a Java package declaration for the class Computer, which is in the "com.journaldev.design.model" package.
package com.journaldev.design.model;
// This is an abstract class named Computer.
public abstract class Computer {
// These are abstract methods that must be implemented by subclasses to get information about the computer's hardware components.
public abstract String getRAM(); // Get the RAM (Random Access Memory) of the computer.
public abstract String getHDD(); // Get the HDD (Hard Disk Drive) of the computer.
public abstract String getCPU(); // Get the CPU (Central Processing Unit) of the computer.
// This is an overridden toString() method that provides a string representation of the computer's hardware components.
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class PC, which is in the "com.journaldev.design.model" package.
package com.journaldev.design.model;
// This is a subclass named PC that extends the abstract class Computer.
public class PC extends Computer {
// These are private instance variables to store the hardware components of the PC.
private String ram; // Stores RAM (Random Access Memory).
private String hdd; // Stores HDD (Hard Disk Drive).
private String cpu; // Stores CPU (Central Processing Unit).
// This is a constructor for the PC class that initializes its hardware components.
public PC(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
// These methods override the abstract methods from the Computer class to provide specific information about the PC's hardware components.
@Override
public String getRAM() {
return this.ram; // Returns the RAM of the PC.
}
@Override
public String getHDD() {
return this.hdd; // Returns the HDD of the PC.
}
@Override
public String getCPU() {
return this.cpu; // Returns the CPU of the PC.
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class Server, which is in the "com.journaldev.design.model" package.
package com.journaldev.design.model;
// This is a subclass named Server that extends the abstract class Computer.
public class Server extends Computer {
// These are private instance variables to store the hardware components of the server.
private String ram; // Stores RAM (Random Access Memory).
private String hdd; // Stores HDD (Hard Disk Drive).
private String cpu; // Stores CPU (Central Processing Unit).
// This is a constructor for the Server class that initializes its hardware components.
public Server(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
// These methods override the abstract methods from the Computer class to provide specific information about the server's hardware components.
@Override
public String getRAM() {
return this.ram; // Returns the RAM of the server.
}
@Override
public String getHDD() {
return this.hdd; // Returns the HDD of the server.
}
@Override
public String getCPU() {
return this.cpu; // Returns the CPU of the server.
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class ComputerFactory, which is in the "com.journaldev.design.factory" package.
package com.journaldev.design.factory;
// Import statements to include necessary classes.
import com.journaldev.design.model.Computer;
import com.journaldev.design.model.PC;
import com.journaldev.design.model.Server;
// This is a factory class named ComputerFactory.
public class ComputerFactory {
// This is a static method that creates and returns an instance of a Computer based on the provided type and hardware components.
// It takes the following parameters:
// - type: A string indicating the type of computer to create ("PC" or "Server").
// - ram: A string representing the RAM (Random Access Memory) of the computer.
// - hdd: A string representing the HDD (Hard Disk Drive) of the computer.
// - cpu: A string representing the CPU (Central Processing Unit) of the computer.
public static Computer getComputer(String type, String ram, String hdd, String cpu) {
// Check if the type is "PC" (case-insensitive) and create a PC instance if so.
if ("PC".equalsIgnoreCase(type)) {
return new PC(ram, hdd, cpu);
}
// Check if the type is "Server" (case-insensitive) and create a Server instance if so.
else if ("Server".equalsIgnoreCase(type)) {
return new Server(ram, hdd, cpu);
}
// If the type is neither "PC" nor "Server," return null.
return null;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class TestFactory, which is in the "com.journaldev.design.test" package.
package com.journaldev.design.test;
// Import statements to include necessary classes.
import com.journaldev.design.factory.ComputerFactory;
import com.journaldev.design.model.Computer;
// This is a class named TestFactory that contains the main method for testing the computer factory.
public class TestFactory {
// The main method is the entry point of the program.
public static void main(String[] args) {
// Create a PC instance using the ComputerFactory and provide the hardware components.
Computer pc = ComputerFactory.getComputer("pc", "2 GB", "500 GB", "2.4 GHz");
// Create a Server instance using the ComputerFactory and provide the hardware components.
Computer server = ComputerFactory.getComputer("server", "16 GB", "1 TB", "2.9 GHz");
// Print the configuration of the PC and Server using the toString() method of the Computer class.
System.out.println("Factory PC Config::" + pc);
System.out.println("Factory Server Config::" + server);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is an abstract class named Computer, which serves as a base class for computer-related classes.
public abstract class Computer {
// These are abstract methods that must be implemented by subclasses to get information about the computer's hardware components.
public abstract String getRAM(); // Get the RAM (Random Access Memory) of the computer.
public abstract String getHDD(); // Get the HDD (Hard Disk Drive) of the computer.
public abstract String getCPU(); // Get the CPU (Central Processing Unit) of the computer.
// This is an overridden toString() method that provides a formatted string representation of the computer's hardware components.
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ", CPU=" + this.getCPU();
}
}
// This is a concrete class named PC that extends the Computer abstract class and provides specific implementations for hardware components.
public class PC extends Computer {
// These are private instance variables to store the hardware components of the PC.
private String ram; // Stores RAM (Random Access Memory).
private String hdd; // Stores HDD (Hard Disk Drive).
private String cpu; // Stores CPU (Central Processing Unit).
// This is a constructor for the PC class that initializes its hardware components.
public PC(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
// These methods override the abstract methods from the Computer class to provide specific information about the PC's hardware components.
@Override
public String getRAM() {
return this.ram; // Returns the RAM of the PC.
}
@Override
public String getHDD() {
return this.hdd; // Returns the HDD of the PC.
}
@Override
public String getCPU() {
return this.cpu; // Returns the CPU of the PC.
}
}
// This is a concrete class named Server that extends the Computer abstract class and provides specific implementations for hardware components.
public class Server extends Computer {
// These are private instance variables to store the hardware components of the server.
private String ram; // Stores RAM (Random Access Memory).
private String hdd; // Stores HDD (Hard Disk Drive).
private String cpu; // Stores CPU (Central Processing Unit).
// This is a constructor for the Server class that initializes its hardware components.
public Server(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
// These methods override the abstract methods from the Computer class to provide specific information about the server's hardware components.
@Override
public String getRAM() {
return this.ram; // Returns the RAM of the server.
}
@Override
public String getHDD() {
return this.hdd; // Returns the HDD of the server.
}
@Override
public String getCPU() {
return this.cpu; // Returns the CPU of the server.
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the package "com.journaldev.design.abstractfactory."
package com.journaldev.design.abstractfactory;
// Import statement to include the Computer interface.
import com.journaldev.design.model.Computer;
// This is an interface named ComputerAbstractFactory, which defines a contract for creating instances of Computer objects.
public interface ComputerAbstractFactory {
// This method is used to create a Computer object, but the specific implementation is left to the implementing classes.
public Computer createComputer();
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class PCFactory, which is in the "com.journaldev.design.abstractfactory" package.
package com.journaldev.design.abstractfactory;
// Import statements to include necessary classes.
import com.journaldev.design.model.Computer;
import com.journaldev.design.model.PC;
// This is a concrete factory class named PCFactory that implements the ComputerAbstractFactory interface.
public class PCFactory implements ComputerAbstractFactory {
// These are private instance variables to store the hardware components of the PC.
private String ram; // Stores RAM (Random Access Memory).
private String hdd; // Stores HDD (Hard Disk Drive).
private String cpu; // Stores CPU (Central Processing Unit).
// This is a constructor for the PCFactory class that initializes its hardware components.
public PCFactory(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
// This method implements the createComputer() method defined in the ComputerAbstractFactory interface.
// It creates and returns an instance of the PC class with the specified hardware components.
@Override
public Computer createComputer() {
return new PC(ram, hdd, cpu);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class ServerFactory, which is in the "com.journaldev.design.abstractfactory" package.
package com.journaldev.design.abstractfactory;
// Import statements to include necessary classes.
import com.journaldev.design.model.Computer;
import com.journaldev.design.model.Server;
// This is a concrete factory class named ServerFactory that implements the ComputerAbstractFactory interface.
public class ServerFactory implements ComputerAbstractFactory {
// These are private instance variables to store the hardware components of the server.
private String ram; // Stores RAM (Random Access Memory).
private String hdd; // Stores HDD (Hard Disk Drive).
private String cpu; // Stores CPU (Central Processing Unit).
// This is a constructor for the ServerFactory class that initializes its hardware components.
public ServerFactory(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
// This method implements the createComputer() method defined in the ComputerAbstractFactory interface.
// It creates and returns an instance of the Server class with the specified hardware components.
@Override
public Computer createComputer() {
return new Server(ram, hdd, cpu);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class ComputerFactory, which is in the "com.journaldev.design.abstractfactory" package.
package com.journaldev.design.abstractfactory;
// Import statements to include necessary classes.
import com.journaldev.design.model.Computer;
// This is a factory class named ComputerFactory.
public class ComputerFactory {
// This is a static method that takes a ComputerAbstractFactory as a parameter and uses it to create a Computer object.
// The specific implementation of the Computer object is determined by the provided factory.
public static Computer getComputer(ComputerAbstractFactory factory) {
return factory.createComputer();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
// This is a Java package declaration for the class TestDesignPatterns, which is in the "com.journaldev.design.test" package.
package com.journaldev.design.test;
// Import statements to include necessary classes.
import com.journaldev.design.abstractfactory.PCFactory;
import com.journaldev.design.abstractfactory.ServerFactory;
import com.journaldev.design.factory.ComputerFactory;
import com.journaldev.design.model.Computer;
// This is a class named TestDesignPatterns that contains a main method to demonstrate the use of design patterns.
public class TestDesignPatterns {
// The main method is the entry point of the program.
public static void main(String[] args) {
// Call the testAbstractFactory method to demonstrate the Abstract Factory design pattern.
testAbstractFactory();
}
// This method demonstrates the Abstract Factory design pattern.
private static void testAbstractFactory() {
// Create a PC instance using the Abstract Factory and provide the hardware components via the PCFactory.
Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB", "500 GB", "2.4 GHz"));
// Create a Server instance using the Abstract Factory and provide the hardware components via the ServerFactory.
Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB", "1 TB", "2.9 GHz"));
// Print the configuration of the PC and Server using the toString() method of the Computer class.
System.out.println("AbstractFactory PC Config::" + pc);
System.out.println("AbstractFactory Server Config::" + server);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------