Adapter mode Extension

Source: Internet
Author: User

In the previous example, the original role has only one role. In this case, we only need to inherit the role. If there are multiple implementation classes, Java does not support multiple inheritance, we will re-write an implementation class, and then reference other classes, for example:

Package adapter. demo; public interface iuserinfo {// obtain the user name Public String GetUserName (); // obtain the home address public string gethomeaddress (); // obtain the mobile phone number Public String gethomenumber (); // obtain the office phone number Public String getofficephonenum (); // obtain the company's position Public String getjobposition (); // obtain the home phone number Public String gethomephone ();}

Package Adapter. Demo; import java. util. Map; public interface iouteruserofficeinfo {// public map getuserofficeinfo ();}

Package Adapter. Demo; import java. util. Map; public interface iouteruserhomeinfo {// public map getuserhomeinfo ();}

Package Adapter. Demo; import java. util. Map; public interface iouteruserbaseinfo {// obtain the basic information of the user public map getuserbaseinfo ();}

package adapter.demo;import java.util.HashMap;import java.util.Map;public class OuterUserBaseInfo implements IOuterUserBaseInfo{@Overridepublic Map getUserBaseInfo() {HashMap baseInfoMap = new HashMap();baseInfoMap.put("username1 ", "name");baseInfoMap.put("mobile1", "1234455");return baseInfoMap;}}

package adapter.demo;import java.util.HashMap;import java.util.Map;public class OuterUserHomeInfo implements IOuterUserHomeInfo{@Overridepublic Map getUserHomeInfo() {HashMap baseInfoMap = new HashMap();baseInfoMap.put("username ", "name");baseInfoMap.put("mobile", "1234455");return baseInfoMap;}}

package adapter.demo;import java.util.HashMap;import java.util.Map;public class OuterUserOfficeInfo implements IOuterUserOfficeInfo{@Overridepublic Map getUserOfficeInfo() {HashMap baseInfoMap = new HashMap();baseInfoMap.put("username11 ", "name");baseInfoMap.put("mobile11", "1234455");return baseInfoMap;}}

Here is the focus, to see how to use the adapter for multi-Inheritance

Package adapter. demo; import Java. util. map; public class outeruserinfo implements iuserinfo {// source object target private iouteruserbaseinfo baseinfo = NULL; private iouteruserofficeinfo officeinfo = NULL; private iouteruserhomeinfo homeinfo = NULL; // data processing private map basemap = NULL; private map homemap = NULL; private map officemap = NULL; // The constructor transmits public outeruserinfo (iouteruserbaseinfo _ baseinfo, iouteruserofficeinfo _ officeinfo, iouteruserhomeinfo _ homeinfo) {This. baseinfo = _ baseinfo; this. officeinfo = _ officeinfo; this. homeinfo = _ homeinfo; this. basemap = This. baseinfo. getuserbaseinfo (); this. homemap = This. homeinfo. getuserhomeinfo (); this. officemap = This. officeinfo. getuserofficeinfo () ;}@ overridepublic string GetUserName () {string hometelnum = (string) This. homemap. get ("username"); system. out. println (hometelnum); Return hometelnum;} @ overridepublic string gethomeaddress () {string homeaddress = (string) This. homemap. get ("mobile"); system. out. println (homeaddress); Return homeaddress;} @ overridepublic string get1_number () {string 1_num = (string) This. homemap. get ("mobile"); Return writable num;} @ overridepublic string getofficephonenum () {// todo auto-generated method stubreturn NULL;} @ overridepublic string getjobposition () {return NULL ;} @ overridepublic string gethomephone () {// todo auto-generated method stubreturn NULL ;}}

package adapter.demo;public class Client {public static void main(String[] args) {IOuterUserHomeInfo homeinfo = new OuterUserHomeInfo();IOuterUserBaseInfo baseinfo = new OuterUserBaseInfo();IOuterUserOfficeInfo office = new OuterUserOfficeInfo();IUserInfo girl = new OuterUserInfo(baseinfo, office, homeinfo);girl.getHomeAddress();}}



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.