The Five Principles of Java

Source: Internet
Author: User

1. Single Duty

Whether in the design class, interface or method, a single responsibility is everywhere, the definition of a single responsibility: we define responsibility as the cause of the system change. All at the time of defining classes, interfaces, methods. After the definition is finished, think about it, there is no more than one motive to change this class, interface, method. If the answer is yes, the description defines the class, interface, and method with more than one responsibility. In the event of a breach of a single duty, this situation should be subdivided into duties until there are no classes of responsibilities, interface methods (discovering responsibilities and separating those responsibilities from each other). Single duty is one of the simplest five principles. In the process of software design is reflected everywhere. Everywhere.

2. Opening and closing principle

The open and closed principle means that classes, modules, and methods can be extended, but not modified. Open to expansion, closed to modify the closed. Application of open and closed principle now, developers should simply abstract (encapsulate the point of change) where changes occur frequently in the program. The change-point encapsulation is the modification of the change is closed. The uncertainty of change can be extended at any time. That is, the use of inheritance. The use of abstract classes.

3. Replacement principle (IS-A)

The substitution principle is always to guarantee that a subclass can replace its base class. The implementation of the substitution principle. For a group of classes that have similar properties, methods, and variables. We can extract public properties, methods, variables as a base class (abstract class or Class), make this group of classes inherit base classes, override virtual methods. Now the relationships of these inherited classes and base classes conform to is-A. If the base class is a bird, then the inheriting class can be a sparrow, a swallow. We can say Sparrow is-a bird, swallow is-a bird. In a project where all subclasses are used, the parent class can be substituted, but when the method is called, the object-oriented programming polymorphism is rendered. The principle of substitution, which is very important, is also the principle of comparison to difficulty. 

4. Dependency Inversion principle

A, high-level modules should not be dependent on low-layer modules. Both should be dependent on abstract B, and abstractions should not be dependent on detail. The details should depend on abstraction. In the process-oriented development language analysis and design, always create some high-level modules to invoke the low-layer module, policy-dependent software structure of the details. In fact, the purpose of this method is to define the subroutine hierarchy, which describes how the high-level module calls the lower-layer module. A well-designed object-oriented program is just "upside down" to this dependency. High-level modules are no longer dependent on low-level modules, so the modification of the lower-layer modules does not affect the high-rise modules, and the high-level modules can be easily reused, both high-level and low-rise modules are dependent on abstraction. This is also very consistent with the strong cohesive loose coupling programming idea. Therefore, this principle is also the core principle of frame design. Using the dependency structure created by the traditional procedural programming, the strategy is dependent on the details, which is bad, because it makes the policy subject to the change in detail, the object-oriented programming is inverted the dependency structure, the whole detail and strategy are dependent on abstraction, and often the client program has a service interface. In fact, the inversion of this dependency is the hallmark of good object-oriented design, and the language in which the program is written is irrelevant. If the dependencies of a program are inverted, it is an object-oriented design. If the dependencies of the program are not inverted, it is a process-based design. The dependency inversion principle is a basic low-level mechanism for realizing the benefits claimed by many object-oriented technologies. Its correct application is necessary to create a reusable framework. It is also important for building code that is resilient in the face of change, and because abstractions and details are isolated from each other, the code is very easy to maintain.

5. Interface Isolation principle

It should be said that the principle is to deal with the shortcomings of the existing "fat" interface. If the interface of a class is not cohesive, it means that the class has a "fat" interface. In other words, the "fat" interface can be broken down into multiple groups of methods. Each group of methods serves a different set of client programs. In this way, the volume citation client can use a set of member functions, while other client programs can use the member functions of other groups. There are two methods for interface isolation (sharing a client is a separate interface):1. Use a delegate (this delegate is not a. NET delegate [Delegate]) to detach an interface using delegates, you create a delegate class, This class is used to implement the methods in other interfaces after separation.  2, using multiple inheritance separation interface, this method, the existing "fat" interface is divided into two or more interfaces for different client program calls, and needs to implement multiple interfaces of the client program, then use multiple inheritance to achieve. These two methods are the whole method of implementing interface isolation, the second method is more common and simpler to use. The first method is relatively complex to use, and also produces duplicate objects in the process of using the delegate, which takes up the runtime and memory overhead. Sometimes the second method is necessary, and the first method is not available. For example, conversions made with a delegate object are required, or different conversions are required at different times. 

The Five Principles of Java

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.