Design mode (1) design principle

zhaozj2021-02-16  59

* Open-Closed Principle (Open-Closed Principle, OCP): A software entity should be developed to modify the extension. Say, when you design a module, you should make this module can be used without modification. Extensions. In other words, the behavior of this module should be changed without having to modify the source code.

* .Uml (Unified Model, Unified Modeling Language), is an Object Management Group published in 1997 Icon Software Design Language.

1. Relationship in class diagram: (1). Generalization: (generalization) Indicates the inheritance relationship between classes and classes, inheritance relationship between interfaces and interfaces, or implementation of classes to interfaces. It is the interface that points from the subclass pointing to the parent class, or from the class that implements the interface. (2). Association: (Association) is the connection between classes and classes, which makes a class know the properties of another class And method. The association can be two-way, or one-way. The two-way association can have two arrows or no arrows. One-way association has an arrow, indicating the direction of the associated, is more common, usually The bidirectional arrow is not encouraged. (3). Aggregation: (AGGREGATION) is a kind of association relationship, is a strong relationship. The polymerization is the relationship between the overall and individuals. (4). Synthetic relationship: (Composition) One of the relationships of the relationship is a more powerful relationship. It requires the general aggregation relationship to represent the overall object to be responsible for the life cycle of the object representing the object, and the synthetic relationship cannot be shared. (5). Relational relationship: (Dependence) is also the connection between classes and classes, depending on the one-way. Dependencies represent a definition of another class.

2. Sequence Diagram Sometimes it is called a sequence diagram / active sequence diagram. As an interactive diagram, the sequence intertency shows each usage case from top to the time sequence. In a timing chart, vertical dashed lines Called with lifelines, it represents an object exists. Each arrow is a call, which is connected from the caller object to the activation bar on the lifeline of the recipient object. Each activation section is continuing time.

3. State diagram, also known as a state transition diagram. The basic idea of ​​the status diagram is to define a machine with a limited internal state. So the status map is also known as a limited state machine. Object is outside The event is excited to convert from one state to another.

4. Simple Responsibility PinciPle SRP, in turn, there should be only one reason that caused it, if you can think of more than one motive to change a class, then this class has more than one Accounting. Separate more than the accusation, then create some classes to complete each account.

5. Lisk Substitution Principle (Liskov Substitution Princi) (1). Based on Barbar Liskov (Barbara. Rhin) (2). Strict expression: If each type is T1 object O1 Objects O2 with T2, so that all programs defined in T1 are replaced by all of the object O1, the behavior of program P does not change, and the type T2 is a subtype of type T1. In other words, one If a software entity is used by a base class, it must be applied to its subclass, and it does not detect the difference between the base class object and the sub-objects. (3). Reversion is not true (4). < Mozi. Small Take> said: "White horse, horse is also; Take the white horse, take the horse. Convolk (black horse), horse is also; Take the horse, multiply the horse," (5). The process is: whether the square is a rectangular subclass (the answer is "NO") (6). Try to inherit from abstraction class, without inheritance, generally, if there are two specific classes A, B has inheritance Relationship, then the simplest modification is to create an abstract class C, then let class A and b have subclass of abstract class C. That is, if there is a registration structure formed by inheritance relationship, then the tree in the level structure All leaves nodes on the figure should be specific classes; all branches nodes should be an abstract class or interface. (7). "Based on contract design, referred to as DBC" technology for Liskov restriction principles Supported. This technology bertrand Meyer Bertrand has done a detailed introduction: Use the DBC, the writer of the class explicitly specifies the contract for this class. Customer code writers can learn the way of behavior that can be rely on The contract is specified by the preconditions and postconditions declared by each method. To make a method, the pre-condition must be true. After the execution is complete, the method should ensure the post The condition is true. That is, when re-declaring the routine (ROUTINE), only the original or weaker pre-condition can be replaced with the original preamp, only the equal or stronger rear Conditions to replace the original back conditions.

6. Dependence Inversion PrinciPle, requires clients to depend on abstract coupling. (1) Expression: Abstraction should not depend on the details, the details should depend on abstraction. (Program to an interface, not an importaction) (2) Expression 2: In the means of programming, the type declaration of the variable should be used to use the interface and abstract class, the type of parameter, the type of return type declaration, and the conversion of the data type. Do not intend to implement programming, The type declaration of variables should not be used, the parameter type declaration, the return type statement of the parameter, and the conversion of the data type. To ensure this, a specific class should only implement the interface and abstract classes. The method should not give an excess method. As long as an abstract type is present, it should use abstract types in any place to reference this object, including the type declaration of the parameters, and the method of returning type, attribute variable Type declaration, etc. The interface and abstraction: (3) The difference between interfaces and abstractions is that the abstract class can provide some implementation of certain methods, and the interface is not, this is also a unique advantage of the abstraction class. If you want to an abstract class Join a new specific method, all subtypes have been obtained at once, and the interface does not do this. If you join a new method to an interface, all implemented this interface The class cannot be compiled because they have not implemented this new declaration method. This is obviously a shortcoming of the interface. (4) A abstract class is only given by this abstract class, that is, This implementation is in the registration structure of the inheritance defined by the abstraction, and because the general language limits a class that can only be inherited from the maximum of one superclass, there is a great discount of the effect as the type definition tool. In turn, see The interface will find that any class that implements the method specified in an interface can have the type of the interface, and a class can implement any multiple interfaces. (5) From the perspective of code reconstruction, will be separate The specific class re-constitutes an implementation of an interface is easy, just declares an interface and add important methods to the interface declaration, and then add a reserved word in the specific class definition statement to inherit it. As an existing concrete class, add an abstract class as an abstract type is not so easy, because this specific class may already have a superclass. So, this new definition abstract class has to continue to move up, becoming this super Class of superclars, such cyclies, and finally this new abstract class must be at the top of the entire type level structure, so that all members in the registration structure will be affected. (6) Interface is the ideal tool for defining a mixed type, The mixed type is the secondary type outside of a class of primary types. A mixture Types indicate that a class does not only have a major type of behavior, but also have other secondary behavior. (7) Joint use interface and abstract class: Due to the advantages of providing default implementation, the interface has all other advantages, So the combination is a good choice. First, the work of the declared type is still responsible, but there is also an abstract class to give this interface. Other use belongs to this abstract type The specific class can choose to implement this interface, you can also choose to inherit from this abstraction class. If a specific class directly implements this interface, it must implement all the interfaces themselves; the opposite, if it inherits the abstract class, it can save Go to some unnecessary methods because it can automatically achieve the default implementation of these methods from the abstraction class; if you need to join a new method to the interface, then just add a specific implementation to this abstract class to this method. Yes, because all subclasses from this abstract class will be given this specific method from this abstract class. This is actually default adapter. (8) What is a high-level policy? It is an abstraction behind it It is the truth that does not change the change in specific details. It is the system inside the system ____ metaphor.

7. Interface SEGREGATION PrinciPle, ISP uses multiple special interfaces to be better than using a single total interface. According to the different clients, different services provide different services to different clients is a encouragement practice. Just like "watching the people's dish", you have to see who the guests are, and then provide different grades. The fat interface will lead to abnormal and harmful coupling relationships between their client programs. When a customer program requires When the fat interface is changed, it will affect all other clients. So the client should only rely on how they actually need to call methods. 8. Composite / aggregate reuse principle, carp in a new Inside the object, use some existing objects, making it part of the new object; new objects have reached the purpose of reaching the multiplexing of these objects. This design principle has another short expression: try to use synthesis as possible / Polymerization, try not to use inheritance. 9. Law of demeter LOD is called the least principle (Least Knowledge Principle, LKP), that is, an object should have as little as possible for other objects. Dimit Block was originally used as a ruler for object-oriented system design style, with Ian Holland in Northeastern University in Northeastern University, is called Dimit. It is called Dimi. Special France [Lieb89] [Lieb86]. This law is actually a lot of famous systems, such as Mars Landing Software System, and the Software System of the Europa Satellite Orbage Software. There is no other OO design principle like Dimi It is such as the speculation that there are several ways, as follows: (1) Only communicate with your direct friends (2) Don't talk to "strangers" (Don't talk to STRANGERS) (3) Each software unit only has the least least, and is limited to software units closely related to those units. That is to say, if two classes don't have to communicate with each other, then these two classes are not Direct interactions should occur. If one of them needs to call another method of another class, you can forward this call through a third party.

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

New Post(0)