Learn the object-oriented design principles

Source: Internet
Author: User
ArticleDirectory
    • About the class design

Today is the popular era of OO language. But can we use oo features to develop software or develop a pure software system? I think most people do not dare to stand up and be sure they are involved in the development of a pure OO system! (However, technology is used for demand, and sometimes it may be difficult to use OO thinking for a moment ).

But in any case, today's software development emphasizes oo, so can we really master the basic knowledge of OO? In fact, the people who have mastered it, in the real software development, still cannot use OO to consider how to solve the problem in the first time? It may all be our actionsProgramThe Thinking of personnel must be limited. It is to consider how to solve a problem (or demand) with a general process-oriented approach ). I still remember from the book "Design Pattern Analysis", the author mentioned that we should learn to think with patterns! This has to cause our meditation! I think we can't be too impetuous as a programmer, but what can we do in today's society? Forget it. Let's take a look at the most basic principles in our Ood! If you study hard, you can review it for yourself and purify your impetuous mind!

[Introduction] in object-oriented design, how to deal with changes in design requirements through small design changes is a matter of great concern to designers. To this end, many oo pioneers have proposed many object-oriented design principles to guide the design and development of OO. Of course, here we should not forget the three main features of OO, namely abstraction, encapsulation, and polymorphism. Leaning
The author of design patterns by looking at code said that 85% of software development is interface-oriented (Abstract-oriented) and multi-state programming!

    1. Let's first look at the design principles in Object-Oriented Systems, follow:

      About the class design

      :

      • OCP
        ,Open and closed Principle
        (Open Close
        Principle), software entities (classes, modules, and functions) should be open in terms of scalability and closed in terms of modification. The interface encapsulation mechanism, abstract mechanism, and polymorphism technology should be considered in object-oriented design. This principle is also suitable for non-object-oriented design, and is a software engineering
        One of the important principles of the design method.
      • Let's use some examples to describe them.
      • This is an example that violates the OCP principle. Every time we add a graph, we need to modify it.Code.
  • // Open-Close principle-bad example
  • Class graphiceditor {
    • Public void drawshape (shape s ){
  • If (S. m_type = 1)
  • Drawrectangle (s );
  • Else if (S. m_type = 2)
  • Drawcircle (s );
  • }
  • Public void drawcircle (circle R ){....}
  • Public void drawrectangle (rectangle R ){....}
  • }
  • Class shape {
  • Int m_type;
  • }
  • Class rectangle extends shape {
  • Rectangle (){
  • Super. m_type = 1;
  • }
  • }
  • Class circle extends shape {
  • Circle (){
  • Super. m_type = 2;
  • }
  • }

The following Disadvantages:
Each time a graph is added, a unit test must be redone for graphiceditor.

When a new graph is added, developers are required to understand the program logic in graphiceditor.

Adding a new graph affects the existing disadvantages of the program in a way we don't expect, even if the newly added graph can run well.

Let's look at the OCP principle to redesign the above example.

// Open-Close principle-good example
Class graphiceditor {
Public
Void drawshape (shape s ){
S. Draw ();
}
}

Class shape {

Abstract void draw ();
}

Class rectangle extends shape {

Public void draw (){
// Draw the rectangle
}
}

    1. SRP
      ,Single Responsibility Principle
      (Single responsiblity
      Principle), for a class, there should be only one reason for its change.
    2. LSP
      ,Liskov replacement principle
      Subclass should be able to replace the parent class and appear anywhere the parent class can appear.
    3. Dip
      ,Dependency inversion principle
      (Dependency inversion
      Principle), the dependency relationship should be dependent on the interface and the abstract class (that is, the dependency abstraction) as much as possible, rather than the specific class.
    4. ISP
      ,Interface isolation principle
      (Interface segregation
      Principle), using multiple interfaces related to a specific customer class is better than using a common interface that covers multiple business methods.

      The other six principles are about the design principles of packages. Here, a package refers to a binary publishable file, such as a jar file or a DLL file. It is not the same as the package or C ++ namespace in Java.

About package cohesion:
    • Rep, reuse release Equivalence Principle
      The granularity of reuse is the publishing granularity.
    • CPP: Principle of Joint Closure
      , All classes in the package should be closed for changes to the same class nature.
    • CRP, principle of joint Reuse
      , All classes in a package should be reused together.
Regarding the coupling principle between packages:
    • ADP: the principle of no-ring dependency. A ring is not allowed in the package dependency graph.
    • SDP, stable dependency principle, dependency in a stable direction.
    • Sap: the principle of stable abstraction, the degree of abstraction of the package should be consistent with its degree of stability.

In the above Ood principles, the common development principles should be the class design principles described above. In the process of object-oriented software development, of course, we cannot always talk about these principles. What is important is to remember to use them as much as possible during the development process. However, we should always check whether our Code conforms to the Basic Principles. in software development, the most important thing is how to establish "loose coupling ".

Code and image source from http://www.oodesign.com/
], There are other content from the Internet. In short, thank you for sharing your knowledge!

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.