[Design mode] -- adapter mode Adapter

Source: Internet
Author: User

[Mode overview] ---------- by xingoo

Pattern intent

If there is already a type, but the interface to be called cannot be implemented through this class. Therefore, convert the existing class to a class that supports interfaces after adaptation.

In other words, it is to program one existing interface to another available interface.

Mode Structure

Class Adapter]

Target Interface

Existing adaptee class

The class converted between the adapter, that is, the target interface is implemented and the existing class is inherited.

 1 package com.xingoo.test1; 2 interface Target{ 3     public void operation1(); 4     public void operation2(); 5 } 6 class Adaptee{ 7     public void operation1(){ 8         System.out.println("operation1"); 9     }10 }11 12 class Adapter extends Adaptee implements Target{13     public void operation2() {14         System.out.println("operation2");15     }16 }17 18 public class test {19     public static void main(String[] args){20         Target tar = new Adapter();21         tar.operation1();22         tar.operation2();23     }24 }

[Object Adapter]

Different from the above, this time, instead of directly inheriting the existing class, we call the existing class as an internal object.

 1 package com.xingoo.test2; 2  3 interface Target{ 4     public void operation1(); 5     public void operation2(); 6 } 7  8 class Adaptee{ 9     public void operation1(){10         System.out.println("operation1");11     }12 }13 14 class Adapter implements Target{15     private Adaptee adaptee;16     public Adapter(Adaptee adaptee){17         this.adaptee = adaptee;18     }19     public void operation1() {20         adaptee.operation1();21     }22 23     public void operation2() {24         System.out.println("operation2");25     }26     27 }28 public class test {29     public static void main(String[] args){30         Target tar = new Adapter(new Adaptee());31         tar.operation1();32         tar.operation2();33     }34 }
Use Cases

1. I want to use an existing class, but its interface does not meet the requirements.

2. Create a reusable class that can work with other classes.

3. You want to use an existing subclass, but it is impossible to match each subclass with their interface. Therefore, the Object Adapter can adapt to its parent class interface. (I don't understand this. I will try it later)

Design Patterns in life

As the saying goes, the lady and the gentleman have a crush on baby recently.

However, if the peach blossom is not prosperous and there is only Fengjie around, you do not need to worry.

You only need simple makeup, PS, beautiful Fengjie, still irreplaceable!

Although there is no anglebaby, We have Fengjie, so we can still see anglebaby smile.

 

 1 package com.xingoo.test3; 2 interface BeautifulGirl{ 3     public void Smiling(); 4 } 5 class UglyGirl{ 6     public void Crying(){ 7         System.out.println("我在哭泣..."); 8     } 9 }10 class ApplyCosmetics implements BeautifulGirl{11     private UglyGirl girl;12     public ApplyCosmetics(UglyGirl girl){13         this.girl = girl;14     }15     public void Smiling() {16         girl.Crying();17     }18 }19 public class test {20     public static void main(String[] args){21         BeautifulGirl girl = new ApplyCosmetics(new UglyGirl());22         girl.Smiling();23     }24 }

Running result

我在哭泣...

 

[Design mode] -- 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.