From simple to deep learning "factory model" (3)

Source: Internet
Author: User

3.5Abstract Factory Model

We continue to analyze the situation of photo and video appliances. We can use the factory method to create products, however, in the previous analysis, we did not consider such problems as the product type and manufacturer. Take the DVD for example, TCL can be produced, LG can also be produced, and many other manufacturers are producing. DVD is one type of product. The concept of product category is called a product family in some books. From another perspective, TCL can produce many products, such as DVDs and VCD. These products can constitute a product structure together. After considering these issues, we put forward two concepts: product type and product structure. In the factory method, we discuss the creation of a single product. If we conduct further research and expansion on this issue, we should go from a single product to multiple product categories, in the factory method, we consider that DVD is a single product. Now we think that DVD is a product type, Including TCL, LG, and VCD, there are VCD produced by TCL and VCD produced by LG. We will re-analyze this issue. There are two product types: DVD and VCD, and two factories are TCL and LG, which produce DVDs and VCD respectively. We use the following class diagram for representation:

DVD is an abstract class that provides a unified interface. LGDVD and TCLDVD are two specific classes. VCD and DVD are similar. There is an abstract factory Create, which derives two specific classes TCLCreate and LGCreate. Create provides two Abstract METHODS: factoryDVD and factoryVCD. They provide two interfaces for creating a DVD product and a VCD product. Implement these two methods in TCLCreate and LGCreate. In this way, TCLCreate can create its own DVD and VCD, and also LGCreate can pass through its own products.

The following is the code structure:

Public abstract class Create

{

Public abstract DVD factoryDVD ();

Public abstract VCD factoryVCD ();

}

 

Public class LGCreate: Create

{

Public override DVD factoryDVD ()

{

Return new LGDVD ();

}

 

Public override VCD factoryVCD ()

{

Return new LGVCD ();

}

}

 

Public class TCLCreate: Create

{

Public override DVD factoryDVD ()

{

Return new TCLDVD ();

}

 

Public override VCD factoryVCD ()

{

Return new TCLVCD ();

}

}

 

Public abstract class DVD

{

Public abstract string PlayVideo ();

}

 

Public class LGDVD: DVD

{

Public override string PlayVideo ()

{

Return "LG's DVD playing ";

}

}

 

Public class TCLDVD: DVD

{

Public override string PlayVideo ()

{

Return "the tcl dvd is playing ";

}

}

 

Public abstract class VCD

{

Public abstract string PlayVideo ();

}

 

Public class LGVCD: VCD

{

Public override string PlayVideo ()

{

Return "LG's VCD is playing ";

}

}

 

Public class TCLVCD: VCD

{

Public override string PlayVideo ()

{

Return "tcl vcd playing ";

}

}

 

The client uses the Abstract Factory Code as follows:

Private void button#click (object sender, System. EventArgs e)

{

Create TCL, LG;

TCL = new TCLCreate ();

LG = new LGCreate ();

PlayDVD (TCL); // output "the tcl dvd is playing"

PlayDVD (LG); // output "lg dvd playing"

 

}

Private void PlayDVD (Create create Create)

{

DVD/dvd = create. factoryDVD ();

MessageBox. Show (dvd. PlayVideo ());

}

 

Below we will promote the abstract factory model to the general situation, and its class diagram is as follows:


Abstract Factory: it provides interfaces for all specific factories and has nothing to do with the specific business logic of the application system. It provides a method for creating each product type.

Specific Factory: create each product in the product structure. It contains the business logic for creating different products. It implements interfaces in the abstract factory.

Abstract product: define the common interfaces of a product.

Specific product: it is the specific object that the customer needs to create.

 

In the factory method, each factory is responsible for creating a product and a series of products in each factory in the abstract factory. In the example above, we use actual factories such as TCL and LG. In actual applications, we often abstract classes based on products. They are mainly responsible for the creation of a series of products, the classes responsible for abstract factories are called specific factories, which are further abstracted from these specific factories. The abstracted factories are called abstract factories. Let's take a look at the extension of the abstract factory model.

 

The extension of the abstract factory includes adding a product type and adding a factory. When adding a specific product under an existing abstract product, we only need to add a new factory. For example, now we have a Haier (Haier) DVD and VCD, we can easily achieve expansion and meet the "open and closed principle ". As shown in:

When we have a new product, we will not be able to meet the "open and closed principle, because we need to modify each output method from the existing factory to create a new product. For example, we add an Mp4 product category.

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.