157 recommendations for writing high-quality code to improve C # programs--Recommendation 103: Differentiating combinations and inheritance applications

Source: Internet
Author: User

Recommendation 103: Applications that differentiate between combinations and inheritance

The polymorphism of inheritance is an important feature of object-oriented, but it cannot be abused in all situations. Inheritance should be used as a useful complement to the design architecture, not all.

A combination cannot be used for polymorphism, but the combination uses much more frequently than inheritance.

The following UML diagrams are inherited:

The corresponding code is as follows:

    Abstract class Stream    {        // omit     }    class  filestream:stream    {         //    }    class  memorystream:stream    {         // omit    }

The combined UML diagram is as follows:

The corresponding code is as follows:

    class Context    {        // omit     }    class  CultureInfo    {        / / omit     }    class  Thread    {        private Context m_context;         Private CultureInfo m_cultureinfo;         // omitted    }

From the design point of view, the inheritance represents "is a", the combination represents "has a". Both FileStream and MemoryStream are (is a) streams, and for thread threads, it has (have a) thread context and zone information CultureInfo. This is the most important difference, at any time, the design concept of factors always ranked first.

Inheritance is not just about inheriting from a type (class), it can also mean inheriting from an interface (interface). The greatest advantage of inheritance is polymorphism, which also lays the groundwork for abstract programming. Inheritance increases the reusability of the code. The combination obviously does not have this feature.

From a grammatical point of view, inheritance is easy to extend. Once the base class extends an interface with public, internal, protected access modifiers, all subclasses automatically have their interfaces, and the combination does not. To combine the behavior of any object, you must manually encode it. Take thread, for example, to combine the behavior of CultureInfo, you must first include a CultureInfo field inside yourself, as follows:

 class   CultureInfo { //  omit  public  void   Othermethod () {}}  class   private   Context M_context;         private   CultureInfo M_cultureinfo;  //  omit  public  void   Othermethod () {m        _cultureinfo.othermethod (); }    }

So far, it seems that the merits of inheritance have been said. In fact, the above advantages of inheritance are just the drawbacks of it. Subclasses naturally have a public interface of the base class, and this breaks the object-oriented "encapsulation". We obviously don't need each layer type to have all the interfaces on top. A class, if its inheritance system reaches 3 layers (of course, there are exceptions to everything, the control integration System in the WPF system, with shape as an example, up to 7 layers), you can consider the stop. If you do not stop, how many public methods and properties will be available to the caller for the lowest-level type? The answer is that the bottom-most type will have open interfaces for all upper-level types. With the development of the project, the advantages of the combination will be gradually reflected, its good encapsulation so that the type can be announced: I only do one thing.

Another advantage of a combination is that it can combine several other types. If you combine too many types, it means that the current class is likely to do too many things, and it needs to be split into two classes. Inheritance does not have such an attribute, in C #, a subclass can have only one base class (the interface releases this restriction, and subclasses inherit from multiple interfaces).

The use of inheritance or composition should be considered according to the actual situation. In general, the combination can be more satisfying for most applications. Do not abuse inheritance in order for the code to look like an "object-oriented".

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs--Recommendation 103: Differentiating combinations and inheritance applications

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.