Design Mode C ++ implementation (1) -- factory Mode)

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. Objects are used in the design model.Programming LanguageImportant features: encapsulation, inheritance, polymorphism, true understanding of the essence of the design model is a long process that requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This article introduces the implementation of the factory model.

The factory mode is a creation mode, which can be roughly divided into three types: simple factory mode, factory method mode, and abstract factory mode. It sounds like a factory model. Next, we will introduce the simple factory model one by one. Its main feature is to make judgments in the factory class to create corresponding products. When a new product is added, You need to modify the factory class. A little abstract. Let's take an example. There is a manufacturer that produces processor cores. It has only one factory and can produce two types of processor cores. What kind of processor cores the customer needs must explicitly inform the production plant. An implementation scheme is provided below.

[CPP] View plaincopyprint?

  1. EnumCtype {Corea, coreb };
  2. ClassSinglecore
  3. {
  4. Public:
  5. Virtual VoidShow () = 0;
  6. };
  7. // Single-core
  8. ClassSinglecorea:PublicSinglecore
  9. {
  10. Public:
  11. VoidShow () {cout <"Singlecore"<Endl ;}
  12. };
  13. // Single-core B
  14. ClassSinglecoreb:PublicSinglecore
  15. {
  16. Public:
  17. VoidShow () {cout <"Singlecore B"<Endl ;}
  18. };
  19. // The only factory that can produce two types of processor cores, which can be determined internally
  20. ClassFactory
  21. {
  22. Public:
  23. Singlecore * createsinglecore (EnumCtype)
  24. {
  25. If(Ctype = Corea)// Factory internal judgment
  26. Return NewSinglecorea ();// Production core
  27. Else If(Ctype = coreb)
  28. Return NewSinglecoreb ();// Production core B
  29. Else
  30. ReturnNULL;
  31. }
  32. };

The main disadvantage of this design has been mentioned before, that is, to add a new core type, you need to modify the factory class. This violates the open and closed principle: software entities (classes, modules, and functions) can be expanded, but cannot be modified. As a result, the factory method model emerged. The so-called factory method mode refers to defining an interface for creating objects, so that the subclass decides which class to instantiate. Factory method delays the instantiation of a class to its subclass.

It sounds very abstract. I will explain it in the example just now. The processor core producer made a lot of money, so he decided to set up another factory dedicated to produce the single-core number B, and the original factory dedicated to producing the single-core number. In this case, the customer needs to find the factory. For example, if the-type core is required, the customer needs Factory A; otherwise, the customer needs factory B, you no longer need to tell the factory what type of processor core is required. An implementation scheme is provided below.

[CPP] View plaincopyprint?

  1. ClassSinglecore
  2. {
  3. Public:
  4. Virtual VoidShow () = 0;
  5. };
  6. // Single-core
  7. ClassSinglecorea:PublicSinglecore
  8. {
  9. Public:
  10. VoidShow () {cout <"Singlecore"<Endl ;}
  11. };
  12. // Single-core B
  13. ClassSinglecoreb:PublicSinglecore
  14. {
  15. Public:
  16. VoidShow () {cout <"Singlecore B"<Endl ;}
  17. };
  18. ClassFactory
  19. {
  20. Public:
  21. VirtualSinglecore * createsinglecore () = 0;
  22. };
  23. // A-core production plant
  24. ClassFactorya:PublicFactory
  25. {
  26. Public:
  27. Singlecorea * createsinglecore (){Return NewSinglecorea ;}
  28. };
  29. // B-core production plant
  30. ClassFactoryb:PublicFactory
  31. {
  32. Public:
  33. Singlecoreb * createsinglecore (){Return NewSinglecoreb ;}
  34. };

The factory method model also has disadvantages. Each time a product is added, a factory with an object needs to be added. If the company is developing rapidly and has launched many new processor cores, it is necessary to set up a new factory. In the implementation of C ++, the factory classes must be defined one by one. Obviously, the factory method mode requires more class definitions than the simple factory mode.

Now that we have a simple factory model and a factory method model, why do we need an abstract factory model? What does it do? For example, the company's technological advances can not only produce single-core processors, but also multi-core processors. At present, the simple factory model and the factory method model are beyond reach. The abstract factory model was launched. It is defined to provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes. In this case, the company opened two factories, one dedicated to the production of A-type single-core processor, and the other dedicated to the production of A-type single-core processor, the following shows the implementationCode.

[CPP] View plaincopyprint?

  1. // Single-core
  2. ClassSinglecore
  3. {
  4. Public:
  5. Virtual VoidShow () = 0;
  6. };
  7. ClassSinglecorea:PublicSinglecore
  8. {
  9. Public:
  10. VoidShow () {cout <"Single Core"<Endl ;}
  11. };
  12. ClassSinglecoreb:PublicSinglecore
  13. {
  14. Public:
  15. VoidShow () {cout <"Single Core B"<Endl ;}
  16. };
  17. // Multi-core
  18. ClassMulticore
  19. {
  20. Public:
  21. Virtual VoidShow () = 0;
  22. };
  23. ClassMulticorea:PublicMulticore
  24. {
  25. Public:
  26. VoidShow () {cout <"Multi core"<Endl ;}
  27. };
  28. ClassMulticoreb:PublicMulticore
  29. {
  30. Public:
  31. VoidShow () {cout <"Multi core B"<Endl ;}
  32. };
  33. // Factory
  34. ClassCorefactory
  35. {
  36. Public:
  37. VirtualSinglecore * createsinglecore () = 0;
  38. VirtualMulticore * createmulticore () = 0;
  39. };
  40. // Factory A, which is specially used to produce a-type Processor
  41. ClassFactorya:PublicCorefactory
  42. {
  43. Public:
  44. Singlecore * createsinglecore (){Return NewSinglecorea ();}
  45. Multicore * createmulticore (){Return NewMulticorea ();}
  46. };
  47. // Factory B, which is specially used to produce the processor type B
  48. ClassFactoryb:PublicCorefactory
  49. {
  50. Public:
  51. Singlecore * createsinglecore (){Return NewSinglecoreb ();}
  52. Multicore * createmulticore (){Return NewMulticoreb ();}
  53. };

So far, the factory model has been introduced. The Rational Rose 2003 software is used to provide UML diagrams of three factory models to deepen the impression.

UML diagram of simple factory mode:

UML diagram of the factory method:

Abstract The UML diagram of the factory model:

 


I have a blogArticleCopyright, reprinted please indicate the sourceHttp://blog.csdn.net/wuzhekai1985

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.