Ood Principles

xiaoxiao2021-03-06  122

Object-oriented design principle summary (collection)

The Principles of Class Design:

SRP (Single Responsibility Principle) Single Responsibilities Principle: A class should have only one responsibility. The so-called responsibility is to guide the cause of this class. If a class has more than one duty, there will be several different reasons to cause this type of change, in fact, it is coupled to each other, not related responsibilities. The cohesiveness of this class will be reduced.

OCP (Open - Colse Princi) Open-Closed Principle: Software Entity (Class, Module, Function, etc.) should be able to quickly expand the module, but cannot be modified.

The modules that follow the OCP principles have two main features. They are:

1. "Open for Extension"

This means that the behavior of the module can be expanded. When the demand change of the application is, we can extend to make new behaviors that meet which changes. In other words, we can change the functionality of the module.

2. "CLOSE for MODIFICATION".

When the module is expanded, the source code or binary code of the module is not necessary. Module's binary code executable, no matter how you can connect, DLL or Java .jar files do not need to be changed.

The mechanism behind OCP is mainly abstraction and polymorphism.

LSP (Lisk Substitution Princi) Lisk (Subtype) must be able to replace their base type (Base Type). If the object O1 of each type S is the object O2 O2, the program P behavior is not changed in all program p written for T, after the O1 is replaced, the program P behavior is constant, then S is the subtype of t .

DIP (Dependency-Inversion Principles) Relying on Invert Principles: 1. High-level modules should not depend on low-level modules. Both should depend on abstraction. 2. Abstraction should not depend on the details, and the details should depend on abstraction.

Using DIP:

Any variable should not hold a pointer or reference to a specific class.

Any class should not be derived from the specific class.

Any method should not write the already implemented method in its base class.

ISP (Interface SegRegation Principles) Interface Isolation Principle: Do not force customers to rely on how they don't need. The interface belongs to the customer and does not belong to the class hierarchy.

These client programs are facing changes due to changes in these unused methods depending on the methods they do not use. This is inadvertently leading to coupling between all customer programs. In other words, if a client depends on a class that contains the way it is not used, other clients should use this method, then this client will affect this client when other customers require this class. We want to avoid this coupling as much as possible, so we want to separate the interface.

The Principles of Package Cohesion:

REP (Reuse Release Equivalence Princi) Reuse publishing equivalence Principle: Re-use granularity is the issuance of particle size.

A Granule of Reuse is as large as the Granule of Reuse. Everything we reuse must be published and tracked at the same time. If the software in a package is used to reuse, then it can no longer contain software not designed to reuse purposes. Software in a package is either reusable or not reuse.

CRP (Common Reuse Princi) co-reused principles:

All classes in a package should be commonly reused. If you reuse a class in the package, you have to reusable all classes in the package. Classes that do not practice closely to each other should not be placed in the same package.

CCP (Common Closure Princi) shared principles:

All classes in the package should be commonly closed for changes in the same type of nature. A variation will have an impact on a package, which will have an impact on all classes in the package, and no impact on other packets. If there is a very tight binding relationship between the two classes, whether it is physically or conceptually, they will always change together, so they should belong to the same package. This will reduce the workload of software, re-authentication, and re-release the workload.

The Principles of Package Coupling:

ADP (Acyclic Dependencies Princi) ringless dependence: The ring is not allowed in the redeemed relationship diagram of the package. There is a loop, in fact, the brackets in the ring become a big bag. This loop makes the isolation of the module very difficult; unit testing and release become very difficult and easy to errors; the construction of the package is not appropriate.

Release the dependency ring: Use the DIP principle, depend on abstraction; or recreate the package you want to rely.

SDP (Stable Dependencies Princi) Stable Dependency Principle: During the stable direction.

For any package, if it is variable, it should not let a difficult package depend on it! Otherwise, the variable package will also be difficult to change.

Calculate the position stability of the package:

(CA) AffERENT Coupling: Refers to the number of classes of the package and depends on the class of classes within the package.

(CE) EFFERENT Coupling: Refers to the number of classes in the package and depends on the class outside the package.

(Instability i)

I = CE / (CA CE)

SDP specifies that the I measure value of a packet should be greater than the measure value of the I it depends on (that is, the I metric should be reducing the direction of dependent).

SAP (Stable Abstractions Princi) Stable Abstract Principle: The abstraction of the package should be consistent with its stability. This principle links the stability and abstraction of the package. It stipulates that a stable package should be abstract, so its stability makes it flexibly. On the other hand, an unstable package should be concrete because its instability makes it easy to modify the specific code in its inside.

转载请注明原文地址:https://www.9cbs.com/read-99961.html

New Post(0)