Thieves company-factory models include (simple factory, factory method, abstract factory) (iii)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.