Adapter pattern
Download
1 / 10

Adapter Pattern - PowerPoint PPT Presentation


  • 93 Views
  • Uploaded on

Adapter Pattern. public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly(); }. Adapter Pattern. WildTurkey. public class WildTurkey implements Turkey { public void gobble() {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Adapter Pattern' - keaton


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

public interface Duck {

public void quack();

public void fly();

}

public interface Turkey {

public void gobble();

public void fly();

}



Wildturkey
WildTurkey

public class WildTurkey implements Turkey {

public void gobble() {

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

}

public void fly() {

System.out.println("I'm flying

a short distance");

}

}


Mallardduck
MallardDuck

public class MallardDuck implements Duck {

public void quack() {

System.out.println("Quack");

}

public void fly() {

System.out.println("I'm flying");

}

}


Turkey adapter
Turkey Adapter

public class TurkeyAdapter implements Duck {

Turkey turkey;

public TurkeyAdapter(Turkey turkey) {

this.turkey = turkey;

}

public void quack() {

turkey.gobble();

}

public void fly() {

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

turkey.fly();

}

}

}


Duck adapter
Duck Adapter

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();

} } }


Turkey test drive
Turkey Test Drive

public class TurkeyTestDrive {

public static void main(String[] args) {

MallardDuck duck = new MallardDuck();

Turkey duckAdapter = new DuckAdapter(duck);

for(inti=0;i<10;i++) {

System.out.println("The DuckAdapter says...");

duckAdapter.gobble();

duckAdapter.fly();

}

}

}


Duck test drive
Duck Test Drive

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("The Turkey says...");

turkey.gobble();

turkey.fly();

System.out.println("\nThe Duck says...");

testDuck(duck);

System.out.println("\nTheTurkeyAdapter says...");

testDuck(turkeyAdapter);

}

static void testDuck(Duck duck) {

duck.quack();

duck.fly();

}

}


ad