Six basic principles of Java Development design

Source: Internet
Author: User

1. Follow the principle of single responsibility


A class only focuses on doing one thing;
Cohesion poly, low coupling;

2. Open-Close principle


Open to the expansion, the modification is closed (as far as possible to expand the original code);
Cohesion poly, low coupling;
Abstract design of the system (key) is necessary to achieve the goal;
UML Examples:

3. The Richter substitution principle (LSP)


Subclasses must be able to replace their base type, the base class is interchangeable with the subclass, and the client is unaware of the situation;
Low coupling;
It is easy to implement the interchange of subclasses under the same parent class, and the client can be unaware;

UML instances:

Soldiers can only use Weapongun and cannot use Toygun to kill, and if Toygun will go wrong. This is the principle of the Richter replacement.

4. Dependency Inversion principle


This is the basis of the open and close principle, the specific content: interface-oriented programming, dependent on the abstract and not dependent on the specific. When writing code, a specific class is used, not interacting with a specific class, but interacting with the upper interface of a specific class.

5. Interface Isolation principle (Interface segregation Principle)


It is better to use multiple-specificity interfaces than to use a total interface, but not to transition;
An interface cannot be too bloated;
High cohesion
When the functionality of a software system expands, the pressure of the changes will not affect other objects that go;
UML instances:

Model can become a model as long as it meets the condition of good stature, and the beautiful girl meets the good figure and good. Angelababy is a good figure and good.

6. Dimitri (Law of Demeter)


Also called the least knowledge principle;
Objects and objects should be associated with as few methods as possible, avoiding the intricate relationships;
Low coupling;
Class knows that other classes should be as few as possible;
Classes can access other classes of methods or properties should also be as few as possible;

If Lily and Jack do their own Hamburg then it will be associated with vegetable, meat, bread. , it is made by KFC, so Lily and Jack are only associated with Hamburg, and the material is not connected.

Six basic principles of Java Development design

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.