Java Object-Oriented design principles

Source: Internet
Author: User
Tags closure

One

1, the core of object-oriented thinking:

  Encapsulation, inheritance, polymorphism.

2, the pursuit of object-oriented programming:

Cohesion-poly low-coupling solution;

Modular design of the code;

3, what is the design mode:

The classic solution for recurring problems is a summary of the experience of the design of a specific condition (context) problem, which is the essence of previous experience in design practice.

4. Object-Oriented design principles

It is the refinement of object-oriented design (the basic Constitution), which is more practical than the core elements of object-oriented thought, which is more abstract than the design pattern (various specific legal provisions).

5. How to minimize the coupling degree?

    1. The inheritance of the class is reduced, and the implementation details are hidden by the multi-interface.
    2. Avoid using global variables.
    3. Multi-use design mode. such as mvc~
    4. Try not to write the program "hard" code, try to avoid using SQL statements to manipulate the database directly.
    5. Avoids direct manipulation or calls to other modules or classes to minimize coupling

7, object-oriented common design principles: SOLID

    1. Single principle of responsibility;
    2. The principle of open closure;
    3. Liskov substitution principle;
    4. interface isolation principle;
    5. dependency inversion principle;
    6. Dimitri Law;
    7. Synthesis/polymerization reuse principle;

Two

1. Minimize access rights :

From the point of view of information hiding, the class should be encapsulated to the maximum extent possible, as long as the method and property members are defined as private, and then the gradual increase of permissions is considered.

  Defensive (defensive) programming: The embodiment of defensive design, in order to ensure that the unpredictable use of the program will not cause the failure of the program function.

  How can I protect a program from illegal data?

    1. Check data values from external resources, such as data values from the network and data values from the file;
    2. Check the values of all input parameters of the subroutine, similar to the above;
    3. Decide how to handle the wrong input data and handle the different types of errors.

2. Replace the basic data type with a class

If there are more than one basic data type associated with each other in a class, it should be extracted as a class.

3. Single Duty

Meaning: A class only involves the implementation of a duty, there is only one cause of change.

The core task of object-oriented design is to discover responsibilities and separate these responsibilities from each other. If you can think of an extra motive to change a class, then this class has one more responsibility and should consider decomposition.

  

4. do not create wheels repeatedly

Definition: Do not write duplicate code (for functionality, not code), and use the abstraction class to abstract public things.

If a hard-coded value is used more than once, it is set to a public constant;

If you need to use a block of code in more than two places, you can extract it as a separate method.

You cannot misuse the merging of feature codes!

  

5. Open Closure principle (OCP)

Definition: Software entities should be open to extensions and closed to modifications.

Opening up to extensions means that existing code can be expanded to accommodate new situations when new requirements change.

The closure of modifications means that once the design is complete, do not modify the implementation of the class.

  How do you do that?

Core idea: For abstract programming, not for specific programming.

Programming Angle:

Programming for interfaces, not for implementation.

You should use the interface type as the method return type, the method parameter type.

6. The Richter replacement principle (LSP)

  Definition: Where any base class can appear, subclasses must be able to appear.

The parent class can be reused only if the child class is able to replace the parent class, and the child class can also add new behavior based on the parent class, if the functionality of the Software Unit is not affected.

If the subclass does not fully represent the parent class, it is recommended to break the parent-child inheritance relationship and replace it with a combination.

  7. Dependency inversion principle (DIP)

Definition: Programming depends on abstraction, not on specifics. That is, to abstract programming, do not program the implementation, reduce the call class and the implementation of the coupling between the module.

High-level modules should not be dependent on the lower levels of the module, should be dependent on abstraction;

Abstractions should not be dependent on specifics, but should be dependent on abstraction.

8. Dimitri Law

Meaning: Also known as the least knowledge principle, that an object should know as little as possible about other objects.

  Objective:

Reduce the coupling between classes, so that each class minimizes its reliance on other classes.

However, this can also result in the existence of a large number of intermediary classes in the system, bloated.

Java Object-Oriented design principles

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.