Learning Diary Template method mode and effective C + +

Source: Internet
Author: User

Template method Mode:

Definition: A skeleton that defines an algorithm in an operation. Instead, some of the steps are extended to subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of the algorithm.

(1), with inheritance, and certainly the case that this inheritance has meaning. You should be a template for subclasses, so repeated code should be promoted to the parent class, rather than having each subclass repeat.

(2). When we are finished with a certain level of detail or a series of steps, the implementation of individual steps at a more specific level may not be the same. We usually consider the template method pattern to handle.

(3), the template method pattern is moved to the superclass by changing the invariant behavior. Remove the repeated code in the subclass to reflect its advantages.

(4). The invariant and mutable behavior of the party is mixed in the subclass implementation of the method. The invariant behavior occurs repeatedly in subclasses. We move these behaviors to a single place through the template method pattern, which helps the subclass to get rid of the entanglement of repeated non-deformation.


Effective C + +:

1: Design class as design type

The design of class is the design of type. Before you define a new type, make sure that you have considered the discussion topics covered below. (Oh, my God!) )

(1). How should new types of objects be created and destroyed?

(2). What is the difference between the initialization of an object and the assignment of an object?

(3), the new type of object is assumed to be passed by value. What does it mean?

(4), what is the legal value of the new type?

(5), does your new type need to match an inheritance graph (inheritance graph)?

(6) What kind of conversion do you need for your new type?

(7), what operators and functions are reasonable for this new type?

(8). What kind of standard function should be dismissed?

(9), who should take the new type of membership?

(10), what is the new type of "Undeclared interface?"

(11), how generalized is your new type?

(12), do you really need a new type?


2: Ning to replace Pass-by-value with Pass-by-reference-to-const

(1), try to replace Pass-by-value with Pass-by-reference-to-const. The former is usually more efficient and can avoid segmentation problems (slicing problem).

(2). The above rules do not apply to built-in types, as well as STL iterators and function objects. For them, often pass-by-value is more appropriate.


3: When you must return an object, don't be paranoid about returning it reference

(1). Never return pointer or reference point to a local stack object, or return reference point to a Heap-allocated object, or return pointer or reference to a local stat An IC object may require multiple such objects at the same time. Logical return in single thread reference point to a local static object. A single case model is available.

Learning Diary Template method mode and effective C + +

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.