|
| 1 | +--- |
| 2 | +author: "Phong Nguyen" |
| 3 | +title: "Design Patterns" |
| 4 | +date: "2025-02-11" |
| 5 | +description: "Design Patterns." |
| 6 | +tags: ["java"] #tags search |
| 7 | +FAcategories: ["syntax"] #The category of the post, similar to tags but usually for broader classification. |
| 8 | +FAseries: ["Themes Guide"] #indicates that this post is part of a series of related posts |
| 9 | +aliases: ["migrate-from-jekyl"] #Alternative URLs or paths that can be used to access this post, useful for redirects from old posts or similar content. |
| 10 | +ShowToc: true # Determines whether to display the Table of Contents (TOC) for the post. |
| 11 | +TocOpen: true # Controls whether the TOC is expanded when the post is loaded. |
| 12 | +weight: 11 # The order in which the post appears in a list of posts. Lower numbers make the post appear earlier. |
| 13 | +--- |
| 14 | +Explain how to use the design patterns. |
| 15 | +[Refer1](https://github.com/kamranahmedse/design-patterns-for-humans?tab=readme-ov-file#-simple-factory) |
| 16 | +[Refer2](https://java-design-patterns.com/) |
| 17 | +[Refer3](https://www.tutorialspoint.com/design_pattern/factory_pattern.htm)<br> |
| 18 | +## 1. 🏠 Simple Factory |
| 19 | +- Factory is an object for creating other objects |
| 20 | +- **Use cases:** |
| 21 | + - when the class does not know beforehand the exact types and dependencies of the objects it needs to create. |
| 22 | + - 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. |
| 23 | + - when designing frameworks or libraries to give the best flexibility and isolation from concrete class types |
| 24 | + |
| 25 | +- **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. |
| 26 | + - Design UML: |
| 27 | + |
| 28 | + |
| 29 | + - Implementation codes: |
| 30 | + |
| 31 | +```java |
| 32 | +package design.patterns.factory; |
| 33 | + |
| 34 | +public interface IProduct { |
| 35 | + public void getType(); |
| 36 | +} |
| 37 | + |
| 38 | +package design.patterns.factory; |
| 39 | + |
| 40 | +public class ProductA implements IProduct { |
| 41 | + private static String TYPE_NAME = "ProductA"; |
| 42 | + @Override |
| 43 | + public void getType() { |
| 44 | + System.err.println(TYPE_NAME); |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +package design.patterns.factory; |
| 50 | + |
| 51 | +public class ProductB implements IProduct { |
| 52 | + private static String TYPE_NAME = "ProductB"; |
| 53 | + @Override |
| 54 | + public void getType() { |
| 55 | + System.err.println(TYPE_NAME); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +package design.patterns.factory; |
| 60 | + |
| 61 | +public class ProductC implements IProduct { |
| 62 | + private static String TYPE_NAME = "ProductC"; |
| 63 | + @Override |
| 64 | + public void getType() { |
| 65 | + System.err.println(TYPE_NAME); |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +package design.patterns.factory; |
| 71 | + |
| 72 | +public enum ProductType { |
| 73 | + TYPEA, |
| 74 | + TYPEB, |
| 75 | + TYPEC; |
| 76 | +} |
| 77 | + |
| 78 | +package design.patterns.factory; |
| 79 | + |
| 80 | +public class ProductFactory { |
| 81 | + public static IProduct createProduct(ProductType type) { |
| 82 | + if (type == null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + switch (type) { |
| 87 | + case TYPEA: { |
| 88 | + return new ProductA(); |
| 89 | + } |
| 90 | + case TYPEB: { |
| 91 | + return new ProductB(); |
| 92 | + } |
| 93 | + case TYPEC: { |
| 94 | + return new ProductC(); |
| 95 | + } |
| 96 | + } |
| 97 | + return null; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +package design.patterns.factory; |
| 102 | + |
| 103 | +public class MainTestFactory { |
| 104 | + |
| 105 | + public static void main(String[] args) { |
| 106 | + ProductFactory productFactory = new ProductFactory(); |
| 107 | + |
| 108 | + IProduct product1 = productFactory.createProduct(ProductType.TYPEA); |
| 109 | + product1.getType(); |
| 110 | + |
| 111 | + IProduct product2 = productFactory.createProduct(ProductType.TYPEB); |
| 112 | + product2.getType(); |
| 113 | + |
| 114 | + IProduct product3 = productFactory.createProduct(ProductType.TYPEC); |
| 115 | + product3.getType(); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +``` |
0 commit comments