Abstract Factory Pattern (Abstract Factory Pattern) is to create other factories around a super factory. The Gigafactory is also known as the factory of other factories. This type of design pattern is a creational pattern, which provides an optimal way to create objects.
In the abstract factory pattern, an interface is a factory responsible for creating a related object without explicitly specifying their class. Each generated factory can provide objects according to the factory pattern.
The abstract factory pattern is the most abstract and general form of all the factory patterns. In order to facilitate the introduction of the abstract factory pattern, a new concept is introduced: Product Family. The so-called product family refers to the family composed of products located in different product hierarchy structures with related functions.
The shapes and colors in the following examples belong to a product family.
Abstract Factory (Abstract Factory) role: This role is the core of the factory method pattern, which has nothing to do with the business logic of the application system.
Concrete Factory role: This role creates an instance of the product directly under the call of the client. This role contains the logic of selecting the appropriate product object, and this logic is closely related to the business logic of the application system.
Abstract Product role: The class that assumes this role is the parent class of the objects created by the factory method pattern, or the interface they have in common.
Concrete Product role: Any product object created by the abstract factory pattern is an instance of a specific product class. This is what the client ultimately needs, and it must be full of business logic of the application system.
Sample code:
Interfaces: Shape Interface and Color Interface
/** * Color interface * * @author wangbo * @date 2018-04-27 18:18:12 */ public interface Color { void fill(); }
/** * Shape interface * * @author wangbo * @date 2018-04-27 18:10:20 */ public interface Shape { void draw(); }
The implementation class of the above interface:
/** * Red implementation class * * @author wangbo * @date 2018-04-27 18:19:19 */ public class Red implements Color { @Override public void fill() { System.out.println( "Fill with red" ); } }
/** * Green implementation class * * @author wangbo * @date 2018-04-27 18:21:09 */ public class Green implements Color { @Override public void fill() { System.out.println( "fill green" ); } }
/** * blue implementation class * * @author wangbo * @date 2018-04-27 18:21:43 */ public class Blue implements Color { @Override public void fill() { System.out.println( "fill blue" ); } }
/** * Circle implementation class * * @author wangbo * @date 2018-04-27 18:16:56 */ public class Circle implements Shape { @Override public void draw() { System.out.println( "Draw a circle" ); } }
/** * Rectangle implementation class * * @author wangbo * @date 2018-04-27 18:14:01 */ public class Rectangle implements Shape { @Override public void draw() { System.out.println( "Draw a rectangle" ); } }
/** * Square implementation class * * @author wangbo * @date 2018-04-27 18:15:34 */ public class Square implements Shape { @Override public void draw() { System.out.println( "Draw a square" ); } }
abstract factory class
/** * Abstract factory * * @author wangbo * @date 2018-04-27 18:24:37 */ public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape); }
color factory
/** * Color factory * * @author wangbo * @date 2018-04-27 18:39:40 */ public class ColorFactory extends AbstractFactory { /** * get color */ @Override Color getColor(String color) { if (color == null) { return null; } if (color.equalsIgnoreCase("RED")) { return new Red(); } else if (color.equalsIgnoreCase("GREEN")) { return new Green(); } else if (color.equalsIgnoreCase("BLUE")) { return new Blue(); } return null; } @Override Shape getShape(String shape) { return null; } }
shape factory
/** * Shape Factory * * @author wangbo * @date 2018-04-27 18:26:26 */ public class ShapeFactory extends AbstractFactory { @Override Color getColor(String color) { return null; } /** * get shape */ @Override Shape getShape(String shape) { if (shape == null) { return null; } if (shape.equalsIgnoreCase("CIRCLE")) { return new Circle(); } else if (shape.equalsIgnoreCase("RECTANGLE")) { return new Rectangle(); } else if (shape.equalsIgnoreCase("SQUARE")) { return new Square(); } return null; } }
Factory creates class
/** * Factory creator * * @author wangbo * @date 2018-04-27 18:47:26 */ public class FactoryProducer { public static AbstractFactory getFactory(String choice){ if (choice.equalsIgnoreCase("SHAPE")) { return new ShapeFactory(); } else if (choice.equalsIgnoreCase("COLOR")) { return new ColorFactory(); } return null; } }
test class
public class AbstractFactorTest { public static void main(String[] args) { // Get the shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE" ); // Get circular Shape shape1 = shapeFactory.getShape("CIRCLE" ); shape1.draw(); // Get rectangle Shape shape2 = shapeFactory.getShape("RECTANGLE" ); shape2.draw(); // Get the square Shape shape3 = shapeFactory.getShape("SQUARE" ); shape3.draw(); // Get the color factory AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR" ); // Get red Color color1 = colorFactory.getColor("RED" ); color1.fill(); // Get green Color color2 = colorFactory.getColor("Green" ); color2.fill(); // Get blue Color color3 = colorFactory.getColor("BLUE" ); color3.fill(); } }