Design pattern C + + implementation (1)--Factory mode

Source: Internet
Author: User

Design patterns in the field of software provide developers with an effective way to use expert design experience. The design pattern uses the important characteristic of object-oriented programming language: encapsulation, inheritance, polymorphism, really comprehend the essence of design pattern is probably a long process, need to accumulate a lot of practical experience. Recently read the design pattern of the book, for each model, with C + + write a small example, deepen understanding. The main reference is "Big talk design mode" and "design pattern: The basis of reusable object-oriented software" two books. This article describes the implementation of the factory pattern.

The factory model belongs to the creation model, which can be divided into three types, simple Factory mode, factory method mode and abstract Factory mode. That sounds almost like a factory model. The following introduction, the first introduction of the Simple factory model, its main feature is the need to make judgments in the factory class, so as to create the corresponding products. When adding new products, you need to modify the factory class. A little abstract, for example, I understand. There is a manufacturer of processor cores, which has only one factory, capable of producing two types of processor cores. What kind of processor core the customer needs, be sure to show to the production plant. The following is an implementation scenario.

[CPP]View Plaincopyprint?
  1. Enum CTYPE {corea, Coreb};
  2. Class Singlecore
  3. {
  4. Public
  5. virtual void Show () = 0;
  6. };
  7. Single Core A
  8. Class Singlecorea: Public singlecore
  9. {
  10. Public
  11. void Show () {cout<<"Singlecore A" <<endl;}
  12. };
  13. Single Core B
  14. Class Singlecoreb: Public singlecore
  15. {
  16. Public
  17. void Show () {cout<<"Singlecore B" <<endl;}
  18. };
  19. The only factory that can produce two types of processor cores, in-house judgment
  20. Class Factory
  21. {
  22. Public
  23. singlecore* Createsinglecore (enum CTYPE CTYPE)
  24. {
  25. if (ctype = = Corea) //Factory internal judgment
  26. return new Singlecorea (); //production of nuclear a
  27. Else if (ctype = = Coreb)
  28. return new Singlecoreb (); //production of nuclear b
  29. Else
  30. return NULL;
  31. }
  32. };

The main drawbacks of this design have been mentioned before, that is, to add new kernel types, you need to modify the factory class. This violates the open closure principle: Software entities (classes, modules, functions) can be extended, but cannot be modified. So, the factory method pattern appeared. The so-called factory method pattern refers to defining an interface for creating objects, so that subclasses decide which class to instantiate. The Factory method defers the instantiation of a class to its subclasses.

It sounds abstract, or it is explained in the example just now. The producer of the processor's core made a lot of money and decided to set up another factory dedicated to the B-type single-core, and the original factory was designed to produce a single core of type A. At this point, the customer to do is to find a good factory, such as to a model of the nuclear, to find a factory to, or to find the B factory, no longer need to tell the factory specifically what type of processor core. Here is an implementation scenario.

[CPP]View Plaincopyprint?
  1. Class Singlecore
  2. {
  3. Public
  4. virtual void Show () = 0;
  5. };
  6. Single Core A
  7. Class Singlecorea: Public singlecore
  8. {
  9. Public
  10. void Show () {cout<<"Singlecore A" <<endl;}
  11. };
  12. Single Core B
  13. Class Singlecoreb: Public singlecore
  14. {
  15. Public
  16. void Show () {cout<<"Singlecore B" <<endl;}
  17. };
  18. Class Factory
  19. {
  20. Public
  21. virtual singlecore* createsinglecore () = 0;
  22. };
  23. Production of a nuclear plant
  24. Class Factorya: Public Factory
  25. {
  26. Public
  27. singlecorea* Createsinglecore () { return new Singlecorea;}
  28. };
  29. Plant producing B-core
  30. Class Factoryb: Public Factory
  31. {
  32. Public
  33. singlecoreb* Createsinglecore () { return new Singlecoreb;}
  34. };

Factory method models also have drawbacks, and each additional product requires an additional plant for the object. If the company develops rapidly and launches many new processor cores, it will have to open a new plant. In the C + + implementation, it is to define a factory class. Obviously, the factory method pattern requires more class definitions than the simple factory model.

Given the simple factory model and the factory approach model, why should there be an abstract factory model? How does it work? To cite this example, the company's technology continues to evolve, not only to produce single-core processors, but also to produce multicore processors. Both the simple Factory mode and the factory method mode are now beyond. The abstract factory model has appeared. It is defined to provide an interface that creates a series of related or interdependent objects without specifying their specific classes. In this application, the company also opened two factories, one dedicated to the production of a single core multi-core processor, and another factory dedicated to the production of B-type single-core multicore processors, the implementation of the code below.

[CPP]View Plaincopyprint?
  1. Single Core
  2. Class Singlecore
  3. {
  4. Public
  5. virtual void Show () = 0;
  6. };
  7. Class Singlecorea: Public singlecore
  8. {
  9. Public
  10. void Show () {cout<<"single Core A" <<endl;}
  11. };
  12. Class Singlecoreb: Publicsinglecore
  13. {
  14. Public
  15. void Show () {cout<<"single Core B" <<endl;}
  16. };
  17. Multi-core
  18. Class multicore
  19. {
  20. Public
  21. virtual void Show () = 0;
  22. };
  23. Class Multicorea: Public multicore
  24. {
  25. Public
  26. void Show () {cout<<"Multi Core A" <<endl;}
  27. };
  28. Class Multicoreb: Public multicore
  29. {
  30. Public
  31. void Show () {cout<<"Multi Core B" <<endl;}
  32. };
  33. Factory
  34. Class Corefactory
  35. {
  36. Public
  37. virtual singlecore* createsinglecore () = 0;
  38. virtual multicore* createmulticore () = 0;
  39. };
  40. Factory A, designed to produce a type a processor
  41. Class Factorya: Publiccorefactory
  42. {
  43. Public
  44. singlecore* Createsinglecore () { return new Singlecorea ();}
  45. multicore* Createmulticore () { return new Multicorea ();}
  46. };
  47. Factory B, dedicated to the production of B-type processors
  48. Class Factoryb: Public corefactory
  49. {
  50. Public
  51. singlecore* Createsinglecore () { return new Singlecoreb ();}
  52. multicore* Createmulticore () { return new Multicoreb ();}
  53. };

At this point, the factory model is finished. Use rational Rose 2003 software to give a UML diagram of three factory models to deepen impressions.

UML diagram for simple Factory mode:

UML diagram of the factory method:

UML diagram for abstract Factory mode:

I enjoy the copyright of the blog article, reproduced please indicate the source http://blog.csdn.net/wuzhekai1985

Design pattern C + + implementation (1)-Factory mode (RPM)

Related Article

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.