Abstract factory pattern of common design patterns in Java

I. Overview

The abstract factory pattern is also a creational pattern in the design pattern, and is used for the construction of product families. The abstract factory pattern is the most general form of all forms of factory patterns. Abstract factory refers to a factory pattern used when there are multiple roles. The abstract factory pattern can provide an interface to the client, which allows the client to create product objects of multiple product families without having to specify the product.

2. Mode structure diagram

Insert picture description here

Three, Java code implementation

Let’s take fruit as an example. First, we have to create the fruit interface and the fruit factory interface separately.

package 抽象工厂模式;

public interface Fruit {
    
    
//	生产水果-接口
	public void get();
}
package 抽象工厂模式;

public interface FruitFactory {
    
    
	public Fruit getApple();
	public Fruit getBanana();
}

Create an abstract class for concrete fruits, and let the concrete fruit abstract class implement the fruit interface

package 抽象工厂模式;

public abstract class Apple implements Fruit{
    
    
//	苹果抽象类
	public abstract void get();
}
package 抽象工厂模式;

public abstract class Banana implements Fruit{
    
    
//	香蕉抽象类
	public abstract void get();
}

Create specific fruit categories, each fruit is divided into two levels, A and B.

package 抽象工厂模式;
//Apple_A 继承Apple
public class Apple_A extends Apple{
    
    
	@Override
	public void get() {
    
    
		System.out.println("生产A级-苹果");
	}
}
package 抽象工厂模式;
// Apple_B继承Apple类
public class Apple_B extends Apple{
    
    
	@Override
	public void get() {
    
    
		System.out.println("生产B级-苹果");
	}
}

Create a type A factory and a type B factory that produce A-level fruits and B-level fruits respectively , and implement the factory interface

package 抽象工厂模式;
//	FruitFactory_calssA 要去实现 FruitFactory
public class FruitFactory_calssA implements FruitFactory{
    
    
//  A级水果工厂只生产A级水果
	@Override
	public Fruit getApple() {
    
    
//		生产A级苹果
		return new Apple_A();
	}

	@Override
	public Fruit getBanana() {
    
    
//		生产A级香蕉
		return new Banana_A();
	}

}
public class FruitFactory_calssB implements FruitFactory{
    
    
//   B级水果工厂只生产B级水果
	@Override
	public Fruit getApple() {
    
    
//		生产B级苹果
		return new Apple_B();
	}

	@Override
	public Fruit getBanana() {
    
    
//		生产B级香蕉
		return new Banana_B();
	}

}

Call demo *

package 抽象工厂模式;

public class test {
    
    
public static void main(String[] args) {
    
    

	FruitFactory f1 = new FruitFactory_calssA();
	FruitFactory f2 = new FruitFactory_calssB();
//	Apple
	Fruit apple_A = f1.getApple();
	apple_A.get();
	Fruit apple_B= f2.getApple();
	apple_B.get();
// 	Banana
	Fruit banana_A = f1.getBanana();
	banana_A.get();
	Fruit banana_B = f2.getBanana();
	banana_B.get();
}
}

Insert picture description here

Fourth, the factory method pattern and the abstract factory pattern comparison

Each form in the factory method pattern is a solution to a certain problem, and it is aimed at multiple product series structures; while the abstract factory pattern is aimed at multiple product family structures, and there are multiple product series within a product.

Guess you like

Origin blog.csdn.net/weixin_44676935/article/details/105066026