Abstract Factory model, Abstract Factory
· Abstract Factory Model
Abstract Factory Model(Abstract Factory Pattern)Is to create other factories around a super factory. This super factory is also called another factory. This type of design mode belongs to the creation mode, which provides an optimal way to create objects.
In the abstract factory mode, an interface is used to create a factory for related objects without explicitly specifying their classes. Each generated factory provides objects in the factory mode.
· Introduction
1. Intention:Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
2. Main solution:It mainly solves the problem of interface selection.
3. When to use:The system has more than one product family, and the system only consumes one of them.
4. How to solve the problem:Define multiple products in a product family.
5. key code:Aggregate multiple similar products in one factory.
6. application instance:After work, there must be two or more clothes to attend some gatherings, such as business clothes (Complete Sets, a series of specific products) and fashion clothes (Complete Sets, a series of specific products ), even for a family, there may be Business Women's Wear, business men's wear, fashion Women's Wear, fashion men's wear, these are also complete sets, that is, a series of specific products. Suppose a situation (in reality, it doesn't exist, otherwise it won't be possible to enter communism, but it is helpful to illustrate the abstract factory model), in your home, a wardrobe (specific factory) you can only store a certain type of clothing (set, a series of specific products). Each time you take this set of clothes, you will naturally need to take them out of this wardrobe. UseOOAll the wardrobes (specific factories) are one of the wardrobes (Abstract factories, each complete set of clothes includes a specific coat (a specific product) and trousers (a specific product). These specific jackets are actually jackets (Abstract products ), the specific trousers are also trousers (another abstract product ).
7. Advantages:When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family.
8. Disadvantages:It is very difficult to expand the product family. To add a product of a seriesCreatorAdd code, and add code to the specific content.
9. Application Scenario:.QQSkin change, a set of change together.
B.Generate programs of different operating systems.
10. Notes:The product family is difficult to expand, and the product grade is easy to expand.
· Implementation
We will createShapeAndColorInterfaces and object classes that implement these interfaces.
The next step is to create an abstract factory class.AbstractFactory.
Then define the factory classShapeFactoryAndColorFactoryThe two factory classes are all extended.AbstractFactory.
Then create a factory Generator/generator classFactoryProducer.
AbstractFactoryPattern, The usage of our demo classFactoryProducerTo obtainAbstractFactoryObject. It willAbstractFactoryTransmit shape informationShape (CIRCLE/RECTANGLE/SQUARE ),In order to obtain the type of the object required by it. At the same time, it alsoAbstractFactoryTransfer color informationColor (RED/GREEN/BLUE ),In order to obtain the type of the object required by it.
Code
Step 1
Create an interface for Shape.
public interface Shape { void draw();}
Step 2
Create an object class that implements the interface.
//Rectangle.javapublic class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}//Square.javapublic class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}//Circle.javapublic class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}
Step 3
Creates an interface for colors.
public interface Color { void fill();}
Step 4
Create an object class that implements the interface.
//Red.javapublic class Red implements Color { @Override public void fill() { System.out.println("Inside Red::fill() method."); }}//Green.javapublic class Green implements Color { @Override public void fill() { System.out.println("Inside Green::fill() method."); }}//Blue.javapublic class Blue implements Color { @Override public void fill() { System.out.println("Inside Blue::fill() method."); }}
Step 5
Create an abstract class for the Color and Shape objects to obtain the factory.
//AbstractFactory.javapublic abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ;}
Step 6
Created extendedAbstractFactoryGenerate object of the Object Class Based on the given information.
//ShapeFactory.javapublic class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } @Override Color getColor(String color) { return null; }}//ColorFactory.javapublic class ColorFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; } @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; }}
Step 7
Create a factory Creator/generator class to obtain the factory by passing the shape or color information.
//FactoryProducer.javapublic 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; }}
Step 8
UseFactoryProducerTo obtainAbstractFactory,The object of the object class is obtained by passing the type information.
// Define actfactorypatterndemo. javapublic class AbstractFactoryPatternDemo {public static void main (String [] args) {// obtain the shape factory AbstractFactory shapeFactory = FactoryProducer. getFactory ("SHAPE"); // obtain the Shape shape1 = shapeFactory of the Circle object. getShape ("CIRCLE"); // call the draw method shape1.draw () of Circle; // obtain the Shape shape2 = shapeFactory of the Rectangle object. getShape ("RECTANGLE"); // call the draw method shape2.draw () of Rectangle; // obtain the Shape shape3 = shapeFactory of the object with the Shape of Square. getShape ("SQUARE"); // call the draw method shape3.draw () of Square; // obtain the color factory AbstractFactory colorFactory = FactoryProducer. getFactory ("COLOR"); // obtain the object Color color1 = colorFactory whose COLOR is Red. getColor ("RED"); // call the Red fill method color1.fill (); // obtain the object Color color2 = colorFactory whose Color is Green. getColor ("Green"); // call the fill method color2.fill () of Green; // obtain the object Color color3 = colorFactory. getColor ("BLUE"); // call the Blue fill method color3.fill ();}}
Step 9
Execution output
Inside Circle::draw() method.Inside Rectangle::draw() method.Inside Square::draw() method.Inside Red::fill() method.Inside Green::fill() method.Inside Blue::fill() method.