Design Patterns--Factory Patterns

Posted by raimis100 on Fri, 09 Aug 2019 09:25:30 +0200

Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern belongs to the creation pattern, which provides the best way to create objects.
In factory mode, we do not expose the creation logic to the client when creating objects, and point to newly created objects by using a common interface.

I. Simple Factory Model
Simple Factory Definition: Provides a function for creating object instances without concern for their implementation. The type of instance created can be an interface, an abstract class, or a concrete class.

Implementing automobile interface:

public interface Car {
    String getName();
}

Mercedes-Benz:

public class Benz implements Car{

    @Override
    public String getName() {
        return "Benz";
    }
}

BMW:

public class BMW implements Car {
    @Override
    public String getName() {
        return "BMW";
    }
}

Simple factory class:

public class SimpleFactory {
     public Car getCar(String name){
         if (name.equals("Benz"))
             return new Benz();
         if (name.equals("BMW"))
             return new BMW();
         else {
             System.out.println("This car can't be produced.");
             return null;
         }
     }
}

Test methods:

public static void main(String[] args) {
        SimpleFactory simpleFactory=new SimpleFactory();
        Car car=simpleFactory.getCar("Benz");
        System.out.println(car.getName());
    }
---------------------------------------------
Benz

The disadvantage of this method is also obvious, which violates the open-close principle of design pattern, because if you want to add classes that factory can initialize, you have to rebuild the factory.

II: Factory Method Model
It is to design the interface of a factory. If you want anything, you can write a class that inherits from the factory, so you don't need to modify anything, just add it directly. This is equivalent to my factory is used to produce shoes, and what brand of shoes specific to each workshop, if a new brand of shoes, directly add a new workshop.

Factory Interface

public interface Factory {
    Car getCar();
}

Mercedes-Benz Factory

public class BenzFactory implements Factory {
    @Override
    public Car getCar() {
        return new Benz();
    }
}

BMW Factory

public class BMWFactory implements Factory {
    @Override
    public Car getCar() {
        return new BMW();
    }
}

Test class

public class Test {
    public static void main(String[] args) {
        Factory factory=new BMWFactory();
        System.out.println(factory.getCar().getName());
        Factory factory1=new BenzFactory();
        System.out.println(factory1.getCar().getName());
    }
}

-----------------------------------------------
BMW
Benz

So the question arises again. What if you want to make bicycles?

Three: abstract factory model
Definition of abstract factory pattern: Provides an interface for creating a set of related or interdependent objects without specifying their specific classes. Here the interface class of abstract factory is able to create multiple related objects, while the interface class of factory method is to create only one object.

Production Benz interface

public interface Benz {
    void carColor();//Setting colors
    void carSpeed();//set speed
    void carPrice();//Setting Prices

}

BanzFactory, an abstract interface class for Mercedes-Benz factories

public interface BanzFactory {
    /**
     * The Way to Create Mercedes-Benz
     */
      Benz createCar();
}

Specific Factory Classes for Production of Mercedes-Benz Vehicle S450

/**
 * The actual factory that produces a certain type of Mercedes-Benz vehicle
 */
public class S450Factory implements BanzFactory {
    @Override
    public Benz createCar() {
        return new BenzS450();
    }

Specific Categories for Production of Mercedes-Benz Vehicle S450

/**
 * Specific categories of production of Mercedes-Benz S450
 */
public class BenzS450 implements Benz {
    /**
     * Constructor, which sets basic properties when created
     */
    public BenzS450() {
        carColor();
        carPrice();
        carSpeed();
    }

    @Override
    public void carColor() {
        System.out.println("Benz S450 The color is silver.");

    }

    @Override
    public void carSpeed() {
        System.out.println("Benz S450 The speed is 200 kilometers per hour.");
    }

    @Override
    public void carPrice() {
        System.out.println("Benz S450 The price is one million.");
    }
}

Test class

/**
 * Abstract factory pattern invocation
 * Production of Mercedes-Benz S450
 */
public class AbstractFactoryDemo {
    public static void main(String[] a) {
        System.out.println("Production of Mercedes-Benz S450");
        //Instances of parent class but object subclass
        BanzFactory banzFactory = new S450Factory();//Create a S450 Factory
        //Calling methods of parent classes is a manifestation of java polymorphism
        banzFactory.createCar();//S450 Factory Produces Vehicle S450
     
    }

}

1. Advantages
To create a new production line for the same group of products, it is only necessary to implement the abstract factory interface of that group of products to create a new factory class.

2. Disadvantages
The biggest disadvantage of the abstract method pattern is that the extension of the product family itself is very difficult. If a new product type is added to the product family, multiple interfaces need to be modified to affect existing factory classes.
For example, if you want to create three objects in this factory, which was originally just two objects, then you need to add a method to create objects in the abstract method. Then all the classes that implement this interface need to be re-added. This method of creating objects is the reason for the impact on previous factories.

Topics: Programming Java