The adaptor pattern
This presentation is the property of its rightful owner.
Sponsored Links
1 / 36

The Adaptor Pattern (结构模式之适配器模式) PowerPoint PPT Presentation


  • 69 Views
  • Uploaded on
  • Presentation posted in: General

The Adaptor Pattern (结构模式之适配器模式). 现实生活中的适配器. 现实生活中的适配器. 面向对象软件系统的适配问题. 假设我们已经有一个软件系统,原来使用了一个第三方类库 A 。现在有一个新的第三方类库 B ,其功能等各方面都更加强大。我们希望用 B 来替换 A ,以改善我们的系统。但是 B 的接口与 A 不一样。那则么办呢?. B. System. A. New System. 办法之一. B. New System. 办法之一. B. System. A. Adapter. 办法之二. B. System. Adapter.

Download Presentation

The Adaptor Pattern (结构模式之适配器模式)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


The adaptor pattern

The AdaptorPattern


The adaptor pattern


The adaptor pattern


The adaptor pattern

  • ABBABA


The adaptor pattern

B

System

A

New System


The adaptor pattern

B

New System


The adaptor pattern

B

System

A

Adapter


The adaptor pattern

B

System

Adapter


The adaptor pattern

B

B

System


The adaptor pattern

  • public interface Duck {

    public void quack();

    public void fly();

    }

Duck


Mallardduck

MallardDuck

public class MallardDuck implements Duck {

public void quack() {

System.out.println("...");

}

public void fly() {

System.out.println("");

}

}

MallardDuckDuck


The adaptor pattern

public interface Turkey {

public void gobble();

public void fly();

}


Wildturkey

WildTurkey

public class WildTurkey implements Turkey {

public void gobble() {

System.out.println("...");

}

public void fly() {

System.out.println("");

}

}


The adaptor pattern


The adaptor pattern

public class TurkeyAdapter implements Duck {

Turkey turkey;

public TurkeyAdapter(Turkey turkey) {

this.turkey = turkey;

}

public void quack() {

turkey.gobble();

}

public void fly() {

for(int i=0; i < 5; i++) {

turkey.fly();

}

}

}


The adaptor pattern

public class DuckTestDrive {

public static void main(String[] args) {

MallardDuck duck = new MallardDuck();

WildTurkey turkey = new WildTurkey();

Duck turkeyAdapter = new TurkeyAdapter(turkey);

System.out.println("...");

turkey.gobble();

turkey.fly();

System.out.println("\n...");

testDuck(duck);

System.out.println("\n...");

testDuck(turkeyAdapter);

}

static void testDuck(Duck duck) {

duck.quack();

duck.fly();

}

}


The adaptor pattern

:


The adaptor pattern

  • DuckAdapter


Duckadapter

DuckAdapter

import java.util.Random;

public class DuckAdapter implements Turkey {

Duck duck;

Random rand;

public DuckAdapter(Duck duck) {

this.duck = duck;

rand = new Random();

}

public void gobble() {

duck.quack();

}

public void fly() {

if (rand.nextInt(5) == 0) {

duck.fly();

}

}

}


Adapter

Adapter

    • Wrapper


Adapter1

Adapter


Adapter2

Adapter

    • 1) A d a p t e r

    • 2) A d a p t e r


Adapter3

Adapter

  • A d a p t e r

    • A d a p t e r


Adapter4

Adapter


Adapter5

Adapter


Adapter6

Adapter

  • AdapterAdapteeAdapterAdapteeAdapterAdapterAdapterAdaptee


Adapter7

Adapter

  • AdapterAdapteeAdapterAdapter

  • AdapterAdapterAdapteeAdapteeAdapterAdapter


Adapter8

Adapter

    • Ta r g e t

      • C l i e n t

    • C l i e n t

      • Ta rg e t

    • A d a p t e e

    • A d a p t e r

      • A d a p t e eTa rg e t


Adapter9

Adapter

    • ClientA d a p t e rA d a p t e e


Adapter10

Adapter

      • A d a p t e rA d a p t e eTa rg e tA d a p t e r

      • A d a p t e rA d a p t e eA d a p t e rA d a p t e e

      • a d a p t e e


Adapter11

Adapter

      • A d a p t e rA d a p t e eA d a p t e eA d a p t e rA d a p t e e

      • A d a p t e eA d a p t e eA d a p t e rA d a p t e e


The adaptor pattern

  • A d a p t e r


The adaptor pattern

public class WebFormDesigner extends JFrame

{

void jbInit() throws Exception

{

text.addActionListener(new

WebFormDesigner_text_actionAdapter(this));

}

void text_actionPerformed(ActionEvent e)

{

TextDialog createText = new TextDialog();

createText.setVisible(true);

createText.setTitle("TEXT");

createText.setBounds(100, 100, 500, 300);

createText.setModal(true);

createText.show();

}

}


The adaptor pattern

---

class WebFormDesigner_text_actionAdapter implements java.awt.event.ActionListener

{

WebFormDesigner adaptee;

WebFormDesigner_text_actionAdapter(WebFormDesigner adaptee)

{

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e)

{

adaptee.text_actionPerformed(e);

}

}


The adaptor pattern

WebFormDesignerAdaptee, WebFormDesigner_text_actionAdapterAdapter, java.awt.event.ActionListener

public interface ActionListener extends EventListener {

/**

* Invoked when an action occurs.

*/

public void actionPerformed(ActionEvent e);

}

actionPerformedWebFormDesigner_text_actionAdapter java.awt.event.ActionListeneractionPerformedWebFormDesigner_text_actionAdapteractionPerformedWebFormDesigneradapteeWebFormDesignerWebFormDesigner_text_actionAdapterWebFormDesigner_text_actionAdapterWebFormDesigner

AdapteeWebFormDesignertext_actionPerformedAdapterWebFormDesigner_text_actionAdapteactionPerformedAdapter


The adaptor pattern

(

  • Kittiemiao(),run(),sleep(),catchRat()

  • Puppiewao(), fetchBall(), run(), sleep()

  • kittiepuppie

  • [email protected]


  • Login