Factory Design Patterns for Java Design Patterns

Keywords: Programming

Plant Design Mode

Interface-oriented programming reduces coupling between classes and Applications

Three forms of factory design patterns:

1. Static Factory

There is a static method in the factory that determines the type of "manufacturing" based on the parameters and transfers the responsibility for creating objects to the factory class.

Product Interface:

public interface Product {
    public void create();
}

Specific product classes that implement interfaces:

public class Washer implements Product{

    @Override
    public void create() {
        System.out.println("The washing machine was made");
    }
}
public class Computer implements Product{
    @Override
    public void create() {
        System.out.println("The computer was made");
    }
}

Product factory, which product is produced according to the parameters:

public class ProductFactory {
    public static Product createProduct(String productName) throws Exception {
        if(productName.equals("Washing machine")){
            return new Washer();
        }else if(productName.equals("Computer")){
            return new Computer();
        }else {
            throw new Exception("No such product");
        }
    }
}

Start production:

public static void main(String[] args){
        try {
            Product washer = ProductFactory.createProduct("Washing machine");
            washer.create();
            Product computer = ProductFactory.createProduct("Computer");
            computer.create();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

The core of the code above is the ProductFactory factory class, which manufactures products by judging the parameters passed into the static method and gives it the name of the product to get the product we want.

High scalability, if you want to add products, just extend the factory class.For callers, the implementation details of the product class are blocked and only the interface of the product is concerned.

Disadvantages and limitations: The entire system relies on factory classes, and the entire system will not work if one of the factory class's methods fails.

To add a product, you must modify the factory class in addition to adding a product class.When there are many products, and there is complex hierarchical logic between products, this factory class must have complex logical judgment, which is not conducive to the maintenance of the system.

2. Factory method

The factory method defines the factory interface interface for the factory class, which weakens the functions of the factory class through polymorphism.

Factory Interface:

public interface Factory {
    public Product create();
}

Product Interface:

public interface Product {
    public void create();
}

Product implementation class:

public class Washer implements Product{

    @Override
    public void create() {
        System.out.println("The washing machine was made");
    }
}
public class Computer implements Product{
    @Override
    public void create() {
        System.out.println("The computer was made");
    }
}

Multiple factory classes are implemented through the factory interface:

public class WasherFactory implements Factory{
    @Override
    public Product create() {
        return new Washer();
    }
}

public class ComputerFactory implements Factory{
    @Override
    public Product create() {
        return new Computer();
    }
}

Differences between static and factory methods are found by comparison:

Static factories place the functions of producing all products in a class and use static methods to determine which products to produce.The factory method is to put different products in different factories to produce. Each factory has a clear division of labor, and even if one factory has problems, the other factories will not be affected.

In the future, to add new products, you only need to add a factory class without modifying the code, which will not affect other factories.

Limitations of factory method: When there are many types of each product, factory method should create a factory class for each type of product. With the increase of product types and product models, there are more product classes and factory classes, which are not conducive to maintenance.

3. Abstract Factory

All products of the factory method implement the same product interface without the concept of classification.

Abstract factories can be understood as classified management based on the factory method.

Washing machine model:'washing machine-A','washing machine-B', computer model:'computer-A','computer-B'

Product section:

Product Interface:

public interface Computer {
    public void create();
}
public interface Washer {
    public void create();
}

Product implementation class:

public class WasherA implements Washer{
    @Override
    public void create() {
        System.out.println("Washing machine-A Made");
    }
}
public class WasherB implements Washer{
    @Override
    public void create() {
        System.out.println("Washing machine-B Made");
    }
}
public class ComputerA implements Computer{
    @Override
    public void create() {
        System.out.println("Computer-A Made");
    }
}
public class ComputerB implements Computer{
    @Override
    public void create() {
        System.out.println("Computer-B Made");
    }
}

Factory Interface:

public interface Factory {
    public Washer createWasher();
    public Computer createComputer();
}

Factory implementation class:

public class FactoryA implements Factory{

    @Override
    public Washer createWasher() {
        return new WasherA();
    }
    @Override
    public Computer createComputer() {
        return new ComputerA();
    }
}
public class FactoryB implements Factory{
    @Override
    public Washer createWasher() {
        return new WasherB();
    }

    @Override
    public Computer createComputer() {
        return new ComputerB();
    }
}

When there are too many models of a product, it can be seen that abstract factories achieve product classification and divide different factories according to different models.

 

 

Posted by spider22 on Fri, 19 Jun 2020 19:06:27 -0700