An example of an abstract factory

Source: Internet
Author: User

The abstract factory in the design mode is like this: it provides an interface to create multiple associated objects without specifying their specific classes. It is a factory that can produce many products. When users need a product, they do not need to care about the specific implementation of the product. Instead, they only need to specify the product name, and the abstract factory can be produced according to your requirements. The product mentioned here represents an instance class in C.

In the object-orientedProgramIn design, we often define many classes and instantiate them through new. Once there are many class objects and Initialization is complicated during instantiation, we can manage them through the abstract factory. Abstract The object into a class based on its nature and content, and define both interfaces and methods. Once an abstract factory is defined, the work of the Instance class is handed over to the factory. The user only needs to tell the factory what the instantiated object is.

Below we define a "Audiovisual factory" with CD and DVD products. In addition, CD and VCD are classified based on audio and vidio respectively. Suppose we regard both CD and DVD as avdevice, then the classes related to CD and DVD can be abstracted into an interface: iavdevice. This interface is used to obtain the audio and video of the product.

Now there are two objects, CD and DVD, which share the same nature as audio and video. Obviously, we should define CD and DVD as classes and audio and video as interfaces. (We can use interfaces as the classification of objects of the same nature. All objects of the same nature implement this interface ). Because both CD and DVD belong to an avdevice, there is no doubt that they all implement the iavdevice interface. But should they also implement the iaudio and ividio interfaces? The answer is yes, but the definition levels are chaotic, so we 'd better define different class objects for CD and DVD to implement these two interfaces. The final definition is as follows:

Public interface iavdevice
{
Iaudio getaudio ();
Ivideo getvideo ();
}

Public interface ivideo
{
String getpicturequality ();
}

Public interface iaudio
{
String getsoundquality ();
}

Class CCD: iavdevice
{
Public iaudio getaudio ()
{
Return new ccdaudio ();
}
Public ivideo getvideo ()
{
Return new ccdvideo ();
}
}

Class cdvd: iavdevice
{
Public iaudio getaudio ()
{
Return new cdvdaudio ();
}
Public ivideo getvideo ()
{
Return new cdvdvideo ();
}
}

Class ccdaudio: iaudio
{
Public String getsoundquality ()
{
Return "CD audio is better then DVD Audio ";
}
}

Class ccdvideo: ivideo
{
Public String getpicturequality ()
{
Return "CD video quality is not as good as DVD ";
}
}

Class cdvdaudio: iaudio
{
Public String getsoundquality ()
{
Return "DVD audio is not as good as CD audio ";
}
}

Class cdvdvideo: ivideo
{
Public String getpicturequality ()
{
Return "DVD video quality is better then cd ";
}
}

Structure

Then, we need to implement a mechanism: Can we "produce" the corresponding products, CD or DVD, as required by users?

Class cavmaker
{
Public iavdevice avmake (string xwhat)
{
Switch (xwhat. tolower ())
{
Case "cd ":
Return new CCD ();
Case "DVD ":
Return new cdvd ();
Default:
Return new CCD ();
}
}

}

Now we can look at its execution process: When we input "cd", the cavmaker instance is a CCD object. (Note that the returned result type is iavdevice ). The result is equivalent to the following:Code:

Iavdevice objfac = new CCD ();

If we call the getaudio () and getvideo () methods for objfac respectively, The ccdaudio and ccdvideo class objects will be instantiated Based on the objfac object type (which belongs to the CCD class. (Note the returned type). In this case, the following code is executed:

Iaudio objaudio = new ccdaudio ();
Ivideo objvideo = new ccdvideo ();

Now we can call their respective methods.

The code for using the above interfaces and classes is as follows:
Public class abstractfactory
{
Static void main (string [] ARGs)
{
Cavmaker objfactmaker = new cavmaker ();
Iavdevice objfact;
Iaudio objaudio;
Ivideo objvideo;
String strwhat;
Strwhat = ARGs [0];
Objfact = objfactmaker. avmake (strwhat );
Objaudio = objfact. getaudio ();
Objvideo = objfact. getvideo ();
Console. writeline (objaudio. getsoundquality ());
Console. writeline (objvideo. getpicturequality ());
}
}

During execution, run the compiled EXE file and enter "cd" or "DVD" in the command line to get different results. Through this example, we can have a preliminary understanding of the abstract factory.

Note: This article uses a www.c-sharpcorner.comArticleIn the previous example, I made a simple analysis on it.

 

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.