编程知识 cdmana.com

Java design pattern Factory Method Pattern

A detailed explanation of the factory method model

   Today we mainly study Java In the design pattern Factory method model , If you read what I wrote before Simple factory model It will be easier to understand the following explanation , In order to understand the factory approach model, I still use the food processing factory as an example .

One , summary

   The factory method pattern removes the static properties of factory methods in the simple factory pattern , So that it can be inherited by subclasses . In this way, the pressure on the factory approach in the simple factory model can be different from that in the factory approach model    To share .

   The factory method pattern uses multiple subclasses inherited from the abstract factory role instead of Simple factory model Medium “ Top class ”. This will share the pressure of object creation ; Avoid using logical judgment in factory classes to decide to create objects ,   And it makes the structure quite flexible when new products come into being , Just follow the abstract product role 、 Abstract rules provided by factory roles to generate , Then it can be used by customers , You don't have to modify anything you already have    Code , It can be seen that the factory method pattern is in line with Opening and closing principle .

Two , chart

  • AbstractProduct class : Abstract product class , The parent class of all objects created by the factory method pattern , It is responsible for describing the common interface Shared by all instances
  • Berr Classes and Drinks class : Specific products , Is the goal of creating a simple factory pattern
  • AbstractFactory class : Abstract factory class , This is the core of the factory approach pattern , It is the interface that the specific factory role must implement or the parent class that must inherit . stay java It is implemented by an abstract class or interface
  • BerrFactory Classes and DrinksFactory class : Specific factory , It contains code related to specific business logic . Called by the application to create the corresponding product specific object

  3、 ... and , Illustrate with examples

   Let's take food processing plants as an example , Suppose there's a food processing plant , It mainly processes beer drink and mineral water , According to the demand , We need to use a separate class to specifically produce Beer, drink, mineral water , Because of me    Our factory will be bigger and stronger later , So we have to consider the impact of this design on subsequent business development , Our factory requires that the design should not affect the existing business on the basis of , Flexible expansion of the new system    Manufacturing business , According to the needs of food processing plants , We used Factory approach design pattern .

  Four , Specific code implementation

   First step : Designing our products is what we want to produce , According to the structure, we design the code as follows

   Abstract product class code is the parent class of all products , The code is as follows :

package pattern.factory.product;
/**
 *  Creating product abstract classes can also be defined with interfaces 
 * @author ningbeibei
 *
 */
public abstract class AbstractProduct {
    // Get product abstraction methods , You need a concrete implementation class to implement 
    public abstract void getFood();
}

   Specific products Berr( beer ), The code is as follows :

package pattern.factory.product;

/**
 *  Beer the specific class of beer is inherited from AbstractProduct class 
 * @author ningbeibei
 */
public class Berr extends AbstractProduct {
    @Override
    public void getFood() {
        System.out.println(" beer ");
    };
}

   Specific products Drinks ( drinks ), The code is as follows :

package pattern.factory.product;

/**
 *  The specific beverage class is inherited from AbstractProduct
 * @author ningbeibei
 */
public class Drinks extends AbstractProduct {

    @Override
    public void getFood() {
        System.out.println(" drinks ");
    }
}

   The second step : Factories that design and manufacture products , This is also the most important part of the factory method model

   Create factory abstract classes , The code is as follows :

package pattern.factory.factorymill;
import pattern.factory.product.AbstractProduct;
/**
 *  Abstract factory class , Food processing plants need abstract design 
 * @author ningbeibei
 *
 */
public abstract class AbstractFactory {
    // This abstract method returns a product object ,
    public abstract AbstractProduct createProduct(); 
}

   Create a specific brewery class BerrFactory, The code is as follows :

package pattern.factory.factorymill;

import pattern.factory.product.AbstractProduct;
import pattern.factory.product.Berr;
/**
 *  Factories that know beer , This class is implemented from  AbstractFactory abstract class 
 *  Returns a beer product object 
 * @author ningbeibei
 *
 */
public class BerrFactory extends AbstractFactory {
    
    // Return beer to abstract factory 
    @Override
    public AbstractProduct createProduct() {
        return new Berr();
    }
}

   Create a specific beverage manufacturing factory class , The code is as follows :

package pattern.factory.factorymill;

import pattern.factory.product.AbstractProduct;
import pattern.factory.product.Drinks;

/**
 *  A beverage factory , Inherited from AbstractFactory abstract class 
 * @author ningbeibei
 */
public class DrinksFactory extends AbstractFactory {

    // Return to the beverage object 
    @Override
    public AbstractProduct createProduct() {
        return new Drinks();
    }
}

   The third step : Write test class for test verification

 

package pattern.factory;

import pattern.factory.factorymill.BerrFactory;
import pattern.factory.factorymill.DrinksFactory;
import pattern.factory.product.AbstractProduct;
/**
 *  Factory method pattern test class 
 * @author ningbeibei
 */
public class test {

    public static void main(String[] args) {
        // Start with a brewery 
        BerrFactory berrfactory = new BerrFactory();
        // Call the brewery createProduct() Method , Create a beer object and return to 
        AbstractProduct product = berrfactory.createProduct();
        // Export beer 
        product.getFood();
        
        // Start with a beverage factory 
        DrinksFactory drinksfactory = new DrinksFactory();
        // Call the beverage factory createProduct() Method , Create a drink object and return to 
        AbstractProduct drinksproduct = drinksfactory.createProduct();
        // Export drinks 
        drinksproduct.getFood();
    }
}

 

   test result :

5、 ... and , How to expand new products

   With the expansion of food processing , Our food factory wants to make more products , Now I want to make mineral water , According to the idea at the beginning of the design , No matter what kind of product will be expanded later, the existing code will not be shaken , That is not against the factory model Opening and closing principle .

  1. Add new product mineral water class and inherit abstract product class , The code is as follows :

package pattern.factory.product;

/**
 *  New mineral water products , Also inherited from the product abstract class AbstractProduct
 * @author ningbeibei
 *
 */
public class Water extends AbstractProduct {

    @Override
    public void getFood() {
        System.out.println(" Mineral water ");
    }
}

  2. Add concrete manufacturing mineral water factory class and inherit abstract class AbstractFactory, The code is as follows :

 

package pattern.factory.factorymill;

import pattern.factory.product.AbstractProduct;
import pattern.factory.product.Water;
/**
 *  New mineral water factories 
 *  Inherited from AbstractFactory abstract class 
 * @author ningbeibei
 *
 */
public class WaterFactory extends AbstractFactory{

    // Make mineral water objects and return to 
    @Override
    public AbstractProduct createProduct() {
        return new Water();
    }
}

 

  3. Test class writing , The code is as follows :

package pattern.factory;

import pattern.factory.factorymill.BerrFactory;
import pattern.factory.factorymill.DrinksFactory;
import pattern.factory.factorymill.WaterFactory;
import pattern.factory.product.AbstractProduct;
/**
 *  Factory method pattern test class 
 * @author ningbeibei
 */
public class test {

    public static void main(String[] args) {
        // Start with a brewery 
        BerrFactory berrfactory = new BerrFactory();
        // Call the brewery createProduct() Method , Create a beer object and return to 
        AbstractProduct product = berrfactory.createProduct();
        // Export beer 
        product.getFood();
        
        // Start with a beverage factory 
        DrinksFactory drinksfactory = new DrinksFactory();
        // Call the beverage factory createProduct() Method , Create a drink object and return to 
        AbstractProduct drinksproduct = drinksfactory.createProduct();
        // Export drinks 
        drinksproduct.getFood();
        
        // This is a new product mineral water 
        WaterFactory waterfactory = new WaterFactory();
        // Call in the mineral water plant createProduct() Method , Create a mineral water object and return to 
        AbstractProduct waterproduct = waterfactory.createProduct();
        // Export mineral water 
        waterproduct.getFood();
    }
}

  4. test result :

 

 

  6、 ... and , The advantages and disadvantages of the factory approach model

   shortcoming :

  • It's easy to have too many classes , Increase complexity
  • Increases the abstractness of the system and the difficulty of understanding
  • Abstract products can only produce one product , This disadvantage can be used Abstract factory pattern solve   

  advantage :

  • Users only need to know the name of the specific factory to get the products they want , No need to know the specific creation process of the product
  • Increased flexibility , For the creation of new products , Just write one more factory class
  • A typical decoupling framework . High level modules only need to know the abstract class of the product , Don't care about other implementation classes , Satisfy the Dimitar rule 、 Rely on the inversion principle and the Ritz substitution principle

 

7、 ... and , Learning ideas

   First Simple factory model ===》 Then Factory method model ====》 Last Abstract factory pattern

   From the shallow to the deep learning in order to understand the deeper and more comprehensive, and then can be integrated

   I hope the criticism can correct the shortcomings of the writing , I will change it

 

版权声明
本文为[Swallow finch knows donkey's ambition]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225105853101f.html

Scroll to Top