Skip to content

Commit a179259

Browse files
committed
factory method
1 parent a41acdd commit a179259

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

content/posts/java-design-pattern.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ ShowToc: true # Determines whether to display the Table of Contents (TOC) for
1111
TocOpen: true # Controls whether the TOC is expanded when the post is loaded.
1212
weight: 11 # The order in which the post appears in a list of posts. Lower numbers make the post appear earlier.
1313
---
14-
Explain how to use the design patterns.
14+
Explain how to use the design patterns. (Creational Patterns)
1515
[Refer1](https://github.com/kamranahmedse/design-patterns-for-humans?tab=readme-ov-file#-simple-factory)
1616
[Refer2](https://java-design-patterns.com/)
17-
[Refer3](https://www.tutorialspoint.com/design_pattern/factory_pattern.htm)<br>
17+
[Refer3](https://www.tutorialspoint.com/design_pattern/factory_pattern.htm)
18+
[Refer4](https://www.oodesign.com/factory-pattern)<br>
1819
## 1. 🏠 Simple Factory
1920
- Factory is an object for creating other objects
2021
- **Use cases:**
@@ -120,3 +121,118 @@ public class MainTestFactory {
120121

121122

122123
```
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+
- **Use cases:**
127+
- when class cannot anticipate the class of objects it must create.
128+
- When class wants its subclasses to specify the objects it creates.
129+
- when designing frameworks or libraries to give the best flexibility and isolation from concrete class types
130+
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.
132+
- Design UML:
133+
![image](/images/factory_method_pattern.png)
134+
135+
- Implementation codes:
136+
137+
```java
138+
package design.patterns.factory.method;
139+
140+
public interface IProduct {
141+
public String getType();
142+
}
143+
144+
package design.patterns.factory.method;
145+
146+
public class ProductA implements IProduct {
147+
private static String TYPE_NAME = "ProductA";
148+
@Override
149+
public String getType() {
150+
return TYPE_NAME;
151+
}
152+
153+
}
154+
package design.patterns.factory.method;
155+
156+
public class ProductB implements IProduct {
157+
private static String TYPE_NAME = "ProductB";
158+
@Override
159+
public String getType() {
160+
return TYPE_NAME;
161+
}
162+
}
163+
164+
package design.patterns.factory.method;
165+
166+
public class ProductC implements IProduct {
167+
private static String TYPE_NAME = "ProductC";
168+
@Override
169+
public String getType() {
170+
return TYPE_NAME;
171+
}
172+
173+
}
174+
175+
package design.patterns.factory.method;
176+
177+
public abstract class AbstractFactory {
178+
public abstract IProduct createProduct();
179+
180+
public void doSomething() {
181+
IProduct product = createProduct();
182+
System.out.println("Do something with" + product.getType());
183+
}
184+
}
185+
186+
package design.patterns.factory.method;
187+
188+
public class FactoryA extends AbstractFactory {
189+
190+
@Override
191+
public IProduct createProduct() {
192+
// TODO Auto-generated method stub
193+
return new ProductA();
194+
}
195+
196+
}
197+
198+
package design.patterns.factory.method;
199+
200+
public class FactoryB extends AbstractFactory {
201+
202+
@Override
203+
public IProduct createProduct() {
204+
// TODO Auto-generated method stub
205+
return new ProductB();
206+
}
207+
}
208+
209+
package design.patterns.factory.method;
210+
211+
public class FactoryC extends AbstractFactory {
212+
213+
@Override
214+
public IProduct createProduct() {
215+
// TODO Auto-generated method stub
216+
return new ProductC();
217+
}
218+
}
219+
220+
package design.patterns.factory.method;
221+
222+
public class MainTestFactory {
223+
224+
public static void main(String[] args) {
225+
AbstractFactory factoryA = new FactoryA();
226+
factoryA.doSomething();
227+
228+
AbstractFactory factoryB = new FactoryB();
229+
factoryB.doSomething();
230+
231+
AbstractFactory factoryC = new FactoryC();
232+
factoryC.doSomething();
233+
234+
}
235+
236+
}
237+
238+
```
82.3 KB
Loading

0 commit comments

Comments
 (0)