Java language Implementation Structural design pattern-Adapter mode

Source: Internet
Author: User
Tags map class

First, describe

The adapter mode is to tell the interface of a system to be transformed into another form, so that an interface that cannot be called directly becomes callable. For example, I now have a list of list type users, but my system needs a map type of user list, so I need to write an adapter to inherit the map class, the list type lists to fit into the list of map type so that can be used directly in the system.

When the system needs to use an external interface that does not meet the needs of the system, we need to encapsulate the external interface using an adapter pattern to convert to a type that the system can use. The adapter class is divided into the object adapter and the class adapter, where the object adapter, which implements the interface of the target class, depends on the adapter class, and for the class adapter, inherits the adapter class directly and implements the interface of the target class.

The adapter pattern consists mainly of 3 parts: The target class, the source class, and the adapter class. The target class here is the type you need for your system, and the source class is the type of system that you now have but is not suitable for you, and the adapter class is the transformation class used to convert the source class to the target class, for example in Java I/O. There are adapters InputStreamReader and outputstreamwriter that convert the byte stream and the character stream, each of which is responsible for converting the input byte stream to a character stream and converting the output byte stream to a character stream, respectively, for the two classes.


Second, the advantages and disadvantages of the adapter class

Advantage: Using the adapter mode in the system, you can connect the interface of one system with the class of another system that is incompatible, build a bridge between the two sides, so that these two classes can smooth transition and fusion through this adapter class, emphasizing the transformation of the interface.

Disadvantage: For class adapters, it is not possible to fit a class and its subclasses, and for object adapters it is difficult to redefine the behavior of the adapted classes. Because the class adapter inherits the Adaptee (the Adaptive Class), in the Client class we can select and create any kind of sub-class that meets the requirements as needed to achieve the specific functionality. The adapter class is a member variable of the adapter class, instead of using multiple inheritance or inheritance to implement it, but using direct affinity, or the way it is called a delegate.


Third, the source code

1. SOURCE class:

package Tong.day5_4.adapter;import Java.util.arraylist;import java.util.list;/** * Source class: Returns a list of internal people for a list type, but the system needs a list of map types, So the adapter class needs to be converted * @author Tong * */public class Uniformfacade {//Get a list of people lists of type public list<person> getpersonlist () {L ist<person> personlist = new arraylist<person> ();p ersonlist.add (New person ("Zhang San"));p Ersonlist.add (new Person ("John Doe"));p Ersonlist.add ("Harry");p Ersonlist.add (The new Person ("Zhao Liu"); return personlist;}} Class Person {private string name;private int age;public person (String Name,int age) {this.name = Name;this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Override ToString () to print out the person object when output @overridepublic String toString () {return "person [name=" + "name +", age= "+ Age +" in the following format) ";}}

2. Target class

Package Tong.day5_4.adapter;import java.util.hashmap;/** * Target class: This is the type of system required, the system will use this class for data output * @author Tong * */class Managemap {public static void-execute (HashMap map) {for (int i = 0; i < map.size (); i++) {System.out.println (Map.get (i) );}}}

3. Adapter class

Package Tong.day5_4.adapter;import Java.util.hashmap;import java.util.list;/** * Adapter class: Convert a list Type people table to a people table of map type * @ Author Tong * */public class Mapadapter extends Hashmap{private list list;public mapadapter (List list) {this.list = list;} public int size () {return list.size ();} public object Get (Object object) {return List.get (new Integer (Object.ToString ())). Intvalue ());}}

4. The client calls the class

Package tong.day5_4.adapter;//Client view class public class Client {public static void main (string[] args) {//Create source class, Gets the result of the source class Uniformfacade Uniformfacade = new Uniformfacade ();//The result of the source class is passed as a parameter to the adapter class, and the adapter object is created Mapadapter Mapadapter = new Mapadapter (Uniformfacade.getpersonlist ());//The adapter object is used as a parameter to the method of the target class, and the conversion effect of the adapter class in the target class is Managemap.execute (Mapadapter);}}

Operation Result:




Java language Implementation Structural design pattern-Adapter mode

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.