“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

We used examples to explain the use of factory pattern and advantages and disadvantages, today we will look at the use of abstract factory pattern and advantages and disadvantages!

Abstract Factory Pattern

The abstract factory pattern creates other factories around a gigafactory. The gigafactory is also known as the factory of other factories. In the abstract Factory pattern, an interface is a factory responsible for creating a related object, without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern. Here’s another example.

Define entity classes

// Car seat
public class Sofa {
    / / color
	private String color;
    / / comfort
	private String soft;
	
	public String getColor(a) {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String getSoft(a) {
		return soft;
	}
	public void setSoft(String soft) {
		this.soft = soft; }}// Car wheels
public class Wheel {
	/ / radius
	private String radiu;
    / / size
	private String size;
    
	public String getRadiu(a) {
		return radiu;
	}
	public void setRadiu(String radiu) {
		this.radiu = radiu;
	}
	public String getSize(a) {
		return size;
	}
	public void setSize(String size) {
		this.size = size; }}Copy the code

Define two interfaces

// Car seat
public interface ISofa {
	// Change the seat color
	void changeColor(a);
	// Change seat comfort
	void changeSoft(a);
}

// Car wheels
public interface IWheel {
	// Change the size
	void changeSize(a);
	// Change the radius
	void changeRadius(a);
}
Copy the code

Define different entity classes to implement the two interfaces respectively

// A Honda seat
public class BenSofa implements ISofa {

	@Override
	public void changeColor(a) {
		System.out.println("Honda turned blue.");
	}

	@Override
	public void changeSoft(a) {
		System.out.println("Honda is getting comfortable."); }}// Toyota car seat
public class FengSofa implements ISofa{

	@Override
	public void changeColor(a) {
		System.out.println("The Toyota turned red.");
	}

	@Override
	public void changeSoft(a) {
		System.out.println("The Toyota is getting sick."); }}// Honda wheels
public class BenWheel implements IWheel{

	@Override
	public void changeSize(a) {
		System.out.println("Honda's wheels are getting bigger.");
	}

	@Override
	public void changeRadius(a) {
		System.out.println("Honda wheel radius has increased."); }}// Toyota wheels
public class FengWheel implements IWheel{
	
	@Override
	public void changeSize(a) {
		System.out.println("Toyota's wheels are getting smaller.");
	}

	@Override
	public void changeRadius(a) {
		System.out.println("The radius of the Toyota wheel is getting smaller."); }}Copy the code

Define the abstract factory class

// Auto factory
public interface CarFactory {
    // Get the seat object
	ISofa getSofa(a);
    // Get the wheel object
	IWheel getWheel(a);
}
Copy the code

Define the factory implementation class

// Honda
public class BenCarFactory implements CarFactory{

	@Override
	public ISofa getSofa(a) {
		return new BenSofa();
	}

	@Override
	public IWheel getWheel(a) {
		return newBenWheel(); }}// Toyota
public class FengCarFactory implements CarFactory{

	@Override
	public ISofa getSofa(a) {
		return new FengSofa();
	}

	@Override
	public IWheel getWheel(a) {
		return newFengWheel(); }}Copy the code

6. Client call

public class DemoTest {
	
	public static void main(String[] args) {
		CarFactory benCar = newBenCarFactory(); ISofa sofa = benCar.getSofa(); sofa.changeColor(); sofa.changeSoft(); }}Copy the code

Execution Result:

Advantage: the abstract factory pattern is the main benefit of the abstract factory pattern is easy to exchange products series, since the factory class in an application only need when initializing a, this makes change an application specific factory very easily, it only need to change specific plant can use different product configurations. No one’s design can completely prevent requirements from changing, or project maintenance, so our goal is to make changes as minimal and as easy as possible. Another benefit of the abstract factory pattern is that it separates the concrete instance creation process from the client side, which operates on the instance through their abstract interface. The concrete class names of the product implementation classes are separated by the concrete factory implementation classes and do not appear in the client code.

CarFactory benCar = new BenCarFactory(); BenCarFactory = new BenCarFactory(); Too complicated.

Good today first said that this, or that sentence, understand is not really understand, or it is necessary to use the hand knock on the code! If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!