C ++ design mode implementation-Template Mode

Source: Internet
Author: User

I. Problems

In the analysis and design process of the object-oriented system, we often encounter the following situation: for a business logic (Algorithm Implementation), different details are implemented in different objects, however, the logic (algorithm) Framework (or general application algorithm) is the same. Template provides an implementation framework for this situation.

Ii. Mode

The Template mode implements this by means of inheritance:Place the logic (algorithm) framework in the abstract base class, define the detailed interface, and implement the details in the subclass..

Iii. Code

[Cpp]View plaincopy
  1. // Abstract the base class to implement a template method
  2. Class AbstractClass
  3. {
  4. Public:
  5. Virtual ~ AbstractClass ()
  6. {
  7. }
  8.  
  9. // Template method, which is only implemented in the abstract base class
  10. Void TemplateMethod ()
  11. {
  12. This-> PrimitiveOperation1 ();
  13. This-> PrimitiveOperation2 ();
  14. }
  15.  
  16. Protected:
  17. Virtual void PrimitiveOperation1 () = 0;
  18.  
  19. Virtual void PrimitiveOperation2 () = 0;
  20.  
  21. AbstractClass ()
  22. {
  23. }
  24. };
  25.  
  26. // Specific subclass to implement specific operation details
  27. Class ConcreteClass1: public AbstractClass
  28. {
  29. Public:
  30. ConcreteClass1 ()
  31. {
  32. }
  33.  
  34. ~ ConcreteClass1 ()
  35. {
  36. }
  37.  
  38. Protected:
  39. Void PrimitiveOperation1 ()
  40. {
  41. Cout <"concreteclass1... PrimitiveOperation1" < }
  42.  
  43. Void PrimitiveOperation2 ()
  44. {
  45. Cout <"concreteclass1... PrimitiveOperation2" < }
  46. };
  47.  
  48. // Specific subclass to implement specific operation details
  49. Class ConcreteClass2: public AbstractClass
  50. {
  51. Public:
  52. ConcreteClass2 ()
  53. {
  54. }
  55.  
  56. ~ ConcreteClass2 ()
  57. {
  58. }
  59.  
  60. Protected:
  61. Void PrimitiveOperation1 ()
  62. {
  63. Cout <"ConcreteClass2. .. PrimitiveOperation1" < }
  64.  
  65. Void PrimitiveOperation2 ()
  66. {
  67. Cout <"ConcreteClass2. .. PrimitiveOperation2" < }
  68. };
  69.  
  70.  
  71. Int main ()
  72. {
  73. AbstractClass * p1 = new ConcreteClass1 ();
  74. AbstractClass * p2 = new ConcreteClass2 ();
  75.  
  76. P1-> TemplateMethod ();
  77. P2-> TemplateMethod ();
  78.  
  79. Return 0;
  80. } The key point is to encapsulate general algorithms in abstract base classes and put different algorithm details into sub-classes for implementation.

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.