Abstract factory pattern

1. General

Compared with the factory method pattern, the abstract factory pattern is for one product family, while the abstract factory pattern is for multiple product families, that is, the factory method pattern is a product family and a factory class, while the abstract factory pattern is a factory class for multiple product families. In the abstract factory pattern, the client is no longer responsible for the creation of objects, but loses this responsibility to the specific factory class. The client is only responsible for calling objects, so as to clarify the responsibilities of each class. And when a series of interrelated products are designed into a factory class, the call of the client will become very simple. Moreover, if you want to replace this series of products, you only need to replace a factory class.

If the client needs to create some product structures, and these product structures belong to different product categories, you can use the abstract factory mode. In the abstract factory mode, the abstract factory class is responsible for defining the interface for creating objects. The creation of this series of objects is completed by the specific factory class that implements the abstract factory.

2. Role

There are four roles in the abstract factory model: Abstract Factory role, concrete factory role, abstract product role and concrete product role.

  • Abstract factory role: this role is the core of the factory method pattern, which is independent of the business logic of the application system.
  • Specific factory role: this role directly creates an instance of the product under the call of the client. This role contains the logic of selecting the appropriate product object, which is closely related to the business logic of the application system.
  • Abstract product role: the class in this role is the parent class of the object created by the factory method pattern, or the interface they share.
  • Concrete product role: any product object created by the abstract factory pattern is an instance of a concrete product class. This is what the client ultimately needs, and its interior must be full of business logic of the application system.

3. Examples


Shoes:

  1. NikeShoes
  2. AdidasShoes

Clothes:

  1. NikeClothes
  2. AdidasClothes

Pants:

  1. NikePants
  2. AdidasPants

Abstract product interface

Shoe interface:
public interface Shoes{}
Clothing interface:
public  interface Clothes{}
Pants interface:
public interface Pants{}

Specific product interface

shoes
public class NikeShoes implement Shoes{
	public NikeShoes(){
		system.out.println("Nike shoes...");
	}
}
public class AdidasShoes implement Shoes{
	public AdidasShoes(){
		system.out.println("Adidas shoes...");
	}
}
clothes
public class NikeClothes implement Shoes{
	public NikeClothes(){
		system.out.println("Nike clothes...");
	}
}
public class AdidasShoesimplement Shoes{
	public AdidasShoes(){
		system.out.println("Adidas clothes...");
	}
}
trousers
public class NikePants implement Shoes{
	public NikePants(){
		system.out.println("Nike pants...");
	}
}
public class AdidasPants implement Shoes{
	public AdidasPants(){
		system.out.println("Adidas pants...");
	}
}

Factory behavior interface

public interface Factory{
	Shoes createShoes();
	Clothes createClothes();
	Pants createPants();
}

Specific factory category
Nike factory

public class NikeFactory implements Factory{
	@Override
	public Shoes createShoes(){
		return new NikeShoes();
	}
	@Override
	public Clothes createClothes(){
		return new NikeClothes();
	}
	@Override
	public Pants createPants(){
		return new NikePants();
	}
}
Adidas factory
public class AdidasFactory implements Factory{
	@Override
	public Shoes createShoes(){
		return new AdidasShoes();
	}
	@Override
	public Clothes createClothes(){
		return new AdidasClothes();
	}
	@Override
	public Pants createPants(){
		return new AdidasPants();
	}
}

4. Function

One of the main functions of the abstract factory pattern is that it can isolate the specific product classes to be generated. Because the actual class names of these classes are hidden inside the factory, the client does not need to care about the details of how to instantiate them. Each design pattern is a solution to a specific problem, and the problem faced by the abstract factory pattern is how to better design the software architecture when multiple product level structures are involved.

Keywords: Java Design Pattern

Added by jiayanhuang on Tue, 04 Jan 2022 11:47:26 +0200