[Design Mode] - adapter mode Adapter

  Preface: [ Mode Overview ] ---------- by xingoo

  Intention mode

  If you already had a kind of class, and call the interface did not pass this class implements. Thus, this conventional type, adapted after converted into class supports interface.

  In other words, the programming interface to a prior Another useful interface.

  Mode structure

  [Adapter] class

  Target Target Interface

  Adaptee existing classes

  Adapter class intermediate conversion, i.e., to achieve the target interface, but inherits an existing class.

 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 }

  [Adapter object]

  The difference is that with the above, this is not a direct successor to the existing classes, but to existing classes, as an internal object, make the call.

 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 }

  scenes to be used

  1 would like to use an existing class, but its interface does not meet the requirements

  2 may want to create a reusable class that other classes can work together

  3 want to use the existing subclasses, but it is impossible for each sub-class match their interface. Thus the adapter can be adapted to the object to its parent class interface. (This did not understand, after slowly pondering)

  Life Design Patterns

  As the saying goes, My Fair Lady, Marty, recently ran male, very obsessed with Baby.

  However, if the peach blossom light, only around Xifeng, you do not need to worry.

  Simply makeup makeup, PS look, beautiful Xifeng, still irreplaceable!

  

  Although there is no AngleBaby, but we have Xifeng, so you can still see AngleBaby sweet 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 }

  operation result

我在哭泣...

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545422

Guess you like

Origin blog.csdn.net/weixin_34290631/article/details/91989314