The principles of----pattern design of reading notes in Java and pattern

Source: Internet
Author: User
Tags abstract variable access
Notes | design
The principle of the Richter substitution----LSP











Concept: If a software entity uses a parent class, it must apply to it, and it cannot perceive the difference between the parent class and the subclass.











The principle of the Richter substitution is the cornerstone of succession reuse. Only when the derived class can replace the base class, the software unit is not affected, the underlying class is actually reused, and the derived class can add new behavior to the underlying class.











The model of Java is not only the embodiment of the principle of the Richter substitution.











Dependency Reversal Principle-----DIP























A narrow reliance relationship.











A connection between a dependency (Dependency) class and a class, and dependencies are always one-way. One category depends on the definition of another. A person can buy a car and a house, and the people class relies on cars and houses.











Generally, dependencies are embodied in Java as local variables, method parameters, and calls to static methods. In other words, the type of a local variable of a Class A is another Class B, then class A depends on class B; if the argument of one method is an instance of another class B, then Class A relies on class B; If Class A calls static methods of Class B, Class A relies on Class B.











Three kinds of dependencies in a broad sense. Here the dependence is also called coupling











L 0 Coupling relationship (Nil coupling). Two classes do not have a coupling relationship.











L Specific coupling relationship (concrete coupling). The coupling relationship of specificity occurs between two specific classes (which can be instantiated) and is directly applied to another class through a variety of categories.











L Abstract Coupling relationship (abstraction coupling). Abstract coupling occurs between a specific class and an abstract class (or interface), giving the greatest flexibility between the two classes that must occur.













Dependency reversal principle meaning: Abstraction should not depend on detail, detail should be dependent on abstraction.











The dependency reversal principle requires that you want to program for the interface, and not for the implementation.











Programming for interfaces means that you should use Java interfaces and abstract classes to declare variable types, declarations of parameter types, declarations of return types, conversions of data types, and so on.











To ensure this: a specific class should implement only the methods declared in the interfaces and abstract classes, not the redundant methods.











List employees = new Vector ();

Where: List is called variable employees static type.











The actual type of vector called variable employees.











In many cases, a Java program needs to refer to an object. This time, if this object is another abstract type, you should use the abstract type of the object as the static type of the variable. ------This is the specific meaning of programming for interfaces.























Interface Isolation principle----------ISP











Meaning: Using multiple specialized interfaces is better than using a single total interface!











From the customer class perspective: The dependency of a class on another class should be based on the smallest interface.











The division of the interface directly brings the classification of the type.











The goal is not to provide unnecessary behavior to the customer class.











The principle of synthetic polymerization reuse--------CARP











Association, aggregation, synthesis.











Synthesis and aggregation are special types of associations. Aggregation means "owning" a relationship or the relationship between a whole and a part, while synthesis is used to represent a strong and much "possessed". In a synthetic relationship, the part and the whole life cycle are the same. A synthesized new object is fully used and has a dominant power over other components. including their creation and annihilation. The grouped objects are absolutely responsible for the memory allocation and memory release of the components.











Further speaking. The multiplicity of a synthesis cannot exceed 1, in other words, in a synthesized relationship, some objects cannot be shared by another composite relational object.











Synthesis is generally understood to be: the aggregation of values. Aggregations are: the aggregation of references.











Synthesis and aggregation is the incorporation of an existing object into a new object, making it part of the new object, so that new objects can invoke the functionality of the object. Achieve the purpose of reuse.

Advantages:











L The only way for a new object to access the Component object is through the interface of the Component object.











L This reuse is a black box multiplexing, which hides the internal details of the component objects.











• This reusable support package.











This multiplexing dependency is small. (Low coupling degree)











• Each new class can focus on one task.











L This reuse can be performed dynamically at runtime. New objects can be dynamically referenced to child objects of the constituent object type.











Disadvantages are:











Systems built through this reuse usually have more objects to manage.











"Synthetic aggregation multiplexing can replace inherited multiplexing to accomplish any one function." ”











Although inheritance is a very important means, however, synthetic aggregations should be considered first.























Dimitri Law------LoD











Also called the minimum Knowledge Principle: An object should have as little understanding of other objects as possible.











Several representations:











L As far as your direct friends are communicating.











L Don't talk to "strangers".











• Each software unit uses as little knowledge as possible for other units and is limited to software units that are closely related to the unit.











About the "friend" agreement:











L The current object itself (this).











The object passed in as a parameter to the current object method.











L The object directly referenced by the instance variable of the current object.











L An instance variable of the current object if it is a clustered then the element is also "friend".











L The object created by the current object.











Generalized Dimitri Law























In the software system, one of the main and most important signs of a modular design is the extent to which the module hides its internal data and other implementation details. A well-designed module hides all of its implementation details, completely separating the API provided to the outside world and its own implementation. This means that the communication between modules is only through each other's APIs, ignoring the details of the work inside the module. This is: "Information hiding"-"encapsulation".























Generalized Dimitri rules require:











L A weakly coupled class should be created on the division of the class. The lower the coupling between classes, the more beneficial to reuse.











L in the design structure of a class, each class should reduce the access rights of members as much as possible.











L in the design of classes, whenever possible a class should be designed as invariant classes. (final).











L The references to other classes should be minimized for references to other objects.


















































Related Article

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.