"Colon class" "Reading Notes" inheritance of OO

Source: Internet
Author: User

Software design should take into account flexibility and stability, promote external static internal movement. External static means to keep the outside interface unchanged, the internal movement refers to allow the inner realization of the change. This mantras the design of both abstract and design patterns as well as libraries, frameworks, architectures, and so on.

Implementing inheritance can break the wrapper of the parent class, such as accessing the protected member of the parent class, exposing the underlying class, and overriding the parent class's methods.

The biggest mishap of inheritance is the strong coupling between the classes, which makes the code stiff, brittle and complex.

When a subclass overrides a method of a parent class, it not only guarantees the specification of the method, but also maintains the specification of the corresponding associated method, and cannot break the logical association within the parent class specification.

When an ancestor class cannot provide a private interface that is isolated from an internal implementation for a descendant class, the encapsulation of the former is destroyed, and its changes can be implicated in the latter, causing the "Brittle base class" problem.

Promote interface inheritance, and implement inheritance with caution.

Non-abstract classes are not suitable for base classes.

Classes designed for inheritance should: Disable protected domain members, ensure the normative and stability of protected method members, and prevent side effects of coverage.

Although the non-polymorphic method is not as flexible as the polymorphic method, it is more stable and reliable, and it can not only avoid the security holes and encapsulation damage caused by inheritance, but also can bring about performance improvement.

Special care should be exercised when a public method of a class directly or indirectly invokes another polymorphic method of its own. If this kind of self-use can not be eliminated, it must be standardized, so as not to cover the quilt incorrectly. In addition, the interface and hooks should be separated as far as possible by the non-virtual interfaces: Make the public method not a polymorphic interface, so that the Polymorphic method is not a public hook.

Class instance methods generally have 4 uses (do not exclude one method of multi-use):

    1. A public interface for providing services to the outside world
    2. An internal callback that provides an expansion point for a subclass
    3. An internal interface that provides services for a subclass or package
    4. A private interface that provides services to itself

* Other than internal callbacks, preferably non-polymorphic

Comparison of inheritance and composition

    1. Inheritance is a kind of static dominant relation, and the combination is a kind of dynamic recessive relationship.
    2. Implementation inheritance is a gray-box multiplexing, and the combination is black-box multiplexing.
    3. Multiple inheritance is not usually supported or advocated, and multiple combinations are not.
    4. Inheritance is an eclectic mix of basic classes, and the combination is a critical inheritance of the underlying class.

The combination emphasizes the relationship of the static structure between the whole and the part, and the delegate forwarding emphasizes the dynamic separation between the external interface and the internal implementation.

"Colon class" "Reading Notes" inheritance of OO

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.