Benefits of the abstract factory Model

Source: Internet
Author: User
Benefits of the abstract factory Model

I was writing an article about the design patterns, but this week I am busy with the compilation principles. I 'd like to write 1.0 pieces first.
I stated in advance that the following content is my personal experience and is not necessarily correct, comprehensive, or reasonable.
--------
In the number of gof instances, there are several creation modes. The most common method is the factory method. A simple description:
You need to create many objects, such as rectangles, circles, and rounded rectangles (maybe this is a configuration software or Visio ).
You can come to new anywhere, but people with a little experience will choose a special method to create objects. For example:
Cdrawobj * createdrawobj (INT objtype)
The benefits of doing so are obvious:
1. Code set
2. If the createdrawobj function is modified, the external code is not affected. For example, crectdrawobj is considered problematic. Someone writes a new
Class crectdrawobjex, you only need to modify createdrawobj, you can change it without knowing it.

In the preceding example, we understand the concept of virtual functions. cdrawobj is such a virtual base class. (C # is more abstract as interface)

Well, the current code is:
There is a createdrawobj function, and the call to this function is spread everywhere.
If I have a large set of new drawobj and want to switch between two sets of drawobj at runtime.
Intuitively, add a parameter to createdrawobj: cdrawobj * createdrawobj (INT objtype, int category)
In fact, this method is quite good.

However, oo does not like this set. We like to encapsulate variables into objects, so the above functions become two:
Cfactory * createfactory (INT factorytype)
Cfactory: createdrawobj (INT objtype)

In this way, when we want to change the drawobj family, we only need to generate a different factory in createfactory,
It does not need to be modified elsewhere. While using cdrawobj * createdrawobj (INT objtype, int category ),
For createdrawobj, different category parameters must be input as needed.

Encapsulating variables into objects is a small trick of object-oriented design.

I don't know.

To be honest, there are not many abstract factory uses. More often, they are the combination of factory methods and template methods, which are commonly used in framework design.

 

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.