Java Design First principle: interface-oriented programming __ algorithm

Source: Internet
Author: User
1. Case Studies

For example, you would register a user, preceded by a business logic, to invoke the Save (user) method of the data access layer. First write a data Access object's interface

Java Codepublic interface idao{ 
    void Save (user user) throws usernameexistexception; 
The implementation of JDBC is
Java Codepublic class Jdbcdao implements idao{public 
    void Save (user user) throws usernameexistexception{ 
        ... c5/>string sql = "INSERT into T_user values (?,?,?,?)"; 
        .... 
        PreparedStatement pstm = conn.preparestatement (sql); 
        Pstm.setstring (1,user.getname ()); 
        .... 
        Pstm.executeupdate (); 
        ...... 
    } 
The realization of Hibernate is
Java Codepublic class Hibernatedao implements idao{public 
    void Save (user user) throws usernameexistexception{ 
        . .... 
        Session.save (user); 
        Session.gettransaction (). commit (); 
        .... 
    } 
Your business layer UserService just write that.
Java Codepublic class userservice{public 
    void Register (String username,string password,int age,.... And so on) { 
        //assuming that the implementation of JDBC is now 
        Idao dao = new Jdbcdao ();//When you need to replace the implementation, just change the Jdbcdao to Hibernatedao, and no other code needs to be changed. 
        User user = new user (); 
        User.setname (userName); 
        ...... 
        Dao.save (user); 
    } 
2. Detailed Description meaning in the project:
In the traditional project development process, because the customer's requirements often change, if not using interface-oriented programming, then we must constantly rewrite the existing business code. Rewriting the code can create new bugs, and rewriting the code can also affect the classes that invoke the business, all of which may need to be modified to affect the stability of the system itself. and in order to minimize the impact of rewriting code, we have to succumb to the current system situation to complete the design, lower code quality and stability. When this situation accumulates to a certain extent, the system will appear unpredictable errors, code messy, difficult to read, after the people who can not read the code, the maintenance of the system more and more heavy, and ultimately may lead to project failure.
Interface in the project is a business logic, interface programming is the first customer's business to extract, as an interface. The implementation of this interface is accomplished by the realization class of the implementation of the business. When customer requirements change, just write a new implementation class for the business logic, and you can accomplish the requirements by changing the implementation class of the interface in the configuration file (such as the spring framework) without overwriting the existing code and reducing the impact on the system. The project based on interface programming, the business logic is clear, code easy to understand, easy to expand, maintainability is strong. Even with the replacement of a group of people, the new arrivals can still be quick. It means a lot to the company.
meaning in Java:
Java itself is also a continuous improvement of the language, he is also frequent changes in his system API to improve, his API is a large system, interrelated, if not using the interface, but are implemented classes, then the API changes will bring instability to the entire system. And if you change the API, there will be a large number of projects that use the old API because they are not working properly and will lose a large number of customers. In other words, the API that the JDK has published is a promise that cannot be changed once it is published. Even if the original API has a variety of problems (for example, the Java.util.Properties class is a failed example), it must be preserved, so there is a method that is not recommended in Java, but the JDK still provides the method. And the Java language itself is a cross-platform language, in order to meet under each platform to run, it must be a variety of operations into the interface, in the preparation of the implementation of the various platform classes.
the embodiment of design pattern:
In the principle of design mode, the principle of opening and closing, in fact, is to use the interface to achieve the expansion of open, to modify the closure. In other principles of design patterns There is also the principle of interfacing programming, that is, the design principle of relying on the reverse (DIP)----High-level modules should not be dependent on the underlying modules. Both should rely on abstraction; abstractions should not depend on detail, and details should depend on abstraction (note: from "Agile software Development-principles, patterns and practices" Robert c.martin). In the process of using interface-oriented programming, separate the specific logic from the implementation, reducing the interdependence between the various classes, when the various classes change, do not need to make changes to the system has been written, add a new implementation class can be, not worry about the new changes in the class on the system's other modules caused by the impact.
An interface is essentially a relationship between the creator and the caller by the creator.
So the usual "interface-oriented programming" can be understood as:
Only the implementation and the caller follow the guideline of "interface-oriented programming", and the coordination purpose of the formulation can be achieved.
A cliché example is JDBC.
Advantages:
Interfaces and implementations are decoupled and are suitable for team collaborative development.
Mainly for the implementation of loosely coupled system, easy to upgrade later, extended. Disadvantages:
Design is difficult, when you do not write implementation, you have to think good interface, interface change, all out of chaos, this is called design than implementation difficult.
So the design of the interface of the people pay high ah ...

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.