"Java and mode" learning notes for object design principles

zhaozj2021-02-16  48

"Java and mode" learning notes for object design principles

Object-oriented cornerstone is "open-closed" principle.

The principle of "opening a closed" is: A software entity should be open to the extension and turn it off for modification.

This rule says that when designing a module, this module should be expanded without modification.

From another perspective, it is the so-called "principle of variable package". "The Principle of Variable Package" means two points:

1. A variability should not be scattered in many corners of the code, but should be encapsulated into an object. Different representations of the same variability means the specific subclass of the same inheritance level structure.

2. A variability should not be mixed with another variability. That is, the inheritance structure of the class map generally should not exceed two layers.

It is not an easy thing to do "open-closed" principles, but there are many rules that can be followed, and these rules are also design principles, which are tools for achieving the principle of open - closed principles.

Rivien replacement principle

Ri Shi MRS: If the object O1 of each type T1 is T2, there is a type T2 object O2, so that all programs definitions defined in T1 are replaced with O2 in all objects O1, the behavior of program P does not change. Then, the type T2 is the subtype of T1.

That is, if a software entity is used by the base class, it must also be applied to the subclass. But the replacement of the contrary is not true.

If there are two specific classes A and B violates the Riviera Significance Principle, you can choose one of the following two reconstruction schemes:

1 Create a new abstract class C, as a superclass of the two specific classes, moves A and B actions to C, thereby resolving problems that are incompletely consistent with the A and B behaviors.

2 Record from the inheritance relationship from B to A as delegate relationships.

Relying on the principle of reverse

Relying on reverse princumber is: To rely on abstraction, don't rely on specific. That is, programming is programmed for the interface. The interface programming is that the type declaration of the variable should be used to use the interface and abstract class, the type declaration of the parameters, the return type declaration of the method, and the conversion of the data type. Don't implement programming, it is to say that the type of variable should not be used, and the type of parametry declaration, the method of return type declaration, and the conversion of data types.

Although it is strong, it is not easy to implement, because depending on the reversal, the creation of the object is likely to use the object factory to avoid direct reference to the specific class, this principle uses the use of a large number of classes. Maintaining such systems requires better object-oriented design knowledge.

In addition, relying on reverse princumber assumes that all specific classes are changing, which is not always correct. There are some specific classes that may be quite stable. If they do not change, the client consumed this specific class instance can be fully dependent on this specific class.

Interface isolation principle

The principle of interface isolation is to use: Use multiple special interfaces than using a single interface. From the customer's perspective: A class of dependencies on another class should be based on the smallest interface. If the client only needs some way, then the method of these needs should be provided to the client, instead of providing unwanted methods. Providing an interface means that it is committed to the client, too much commitment will cause unnecessary burden to the system's maintenance.

Synthesis, polymerization

Synthesis, the principle of aggregation is to use some existing objects in a new object, making it a new object, and new objects reach the purpose of reaching the delegation to these objects. This principle has a short description: try to use synthesis, aggregate, try not to use inheritance.

Synthesis, polymerization has the benefit:

The only way to access component objects in new objects is through the interface of the component object.

This multiplex is a black box multiplex because the internal details of the ingredient object are not seen.

Such multiplexing can be dynamically performed during runtime, and new objects can dynamically reference the same object as the ingredient object type.

Synthesis, aggregation can be applied to any environment, while inheritance can only be applied to some limited environments.

A common cause of the use of the incorrect and inheritance is the error of the "HAS-A" relationship as the "IS-A" relationship. If the two classes are "HAS-A" relationships, they should be used, aggregated, if it is "IS-A" relationship, then inheritable. Dimit Block

Dimitfa is that an object should have as little as possible for other objects. That is, I only communicate with your direct friends, don't talk to strangers. If you need to talk to a stranger, and your friend and strangers are friends, then you can forward your call by your friends, so that someone only knows friends, don't know strangers. In other words, someone will think that he calls a friend's method.

The following conditions are called friends:

The current object itself.

Passing the object in the current object method in the form of a parameter.

The object of the current object instance variable is directly referenced.

If an instance variable of the current object is a gathering, then the elements in the concentration are also friends.

Objects created by the current object.

Any object, if one of the above conditions is to be a friend of the current object, otherwise it is a stranger.

The main intention of the Dimit Block is the overload of the control information, and should pay attention to the following points in the system design:

On the classification, you should create a weak coupled class. The weaker the coupling between the class is more conducive to multiplexing.

In the structure design of the class, each class should minimize the access rights of the member. A class should not be public its own properties, but to provide the value and assignment method to allow the outside world to access its own properties.

In the design, as long as it is possible, a class should be designed to be invariant.

On a reference to other objects, a reference to other objects should be minimized.

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

New Post(0)