Adapter Mode Adapter

Source: Internet
Author: User

One system wants to use another system's data source, but the data source format does not meet its own requirements, which can be used in adapter mode

One example: The data provided by the personnel system returns the map type, but the financial system needs a List type of data

Data sources provided by the personnel system:

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4  Public classpeople {5      PublicMap<object, object>Getorgname () {6Map<object, object> map =NewHashmap<>();7Map.put (1, "personnel Department");8Map.put (2, "Administrative Office");9Map.put (3, "Finance Office"));Ten         returnmap; One     } A}

Call mode in the personnel system:

1 ImportJava.util.Map;2 3  Public classpclient {4 5      Public Static voidMain (string[] args) {6People people =Newpeople ();7Map<object, object> map =people.getorgname ();8          for(inti = 0; I < map.size (); i++) {9System.out.println (Map.get (i + 1));Ten         } One  A     } -  -}

The financial system uses the data source adapter:

1 Importjava.util.ArrayList;2 ImportJava.util.Map;3 4  Public classPeopleadapterextendsArraylist<object> {5     Privatemap<?,? >map;6 7      PublicPeopleadapter (map<?,? >map) {8         Super();9          This. Map =map;Ten     } One  A      Public intsize () { -         returnmap.size (); -     } the  -      PublicObject Get (inti) { -         returnMap.get (i); -     } +}

Financial system calls:

1  Public classeclient {2 3      Public Static voidMain (string[] args) {4Peopleadapter list =NewPeopleadapter (Newpeople (). Getorgname ());5          for(inti = 0; I < list.size (); i++) {6System.out.println (List.get (i + 1));7         }8     }9 Ten}

Adapter Mode Adapter

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.