Object-Oriented Design Patterns and principles

Source: Internet
Author: User

I recently learned a series of courses about the object-oriented design model by Mr. Li Jianzhong. I want to summarize them so that I can learn them later. The design model is a very empty topic, with the accumulation of our programming experience, we can increase our understanding of it. This is a long process, but we should learn to think and analyze it in the usual programming process, think about what kind of design pattern is used in a specific scenario and how code can be easily expanded and maintained. This is a problem we need to think about, therefore, mastering the design pattern can play a major role in our programming and make the code we write very beautiful.

The design pattern describes a general solution to a certain type of common problems in the software design process. The object-oriented design pattern describes the specific scenarios in the object-oriented design process, common organizational relationships between classes and objects that communicate with each other.

Design Pattern and object-oriented

1. The object-oriented design model solves the "Organizational Relationship between classes and communication objects", including their roles, responsibilities, and collaboration methods.

2. The object-oriented design model is "Good object-oriented design". The so-called "Good object-oriented design" is the design that can meet the needs of "Coping with changes and improving reuse.

3. Object-oriented Design Patterns Describe software design, so they are independent of programming languages. However, the ultimate implementation of object-oriented design patterns still needs to be expressed in object-oriented programming languages.

4. Unlike algorithm techniques, the object-oriented design model can be used as usual. It is based on an empirical understanding of the "Object-Oriented" skillful and in-depth understanding. The premise of understanding the object-oriented design model is to first master the "Object-Oriented"!

Intuitive understanding of object-oriented programming languages

1. Various object-oriented programming languages are different from each other, but we can see their support for the three major object-oriented mechanisms: "encapsulation, inheritance, and polymorphism". Encapsulation: Hiding internal implementations; inheritance: code reuse; polymorphism: rewriting object behavior

2. The use of object-oriented programming languages (such as C #) can promote programmers to think about the software design structure with object-oriented thinking, so as to strengthen the object-oriented programming paradigm.

3. C # is an excellent language that supports object-oriented programming, including: support for various levels of encapsulation; single-implementation inheritance + Multi-implementation interfaces; rewrite of abstract methods and Virtual Methods

However, the object-oriented programming language (oopl) is not all object-oriented, or even stranded. Oopl's three major mechanisms, namely encapsulation, inheritance, and polymorphism, can express all object-oriented concepts. However, these three mechanisms do not represent the core spirit of forward objects. In other words, you can implement object-oriented design and development without using an object-oriented language! Therefore, we cannot rely on the object-oriented mechanism of programming languages to master object-oriented.Oopl did not answer the fundamental question of object-oriented-why should we use object-oriented? How should we use three mechanisms to achieve "Good Object-Oriented"? What kind of object-oriented principles should we follow?

From design principles to design patterns

1. Programming for interfaces, rather than for implementation

The customer program does not need to know the specific type of the object in use, but only needs to know that the object has the interface the customer expects.

2. prioritize the use of object combinations rather than class inheritance

Class inheritance is usually "white box reuse", and the object combination is usually "black box reuse". Inheritance damages encapsulation to some extent, and the Child class parent class has a high Coupling Degree. Object combination only requires that the combined object has a well-defined interface, and the coupling degree is low.

3. encapsulation changes

The encapsulation is used to create the demarcation layer between objects, so that the designer can modify the interface on one side of the demarcation layer without adversely affecting the other side, so as to achieve low coupling between layers.

4. Use the refactoring Mode

The application of the design pattern is not preemptible. The use of the design pattern as soon as it comes up is a major misuse of the design pattern. There is no one-step design model. The "refactoring to patterns (reconstruction to pattern)" advocated by agile software development practices is the best method to use the design pattern.

Several more specific design principles

Single Responsibility Principle (SRP)

A class should have only one reason for its change.

OCP)

Class modules should be extensible, but cannot be modified (open to extensions and closed to changes)

Liskov replacement principle (LSP)

Subclass must be able to replace their base class

Dependency inversion principle (DIP)

The high-level module should not depend on the underlying module, and the two should depend on abstraction.

Abstraction should not depend on implementation details. Implementation Details should depend on abstraction.

Interface isolation principle (ISP)

Customers should not be forced to rely on methods they do not need.

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.