New people talking about the cognition of Java interface in actual development

Source: Internet
Author: User
Tags stub
talk about the cognitive--1 of the interface. Define the development of the standard, 2. Continuation of the code later maintenance and extensibility.

1. First of all, I am in the Department of a project development, the understanding of the standard, a block of business after the analysis of the group discussion, by my position as leader, my first development task is to discuss the completion of the business of a background function point of collation, set out a background of our development interface documents, and then according to the interface document, Unified Standard to build a business interface class, in the Class I will all the functions have defined the abstract method, and then the division of the team to achieve. This has several advantages: 1: The name of the normative, to eliminate the team members according to their own style of class name and method name. 2: Improve the developer of the search and positioning of the code efficiency (if not interface, just the function directly to the team members to achieve, we build a class to build their own methods for the camp). 3: Does not appear which method does not realize (eliminates the team member forgets realizes the function point or the lazy tricks crosses the function point).

2. With regard to maintainability and extensibility, the following examples illustrate:
Simulate an actual development of the code scenario, there is a businessservice generic class, which defines a query () method, this method provides an inquiry service, there is a client class three business methods are used in the Businessservice query () method.
Write code:

public class Businessservice {public
    void query () {
        System.out.println ("Execute Query Business");
    }

    public void NewQuery () {
        System.out.println ("Execute new query Business");
    }
public class TestClient {
    businessservice bs = new Businessservice ();

    public static void Main (string[] args) {
        testclient TC = new TestClient ();
        Tc.getstring1 ();
        Tc.getstring2 ();
        Tc.getstring3 ();
    }

    public void GetString1 () {
        bs.query ();
    }

    public void GetString2 () {
        bs.query ();
    }

    public void GetString3 () {
        bs.query ();
    }

}

The above code has no problem in terms of functionality, if a business department changes the requirements one day, causing the query () method to be modified, the developer takes two scenarios for code such as, 1: Modify the original query () method directly (no one does), 2: Rewrite a newquery () method to meet new requirements. So do this. The three business methods in the TestClient class above will also be modified with the invocation of the method, changing the original Bs.query () to Bs.newquery (). As follows:

public void GetString1 () {
//      bs.query ();
        Bs.newquery ();
    }

    public void GetString2 () {
//      bs.query ();
        Bs.newquery ();
    }

    public void GetString3 () {
//      bs.query ();
        Bs.newquery ();
    }

Just imagine, in actual development, the TestClient class may have dozens of business methods in the Bs.query () of the call, which reflects the code maintenance and scalability is relatively poor.

Now replace the original business with an interface code scenario.

Define Businessservice Interface: Public
interface Businessservice {public
    void query ();
}
Businessserviceimpl implementation interface: Public
class Businessserviceimpl implements businessservice{public
    void query ( {
        //TODO auto-generated method stub
        System.out.println ("Execute Query Business");
    }

Like the beginning, the query () method is used in three business methods in the TestClient2 customer class:

public class TestClient2 {
    businessservice bs = new Businessserviceimpl ();
    public static void Main (string[] args) {
        testclient TC = new TestClient ();
        Tc.getstring1 ();
        Tc.getstring2 ();
        Tc.getstring3 ();
    }

    public void GetString1 () {
        bs.query ();
    }

    public void GetString2 () {
        bs.query ();
    }

    public void GetString3 () {
        bs.query ();
    }
}

The same day, the business unit changed the requirements, causing the query () method to be modified, so for example, we take a Newbusinessserviceimpl class and redefine the query () method in this class. Newbusinessserviceimpl implements the interface.

public class Newbusinessserviceimpl implements businessservice{public
    void query () {
        //TODO auto-generated Method stub
        System.out.println ("Execute new query Business");
    }

In the TestClient2 customer class, you only need to modify one line of code to businessservice bs = new Businessserviceimpl (); replace businessservice bs = new Newbusinessserviceimpl (), there are many business methods in the customer class that call the query () method, and we only need to modify one line of code to solve the business change problem, from the maintenance and extensibility point of view, very worthwhile.

Summary: Working time 1.5, only to understand the experience of a collation and sharing, if there is a wrong place to see the great God can correct ....

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.