Adapter mode (structural type)

Source: Internet
Author: User

Thinking: For legacy systems, the service already exists, and the controller layer assumes that the service layer provides the add user method to add users, whereas the service layer declares the insert (user u) to add users. When the controller layer and service layer docking to find the problem, without modifying the existing code, you how to complete the docking?

Answer: There are two ways:

    1. Inheritance
    2. Combination (do not understand can continue to look down);

      First of all, we are familiar with the way of inheritance, combined with the problem, is to let an adapter class Userserviceadapter inherit the service layer implementation class Userserviceimpl, and implement the Controller layer assumed good service interface method, When the method name is inconsistent, it is only necessary to call the parent-class Insert (U) method in the implemented add (User u) method to achieve docking.

      The instance code is as follows:
      The USERSERVICEI assumes that the controller layer is set:

package com.shusheng.adapter.extend;/**该方法是controller层假定的*/publicinterface UserServiceI {    /**添加用户*/    publicbooleanadd(User u);}

Userserviceimpl is assumed to be a legacy system:

package com.shusheng.adapter.extend;/**假设该类是遗留系统已有的*/publicclass UserServiceImpl {    /**添加用户*/    publicbooleaninsert(User u){        System.out.println("这是userServiceImpl层的insert方法");        returntrue;    }}

Using inheritance to implement code reuse:

 PackageCom.shusheng.adapter.extend;/** Adapter class, use this class when instantiating to a controller * / Public  class userserviceadapter extends userserviceimpl  implements  Userservicei{//Private Userserviceimpl Userserviceimpl = new Userserviceimpl (), no declaration required on inheritance    @Override     Public Boolean Add(User u) {System.out.println ("This is the adapter's Add method");returnInsert (U);//Explicitly call the Userserviceimpl Insert method of the parent class}}

User class:

package com.shusheng.adapter.extend;/**用户类*/publicclass User {}

Test procedure:

package com.shusheng.adapter.extend;publicclass AdapterTest {    publicstaticvoidmain(String[] args) {        new UserServiceAdapter();//完成了对接        System.out.println(userServiceI.add(new User()));    }}

The second solution is a combination of:
For the use of reuse code, if not a strong is-a relationship, as little as possible inheritance, because inheritance will cause the system architecture is complex, not conducive to decoupling.
A combination is the addition of a reference to a class that is used to display the reuse code. (If you still can't understand, keep looking)

Userservicei class:

package com.shusheng.adapter;/**该方法是controller层假定的*/publicinterface UserServiceI {    /**添加用户*/    publicbooleanadd(User u);}

Userserviceimpl class:

package com.shusheng.adapter;/**假设该类是遗留系统已有的*/publicclass UserServiceImpl {    /**添加用户*/    publicbooleaninsert(User u){        System.out.println("这是userServiceImpl层的insert方法");        returntrue;    }}

Adapter classes designed for docking:

package  com.shusheng.adapter; /** adapter class, use this class when injected into the controller */ public  class  userserviceadapter  implements  userservicei  { private  userserviceimpl userserviceimpl = new  Userserviceimpl (); //combination   @Override  public  boolean  add  (User u) {System.out.println ( "This is the adapter's Add Method" ); return  userserviceimpl.insert (u); //explicitly calls Userserviceimpl's Insert method }} 

User class:

package com.shusheng.adapter;publicclass User {}

Test Class Code:

package com.shusheng.adapter;publicclass AdapterTest {    publicstaticvoidmain(String[] args) {        new UserServiceAdapter();//完成了对接        System.out.println(userServiceI.add(new User()));    }}

Adapter mode (structural type)

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.