Skip to content

Commit fcfa11a

Browse files
committed
added factory abstract
1 parent 83ed448 commit fcfa11a

File tree

2 files changed

+165
-6
lines changed

2 files changed

+165
-6
lines changed

content/posts/java-design-pattern.md

Lines changed: 165 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ Explain how to use the design patterns. (Creational Patterns)
1616
[Refer2](https://java-design-patterns.com/)
1717
[Refer3](https://www.tutorialspoint.com/design_pattern/factory_pattern.htm)
1818
[Refer4](https://www.oodesign.com/factory-pattern)<br>
19-
## 1. 🏠 Simple Factory
19+
20+
## 1. Creational Design Patterns.
21+
## 1.1 🏠 Simple Factory
2022
- Factory is an object for creating other objects
2123
- **Use cases:**
2224
- when the class does not know beforehand the exact types and dependencies of the objects it needs to create.
2325
- When a method returns one of several possible classes that share a common super class and wants to encapsulate the logic of which object to create.
2426
- when designing frameworks or libraries to give the best flexibility and isolation from concrete class types
2527

26-
- **Examples:** You want to manufacture the products. You must be able to create both A and B (C,D) products and switch between them without modify the existing source codes.
28+
- **Examples:** You want to manufacture the products many times.
2729
- Design UML:
2830
![image](/images/factory_pattern.png)
2931

@@ -121,14 +123,16 @@ public class MainTestFactory {
121123

122124

123125
```
124-
## 2. 🏭 Factory Method
125-
- Factory method provides a way to delegate the instantiation logic to child classes.Defines an interface for creating objects, but let subclasses to decide which class to instantiate. Refers to the newly created object through a common interface
126+
<br>
127+
128+
## 1.2. 🏭 Factory Method
129+
- **Factory Method** is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
126130
- **Use cases:**
127131
- when class cannot anticipate the class of objects it must create.
128-
- When class wants its subclasses to specify the objects it creates.
132+
- When class **wants its subclasses** to **specify the objects it creates**.
129133
- when designing frameworks or libraries to give the best flexibility and isolation from concrete class types
130134

131-
- **Examples:** You want to manufacture the products. You must be able to create both A and B (C,D) products and switch between them without modify the existing source codes.
135+
- **Examples:** You want to manufacture the products. You must be able to create both A and B (C,D) products and add/switch between them without modify the existing source codes.
132136
- Design UML:
133137
![image](/images/factory_method_pattern.png)
134138

@@ -233,4 +237,159 @@ public class MainTestFactory {
233237

234238
}
235239

240+
```
241+
242+
<br>
243+
244+
## 1.3. 🔨 Abstract Factory
245+
- **Abstract Factory** is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
246+
- **Use cases:**
247+
- when need a way to **create individual furniture objects** so that they **match other objects of the same family**.
248+
- When don’t want to change existing code when adding new products or families of products to the program.
249+
250+
- **Examples:** You intend to manufacture a product line that included a family of related products: A,B,and C - along with several variants such as AWin,ALinux, BWin,BLinux, CWin, and CLinux.
251+
- Design UML:
252+
![image](/images/factory_abstract_pattern.png)
253+
254+
- Implementation codes:
255+
256+
```java
257+
package design.patterns.factoryabstract;
258+
259+
public interface IProductA {
260+
public String getType();
261+
}
262+
263+
package design.patterns.factoryabstract;
264+
265+
public interface IProductB {
266+
public String getType();
267+
}
268+
269+
package design.patterns.factoryabstract;
270+
271+
public class ProductALinux implements IProductA {
272+
273+
private static final String TYPE = "ProductALinux";
274+
275+
@Override
276+
public String getType() {
277+
return TYPE;
278+
}
279+
280+
}
281+
282+
package design.patterns.factoryabstract;
283+
284+
public class ProductAWin implements IProductA {
285+
286+
private static final String TYPE = "ProductAWin";
287+
288+
@Override
289+
public String getType() {
290+
return TYPE;
291+
}
292+
}
293+
294+
package design.patterns.factoryabstract;
295+
296+
public class ProductBLinux implements IProductB {
297+
298+
private static final String TYPE = "ProductBLinux";
299+
300+
@Override
301+
public String getType() {
302+
return TYPE;
303+
}
304+
}
305+
306+
package design.patterns.factoryabstract;
307+
308+
public class ProductBWin implements IProductB {
309+
310+
private static final String TYPE = "ProductBWin";
311+
312+
@Override
313+
public String getType() {
314+
return TYPE;
315+
}
316+
}
317+
318+
package design.patterns.factoryabstract;
319+
320+
public interface IFactoryProduct {
321+
public IProductA createProductA();
322+
323+
public IProductB createProductB();
324+
}
325+
326+
package design.patterns.factoryabstract;
327+
328+
public class FactoryProductLinux implements IFactoryProduct {
329+
330+
@Override
331+
public IProductA createProductA() {
332+
return new ProductALinux();
333+
}
334+
335+
@Override
336+
public IProductB createProductB() {
337+
return new ProductBLinux();
338+
}
339+
340+
}
341+
342+
package design.patterns.factoryabstract;
343+
344+
public class FactoryProductWin implements IFactoryProduct {
345+
346+
@Override
347+
public IProductA createProductA() {
348+
return new ProductAWin();
349+
}
350+
351+
@Override
352+
public IProductB createProductB() {
353+
return new ProductBWin();
354+
}
355+
356+
}
357+
358+
package design.patterns.factoryabstract;
359+
360+
public class UserApplication {
361+
private IFactoryProduct factory;
362+
private IProductA productA;
363+
private IProductB productB;
364+
365+
public UserApplication(IFactoryProduct factory) {
366+
this.factory = factory;
367+
}
368+
369+
public void createProducts() {
370+
productA = factory.createProductA();
371+
productB = factory.createProductB();
372+
}
373+
374+
public void someOperations() {
375+
System.err.println(
376+
"This is the User Application created with " + productA.getType() + " and " + productB.getType());
377+
}
378+
}
379+
380+
package design.patterns.factoryabstract;
381+
382+
public class Main {
383+
384+
public static void main(String[] args) {
385+
IFactoryProduct factory = new FactoryProductWin();
386+
// IFactoryProduct factory = new FactoryProductLinux();
387+
388+
UserApplication userApp = new UserApplication(factory);
389+
userApp.createProducts();
390+
userApp.someOperations();
391+
392+
}
393+
}
394+
236395
```
94.2 KB
Loading

0 commit comments

Comments
 (0)