Design patterns Best practices factory patterns

Design pattern.jpg

What

Factory pattern: Is a creative design pattern that creates an interface for objects and lets subclasses decide which instance to new

Usage Scenarios:

  • Consider using engineering patterns anywhere you need to generate complex objects that need to be generated
  • The factory pattern is suitable for complex objects, but not for objects that can be created with new

UML diagrams

Factory pattern UML.png

How

The code example

  • Start by defining the factory abstract class

    • Factory

      public abstract class Factory{
        /** * Abstract factory method * What product is produced is determined by subclass * return product */
        public abstract Product createProduct(Class<T> clz);
      }Copy the code
  • Define factory implementation classes to produce products based on the incoming factory implementation type

    • Use a reflective approach to reduce redundant factory implementation classes
    • FactoryImp

      public class FactoryImp extends Factory{
        /** * Specific factory * returns the corresponding product based on the product type passed in * return product */
        public <T extends Product> createProduct(Class<T> clz){
            Product p = null;
            try{
                p = (Product)class.forName(clz.getName()).newInstance(a);
            }catch(Execption e){
            }
            return p;
        };
      }Copy the code
  • Define product abstract classes

    • Product

      public abstract class Product{
        public abstract void method(a);
      }Copy the code
  • Defining Product A

    • ProductA

      public class ProductA extends Product{
        public void method(a){
            System.out.println("I am a product A!!"); }}Copy the code
  • Defining Product B

    • ProductB

      public class ProductB extends Product{
        public void method(a){
            System.out.println("I am a product B!!"); }}Copy the code
  • Test examples:

      public static void main(String[] args) {
              Factory factory = new FactoryImp();
              / / in the production of ProductA
              Product pA = factory.createProduct(ProductA.class);
              pA.method();
    
              / / ProductB production
              Product pB = factory.createProduct(ProductB.class);
              pB.method();
          }Copy the code
  • Results:

      I am a product A!!
      I am a product B!!Copy the code

conclusion

  • advantages
    • As can be seen from the above example, the factory pattern is a good design pattern, which can well decouple the relationship between the factory and the product, specify the specification through the interface/abstract class, and just replace the corresponding implementation to complete the replacement, which well conforms to the open and closed principle of the design pattern principle, open for expansion, closed for modification
    • To reduceif... elaseForm of hard-coded judgment, logic more clear and concise
  • disadvantages
    • There will be too many product classes, which will complicate the class structure. For every new product, a new product class needs to be written

If you have any suggestions on my article, you can leave a message to me, I will reply, and humbly accept, only to give the best to the best lovely you ~