Design Pattern learning note (3) -- abstract factory pattern

Source: Internet
Author: User

Abstract Factory is a creation mode to solve the problems caused by instantiation.
Let's first look at the problem. Sometimes we will encounter this situation and we need a series of objects. For example, there are a series of BMW Auto Parts objects: wheel bmwwheel, tank bmwoilbox, which are called in a management function, Code As follows:
Class bmwwheel
{
Public bmwwheel (){};
}
Class bmwoilbox
{
Public bmwoilbox (){};
}
Public void manage ()
{
Bmwoilbox oilbox = new bmwoilbox ();
Bmwwheel wheel = new bmwwheel ();
}
If the demand changes now, we need to use parts of Volkswagen FAW Bora, instead of BMW, in addition to adding the corresponding part object, we also need to change the object in the manage function to the BORA part object.
At this time, we found that new will bring about some problems: Implementation dependencies cannot cope with changes in the specific instantiation type.
How can this problem be solved? Encapsulation change point. (Encapsulation is not required if no changes exist)
The origin of the factory model:
1. The change point is "Object creation", so the "Object creation" is encapsulated"
2. Interface-Oriented Programming
Simple factory problems:
1. cannot cope with changes to "different series objects. For example, if we want to add other objects in the code above, it cannot be a good response.
2. Use object-oriented Chinese technology to encapsulate changes
Motivation: In software systems, there are often "a series of mutually dependent objects". At the same time, due to changes in requirements, there are often more series of object creation work. In the face of this problem, we want to bypass the conventional object creation method and provide a "encapsulation mechanism" to avoid customersProgram It is tightly coupled with this "Multi-series object creation work.
I did not like this word for "tight coupling", but today I understand that it is not a poor program tight coupling, but a frequent change in demand, tight coupling will make programming very difficult. In the face of a constant demand, loose coupling and tight coupling should be no different in code writing.
The intent of this pattern is to provide an interface for creating a series of "related or interdependent objects" without specifying their specific classes.
Let's take a look at how to encapsulate this change using the abstract factory model:
First, we need BMW's wheels and fuel tanks. Of course they need to inherit their own base classes. The Code is as follows:

Abstract class abstractwheel
{
Public encryption actwheel ()
{
// Console. Write ("Create a abstractproduct ");
}
}

Abstract class abstractoilbox
{
Public abstractoilbox ()
{}
}

Class bmwwheel: abstractwheel
{
Public bmwwheel ()
{
Console. Write ("Create a bmwwheel ");
}
}

Class bmwoilbox: abstractoilbox
{
Public bmwoilbox ()
{
Console. Write ("Create a bmwoilbox ");
}
}
 
Then, we are building a factory that produces these parts. It inherits from an abstract factory.

// Abstract Factory
Abstract class abstractfactory
{
Abstract Public abstractwheel creatwheel ();
Abstract Public abstractoilbox creatoilbox ();
}

Class bmwfactory: abstractfactory
{
Public override maid ()
{
Return new bmwwheel ();
}

Public override maid ()
{
Return new bmwoilbox ();
}

}

Now we call them in the main function:
Static void main (string [] ARGs)
{
Abstractfactory factory = NULL;
Factory = new bmwfactory ();
Factory. creatwheel ();
Console. Write ("\ n ");
Factory. creatoilbox ();
Console. Write ("\ n ");
Console. Read ();
}
Display result:
Create a bmwwheel
Create a bmwoilbox

Now we want to use Bora parts instead of BMW parts. First, we need to write some Bora parts:
Class borawheel: abstractwheel
{
Public borawheel ()
{
Console. Write ("Create a borawheel ");
}
}

Class boraoilbox: abstractoilbox
{
Public boraoilbox ()
{
Console. Write ("Create a boraoilbox ");
}
}
Then we create a Bora part Factory:
Class borafactory: abstractfactory
{
Public override maid ()
{
Return new borawheel ();
}

Public override maid ()
{
Return new boraoilbox ();
}

}< br> let's take a look at how to modify the part that calls Bora in the main function. We just need to instantiate the factory in main as the BORA Factory:
static void main (string [] ARGs)
{< br> abstractfactory factory = NULL;
factory = new borafactory ();
factory. creatwheel ();
console. write ("\ n");
factory. creatoilbox ();
console. write ("\ n");
console. read ();
}< br>
result:
Create a borawheel
Create a boraoilbox


Abstract Factory mode:
1. If the "Multi-series Object Construction" requirement does not change, the abstract factory mode is not necessary.
2. "series objects" refer to the relationship between objects that are mutually dependent or act.
3. The Abstract Factory mode mainly aims to cope with changes in the demand of the new series. The disadvantage is that it is difficult to cope with changes in the demand for "new objects. It should be noted that, as mentioned above, if we want to add other series classes, the code will be greatly changed.
4. The Abstract Factory mode is often combined with the factory method mode to cope with changes in the "Object creation" requirement.

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.