Benefits of interface-oriented design

Source: Internet
Author: User

The textbook has long told us what the benefits of interface-oriented are. But how can we deeply understand the benefits of interface-oriented?

Here I will give you an example of Dubbo, which will show you the benefits of interface-oriented.

First, define the interface

 package com.alibaba.dubbo.demo;public interface DemoService {     String sayHello(String name); }

Then define the implementation of interace

package com.alibaba.dubbo.demo.provider;public class DemoServiceImpl implements DemoService {     public String sayHello(String name) {        return "Hello " + name;    } }

As a service provider, a configuration file is required.

Provider. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: Dubbo = "http://code.alibabatech.com/schema/dubbo" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! -- Provider application information for dependency calculation --> <Dubbo: Application name = "hello-world-app"/> <! -- Use the multicast broadcast registry to expose the service address --> <Dubbo: Registry address = "Multicast: // 224.5.6.7: 1234"/> <! -- Expose services on port 20880 using Dubbo protocol --> <Dubbo: protocol name = "Dubbo" Port = "20880"/> <! -- Declare the service interface to be exposed --> <Dubbo: Service Interface = "com. Alibaba. Dubbo. Demo. demoservice" ref = "demoservice"/> <! -- Implement services like local beans --> <bean id = "demoservice" class = "com. Alibaba. Dubbo. Demo. provider. demoserviceimpl"/> </beans>

As a service caller, a configuration file too is required,Consumer. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: Dubbo = "http://code.alibabatech.com/schema/dubbo" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <! -- Consumer Application name, used to calculate dependencies. It is not a matching condition. Do not use the same name as the provider --> <Dubbo: Application name = "consumer-of-helloworld-app"/> <! -- Use the multicast broadcast registration center to expose the Discovery Service address --> <Dubbo: Registry address = "Multicast: // 224.5.6.7: 1234"/> <! -- Generate a remote service proxy and use demoservice --> <Dubbo: Reference id = "demoservice" interface = "com. alibaba. dubbo. demo. demoservice "/> </beans>

As a service provider, you need to start a service. Here we use the main function to implement it.

Import Org. springframework. context. support. classpathxmlapplicationcontext; public class provider {public static void main (string [] ARGs) throws exception {classpathxmlapplicationcontext context = new classpathxmlapplicationcontext (New String [] {"http: // 10.20.160.198/wiki/display/Dubbo/provider. XML "}); context. start (); system. in. read (); // press any key to exit }}

As the service caller, the call code is as follows:

Import Org. springframework. context. support. classpathxmlapplicationcontext; import COM. alibaba. dubbo. demo. demoservice; public class Consumer {public static void main (string [] ARGs) throws exception {classpathxmlapplicationcontext context = new classpathxmlapplicationcontext (New String [] {"http: // 10.20.160.198/wiki/display/Dubbo/consumer. XML "}); context. start (); demoservice = (demoservice) context. getbean ("demoservice"); // obtain the remote service proxy string Hello = demoservice. sayhello ("world"); // execute the remote method system. out. println (Hello); // display the call result }}

Now the focus is:

The advantage of interface-oriented is that it separates the definition and implementation of functions. In this case, you can create an interface. jar package for the interface, which is then in the Lib of the client and the call segment respectively. When calling, you only need to follow the bean's local method call. This is the benefit of interface-oriented.


Benefits of interface-oriented design

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.