As the number of thieves grew bigger, the company began to start a branch, which became the family of thieves. There are companies in Beijing and Hangzhou. Because people in different places have different octaves
The items used are also different. Of course, the theft methods are different. There are methods of theft in Beijing and in Hangzhou. This is the Abstract Factory of a thief company. They steal mobile phones and cameras.
The stealing methods in Beijing and Hangzhou are different. Therefore, Mobile Phone stealing is an abstract class (Interface), and camera stealing is also an abstract class. That is, abstract products. Stealing mobile phones in Beijing is a specific product.
Direct class diagram:
There is a picture with the truth. A picture basically shows everything. That's a little less explanation. The attachment containsCodeJava version. In fact, it has basically been achieved in the three bubble boxes of red, blue, and purple on the way.
The following code is used to write and analyze only the client code:
Package designpattern. abstractfactory;
Public ClassAbstractfactorytest {
/**
* @ Param ARGs
*/
Public Static VoidMain (string [] ARGs ){
// Todo auto-generated method stub
Istealcompanyfactory stealcamera =NewBeijingstealcompanyfactory ();
Istealcamera stealcamerainstance = stealcamera. createstealcamera ();
Stealcamerainstance. stealcamera ();
}
}
Here, the client first declares a variable that abstracts the factory interface, then instantiate an object of a specific factory, and then assign the object to the interface variable. I instantiated a thief company in Beijing as an object in a specific factory,
Then, the createstealcamera () method of this interface is called to produce a specific product, that is, a specific product of Beijing camera stealing, because the specific product category of camera stealing in Beijing implements an abstract interface such as camera stealing. Therefore, assign the specific product to the camera stealing interface.
Then, it is executed by calling the stealcamera () method in this interface.
The final result is as follows:
I stole a camera from Beijing!
Summary:
Abstract pipelines mainly provide interfaces for creating a series of related or mutually dependent objects, without the need to define their specific classes. (From the design mode ). The biggest benefit is the ability to change the product system. For example, in this example, if I want to steal a camera from Beijing to Hangzhou, I just need to say this. Istealcompanyfactory stealcamera =NewBeijingstealcompanyfactory ();NewBeijingstealcompanyfactory (); changed to new hangzhoustealcompanyfactory (); to minimize the modification.
Attachment: Abstract Factory of Java version