Design Patterns (2) - Abstract Factory pattern of preliminary study and case presentation

Abstract factory pattern

Abstract Factory (Abstract Factory Pattern) is to create other sub-factory design pattern around a super plant. The super plant is also known as total plant other plants. This type of design pattern and factory pattern to create the same type belonging to the model, which provides the best way to create objects.

In the abstract factory pattern, the interface is responsible for creating a related object factory, you do not need to explicitly specify their class. Each generated in accordance with the object factory can provide factory model.

Introduction

Intent: to provide a series of related or dependent objects to create interfaces without specifying their concrete classes.

Mainly to solve: the main problem of interface options.

When to use: the system has more than one product family product, the product can be drawn family plurality of objects, and the system in which only a family of consumer products.

How to solve: in a product family inside, define multiple products.

The key code: aggregating multiple similar products in a factory.

Pros: When a product family of multiple objects are designed to work together when it guarantees clients always use only objects in the same product family.

Disadvantages: product family expansion is very difficult, to increase a product of a series, both in the abstract Creator Riga codes, but also add specific code inside.

Usage scenarios:  1, QQ for the skin, a set of change together. 2, generate different procedures of the operating system.

Note: The product family is difficult to expand, easy to expand the product level.

Case I (references provided):

We will create  Shape  and  Color  interfaces and implementations of these interfaces entity classes. The next step is to create an abstract factory class  AbstractFactory . It then defines factory class  ShapeFactory  and  ColorFactory , these two classes are the factory extends  AbstractFactory . Then create a factory creator / builder class  FactoryProducer .

AbstractFactoryPatternDemo , our demo class uses  FactoryProducer  to get  AbstractFactory  object. To  AbstractFactory  transmitting shape information of  the Shape ( CIRCLE / the RECTANGLE / SQUARE ), it needs to obtain the type of the object. Also to  AbstractFactory  transmitting color information  Color ( RED / GREEN / BLUE ), it needs to obtain the type of the object.

 Case II (small series of write your own):

Background: The hypothesis for clothes to words, from the kinds of clothes you can be divided into shoes, T-shirts, suit pants, from the color of his clothes, you can be divided into red, yellow, green, blue, if we only generated according to the type of clothes in the case of an object, we can not accurately determine, for example, I party wear suit pants, but my suit pants in blue and black, I actually choose which one is it? Because we also need to instantiate a color.

I will create Closing and Color interfaces and implementations of these interfaces entity classes. Then create a total of abstract factory class AbstractFactory, and the definition of its sub-factory class ClosingFactory and ColorFactory, these two classes are the factory extends the abstract factory class, and then you need to create a FactoryProducer (factory creator)

Code Step:

First, the creation Closing interface and implement the interface entity class

1 public interface Closing {
2     void party();
3 }
1 public class TShirt implements Closing {
2     @Override
3     public void party() {
4         System.out.println("I am T-shirt");
5     }
6 }
1 public class Hat implements Closing {
2     @Override
3     public void party() {
4         System.out.println("I am hat");
5     }
6 }

Second, create a Color interface and its implementation class

1 public interface Color {
2     void party();
3 }
1 public class Red implements Color {
2     @Override
3     public void party() {
4         System.out.println("I am red");
5     }
6 }
1 public class Green implements Color {
2     @Override
3     public void party() {
4         System.out.println("I am Green");
5     }
6 }

Third, create an abstract factory class and two sub-total factory class

1 public abstract class  AbstractFactory {
2     public abstract Color getColor(String type);
3     public abstract Closing getClosing(String type);
4 }
 1 public class ClosingFactory extends AbstractFactory {
 2     @Override
 3     public Color getColor(String type) {
 4         return null;
 5     }
 6 
 7     @Override
 8     public Closing getClosing(String type) {
 9         if(type.equalsIgnoreCase("tshirt")){
10             return new TShirt();
11         }else if(type.equalsIgnoreCase("hat")){
12             return new Hat();
13         }
14         return null;
15     }
16 }
 1 public class ColorFactory extends AbstractFactory {
 2     @Override
 3     public Color getColor(String type) {
 4         if(type.equalsIgnoreCase("red")){
 5             return new Red();
 6         }else if(type.equalsIgnoreCase("green")){
 7             return new Green();
 8         }
 9         return null;
10     }
11 
12     @Override
13     public Closing getClosing(String type) {
14         return null;
15     }
16 }

Fourth, create a factory builder

 1 public class FactoryProducer {
 2     public static AbstractFactory getFactory(String choice){
 3         if(choice.equalsIgnoreCase("Color")){
 4             return new ColorFactory();
 5         }else if(choice.equalsIgnoreCase("closing")){
 6             return new ClosingFactory();
 7         }
 8         return null;
 9     }
10 }

Fifth, test our abstract factory method

 1 public class FactoryTest {
 2     public static void main(String[] args) {
 3         AbstractFactory closingFactory = FactoryProducer.getFactory("closing");
 4         Closing tshirt = closingFactory.getClosing("tshirt");
 5         tshirt.party();
 6         Closing hat = closingFactory.getClosing("hat");
 7         hat.party();
 8 
 9         AbstractFactory colorFactory = FactoryProducer.getFactory("color");
10         Color red = colorFactory.getColor("red");
11         red.party();
12         Color green = colorFactory.getColor("green");
13         green.party();
14     }
15 }

Sixth, the printout

I am T-shirt
I am hat
I am red
I am Green

  

Reference Source: https://www.runoob.com/design-pattern/abstract-factory-pattern.html

Guess you like

Origin www.cnblogs.com/zaevn00001/p/12612706.html