Article structure: what is the meaning, what is solved, examples, examples, illustrations + illustration
Meaning:
Provides an interface for creating a list of related or interdependent objects without specifying their specific classes.
Solution:
Solves the interface selection problem
Advantages:
When multiple objects in the whole product are designed to work together, he can ensure that the client is always in the same product is used only object, like a USB mouse and keyboard interface can link, then you use the mouse and keyboard are belong to the USB interface, and DP and HDMI link display, that is to say, when you use the display, not to link the USB interface
Disadvantages:
When you add a USB interface user, you need to add an object to the abstract factory, and you need to create an interface, a set of objects, and a return value in the FactoryProduct factory selection, which is relatively cumbersome
The instance
Step 1: Create an interface and define a graphical interface as the base interface
public interface Shape {
void draw(a);
}
Copy the code
Step 2: Create three graphical entity classes and implement the graphical interface
Rectangle classpublic class Rectange implements Shape {
@Override
public void draw(a) {
System.out.println("Inside Rectangle::draw() method."); }} Square classpublic class Square implements Shape {
@Override
public void draw(a) {
System.out.println("Inside Square::draw() method."); }} Round classpublic class Circle implements Shape {
@Override
public void draw(a) {
System.out.println("Inside Circle::draw() method."); }}Copy the code
Step 3: Create a color interface. Create it in factory mode
public interface Color {
void fill(a);
}
Copy the code
Step four: Create a concrete implementation class for the color interface
Color - Redpublic class Red implements Color {
@Override
public void fill(a) {
System.out.println("Inside Red::fill() method."); }} Color - greenpublic class Blue implements Color {
@Override
public void fill(a) {
System.out.println("Inside Blue::fill() method."); }} Color - greenpublic class Green implements Color {
@Override
public void fill(a) {
System.out.println("Inside Green::fill() method."); }}Copy the code
Step 5: Create abstract factories for colors and shapes
This step is also a step that distinguishes the abstract factory from the factory design pattern, creating abstract classes for color and Shape objects to get the factory.
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape) ;
}
Copy the code
Step 6: Create a color and shape factory class
public 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
public Color getColor(String color) {
return null; }}public class ColorFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
return null;
}
@Override
public 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; }}Copy the code
Step 7: Create a factory to create it, and pass the corresponding information to get the factory
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; }}Copy the code
Step 8: Use FactoryProducer to get AbstractFactory. Pass type information to get object of entity class.
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
// Get the shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
// Get the object with the shape Circle
Shape shape1 = shapeFactory.getShape("CIRCLE");
// Call the draw method of Circle
shape1.draw();
// Get an object with a Rectangle shape
Shape shape2 = shapeFactory.getShape("RECTANGLE");
// Call the Rectangle draw method
shape2.draw();
// Get an object with the shape Square
Shape shape3 = shapeFactory.getShape("SQUARE");
// Call Square's draw method
shape3.draw();
// Get the color factory
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
// Get an object whose color is Red
Color color1 = colorFactory.getColor("RED");
// Call Red's fill method
color1.fill();
// Get the object whose color is Green
Color color2 = colorFactory.getColor("Green");
// Call Green's fill method
color2.fill();
// Get the object whose color is Blue
Color color3 = colorFactory.getColor("BLUE");
// Call Blue's fill methodcolor3.fill(); }}Copy the code
Illustrations and illustrations (illustrations – correspond to the above example, illustrations correspond to my own example)
Description of the entire abstract factory design pattern
We create an abstract factory class C and add two abstract methods (A,B,B) to it. We create an abstract factory class C and add two abstract methods (A,B,B) to it. Create a factory output class with D for factory 4: If AF is obtained, then AF. GetShape (” the type of the graphic passed in “) will obtain an instance of the graphic. If AF is obtained, then AF.