Learning design patterns by contrast -- from factory patterns to abstract factory patterns

Posted by EviL_CodE on Wed, 05 Feb 2020 13:25:09 +0100

Catalog

I. Preface

2, Factory mode

2.1 overview

2.2 code

2.3 summary

3, Abstract factory pattern

3.1 overview

3.2 code

3.3 summary

4, Comparison of two modes

4.1 draw "factory mode" and "abstract factory mode" in the same figure

4.2 further illustration of "factory mode" and "abstract factory mode"

Five, summary

I. Preface

Factory pattern and abstract factory pattern are the two design patterns that everyone starts to learn. The significance of this paper is to compare the similarities and differences between the two design patterns. This paper uses the same example to carry out comparative learning (using the same example helps readers fundamentally distinguish the two design patterns).

This paper is divided into three parts, which are "factory model", "abstract factory model" and "comparison of two models". See the following.

2, Factory mode

2.1 overview

Official explanation: define an interface for creating an object, but let subclass decide which class to instance.factory method lets a class def instance to subclass. Factory method is a class whose instantiation is delayed to its subclass.)
My understanding: factory pattern, in a method (factory method) of a class (factory class), determines the runtime type according to the parameter, creates a new object and return s.

Participants: product class (abstract product class AbstractProduct, concrete product class ConcreteProduct), concrete factory class ConcreteFactory

Class diagram:

2.2 code

Specific class diagram:

Code sketch:


interface Fruit{
	void display();
}
class Apple implements Fruit{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is an Apple");
	}
	
}
class Orange implements Fruit{

	@Override
	public void display() {
		System.out.println("This is an orange");
		
	}
	
}
class Banana implements Fruit{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is a banana");
	}
	
}
class FruitFactory {
	public Fruit produce_Fruit(String type){
		//The essence of factory pattern is to build different factory products according to parameters in factory method of factory class, and apply polymorphism to them
		if (type.equalsIgnoreCase("apple")) {
			return new Apple();
		}
		if (type.equalsIgnoreCase("orange")) {
			return new Orange();
		}
		if (type.equalsIgnoreCase("banana")) {
			return new Banana();
		}
		return null;
	}
}
public class TestFactory {

	public static void main(String[] args) {
		FruitFactory factory=new FruitFactory();
		factory.produce_Fruit("apple").display();
		factory.produce_Fruit("orange").display();
		factory.produce_Fruit("banana").display();
	}

}

Output results:

This is an Apple
This is an orange
This is a banana

2.3 summary

The essence of factory mode is: according to the parameters, the factory method of factory class uses upward transformation to create different factory products, instead of creating each class separately

3, Abstract factory pattern

3.1 overview

Provide an interface for creating families of related or dependent objects without specifying their concrete classes

My understanding: abstract factory pattern is to realize the abstraction of factory itself. Further in the factory mode, it not only solves the upward transformation of the factory products, but also reflects the upward transformation of the factory itself.

First, there is an abstract factory. In the factory creation class, the runtime type is determined according to the parameter, and each specific factory is created and returned;

Step 2: each factory is responsible for the corresponding abstract products. For any abstract product, the product creation class in the factory class determines the runtime type according to the parameters, and creates and returns each specific product. (the second step is the factory model in the previous article)

Factory pattern and abstract factory pattern
Factory mode Abstract product
Abstract factory pattern Abstract factory + abstract product

Participants: product class (abstract product class AbstractProduct, concrete product class ConcreteProduct), factory class (abstract factory class AbstractFactory, concrete factory class ConcreteFactory)

Class diagram:

3.2 code

Specific class diagram:

Code sketch:

interface Fruit{
	public void display();
}
class Apple implements Fruit{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is an Apple");
	}
	
}
class Orange implements Fruit{

	@Override
	public void display() {
		System.out.println("This is an orange");
		
	}
	
}
class Banana implements Fruit{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is a banana");
	}
	
}
interface Vegetable {
	public void display();
}
class Potato implements Vegetable{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is a Potato");
	}
	
}
class Tomato implements Vegetable{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is a Tomato");
	}
	
}
class Cabbage implements Vegetable{

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("This is a Cabbage");
	}
	
}
abstract class AbstarctFactory{
	abstract Fruit produce_Fruit(String type);
	abstract Vegetable produce_Vegetable(String type);
}
class FruitFactory extends AbstarctFactory{
	public Fruit produce_Fruit(String type){
		//The essence of factory pattern is to build different factory products according to parameters in factory method of factory class, and apply polymorphism to them
		if (type.equalsIgnoreCase("apple")) {
			return new Apple();
		}
		if (type.equalsIgnoreCase("orange")) {
			return new Orange();
		}
		if (type.equalsIgnoreCase("banana")) {
			return new Banana();
		}
		return null;
	}

	@Override
	Vegetable produce_Vegetable(String type) {
		// TODO Auto-generated method stub
		return null;
	}
}
class VegetableFactory extends AbstarctFactory{

	@Override
	Fruit produce_Fruit(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	Vegetable produce_Vegetable(String type) {
		if (type.equalsIgnoreCase("Potato")) {
			return new Potato();
		}
		if (type.equalsIgnoreCase("Tomato")) {
			return new Tomato();
		}
		if (type.equalsIgnoreCase("Cabbage")) {
			return new Cabbage();
		}
		return null;
	}
	
}
public class TestAbstractFactory {

	public static void main(String[] args) {
		System.out.println("=====Fruit factory=====");
		AbstarctFactory _fruit_factory=new FruitFactory();
		_fruit_factory.produce_Fruit("apple").display();
		_fruit_factory.produce_Fruit("orange").display();
		_fruit_factory.produce_Fruit("banana").display();
		System.out.println("=====Vegetable factory=====");
		AbstarctFactory _vegetable_factory=new VegetableFactory();
		_vegetable_factory.produce_Vegetable("potato").display();
		_vegetable_factory.produce_Vegetable("tomato").display();
		_vegetable_factory.produce_Vegetable("cabbage").display();
	}

}

Output:

=====Fruit factory=====
This is an Apple
This is an orange
This is a banana
=====Vegetable factory=====
This is a Potato
This is a Tomato
This is a Cabbage

3.3 summary

Abstract factory pattern not only completes the abstraction of products, but also renews and increases the abstraction of factories. Learning abstract factory pattern can strengthen our object-oriented understanding. Skillful use of abstract factory pattern can make development more efficient with less effort.

4, Comparison of two modes

Before comparison, the author adds a sentence here:

Factory mode, also known as factory method mode, is more appropriate to use factory method mode, because the core of factory mode is the method that creates a new product object according to the parameters and returns it.

Abstract factory pattern refers to the establishment of a parent-child inheritance system for factory classes (classes that can create new objects by passing in parameters).

Note: the "factory mode" and "factory method mode" mentioned below are the same thing. For better explanation, the following are all called "factory method mode".

4.1 draw "factory mode" and "abstract factory mode" in the same figure

We draw "factory mode" and "abstract factory mode" in the same figure, which is convenient for us to compare, as shown in the figure:

In the figure above (the box represents the product and the ellipse represents the factory):

The left border box is factory mode, indicating fruit factory;
The right border box is factory mode, indicating vegetable factory;
The above frame is the abstract factory pattern, which abstracts factory objects (classes that can create factories)

From the above figure, we can see that:

Although factory method pattern and abstract factory pattern sound very similar, in fact, there is no intersection between them, no inclusion relationship.

Factory method mode is to create a new product object and return it according to the parameters.

Abstract factory pattern refers to the abstraction of factory class (the class that can be created and returned according to parameters), and the establishment of a parent-child inheritance system for factory class.

4.2 further illustration of "factory mode" and "abstract factory mode"

As we mentioned in the above figure, the whole figure is a combination of "factory pattern" and "abstract factory pattern", which can also be called abstract factory pattern (the abstract factory pattern that provides new objects of factory class). What does this mean? See the figure below:

The explanation of this figure is: the first is the factory method pattern, the second is the abstract factory pattern, and the combined result (i.e. the result after the equal sign) is the combination of "factory pattern" and "abstract factory pattern". So the whole graph is a combination of "factory pattern" and "abstract factory pattern".

In fact, the second and the third can be called abstract factory pattern. The second is the abstract factory pattern that does not implement new factory objects. The third is the abstract factory pattern that implements new factory objects. So it can also be called abstract factory pattern (which provides new objects of factory class).

Five, summary

This paper is divided into three parts, the second part introduces the factory mode (factory method mode), the third part introduces the abstract factory mode, the fourth part introduces the comparison of the two modes, hoping to be helpful for beginners.

Every day code, every day progress!

176 original articles published, 28 praised, 70000 visitors+
Private letter follow

Topics: less