I also designed the mode -- 2. Abstract Factory

Source: Internet
Author: User
This mode is a high-level abstraction of a simple factory. The UML diagram is as follows:

Public abstract class abstractfactory
{
Public abstract abstractproducta createproducta ();
Public abstract abstractproductb createproductb ();
}

Public class concretefactory1: abstractfactory
{
Public override abstractproducta createproducta ()
{
Return new producta1 ();
}

Public override abstractproductb createproductb ()
{
Return new productb1 ();
}
}

Public class concretefactory2: abstractfactory
{
Public override abstractproducta createproducta ()
{
Return new producta2 ();
}

Public override abstractproductb createproductb ()
{
Return new productb2 ();
}
}

Public abstract class abstractproducta {}

Public class producta1: abstractproducta {}

Public class producta2: abstractproducta {}


Public abstract class abstractproductb {}

Public class productb1: abstractproductb {}

Public class productb2: abstractproductb {}

Therefore, on the client side, interface-oriented programming can be used as follows:
Abstractfactory AF = new concretefactory1 ();
Abstractproducta APA = AF. createproducta ();
Abstractproductb APB = AF. createproductb ();

Abstract Factory, applicable to a set of product series, that is, it is easy to add a product-as long as three classes of producta3, productb3, and concretefactoryare added at the same time; it is not easy to add a method to operate ACTC-you can use the decorator mode to complete this function.

You can think about it using DB: A simple factory must generate a subclass for each database, and each subclass must implement the same method-there may be code redundancy; abstract Factory abstracts base classes such as abstractconnnection and abstractcommand respectively. Note that it is easy to add a subclass of oledbfactory and oledbconnection and oledbcommand, rather than adding a base class of abstractadapter.

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.