adapter pattern (with wreck it ralph)

9
Name : Adapter Problem : How to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces? Solution : Convert the original interface of a component into another interface, through an intermediate adapter object.

Upload: rajeev-naik

Post on 16-Mar-2018

181 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Adapter Pattern (with Wreck It Ralph)

Name : Adapter

Problem : How to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces?

Solution : Convert the original interface of a component into another interface, through an intermediate adapter object.

Page 2: Adapter Pattern (with Wreck It Ralph)
Page 3: Adapter Pattern (with Wreck It Ralph)

fireBullets()

Page 4: Adapter Pattern (with Wreck It Ralph)

fireBullets()

Page 5: Adapter Pattern (with Wreck It Ralph)

fireBullets()

Smash()

Page 6: Adapter Pattern (with Wreck It Ralph)

fireBullets()

Smash()

fireBullets()

Page 7: Adapter Pattern (with Wreck It Ralph)

public interface BotLeader {

public void fireBullets();

}

Public class Bot implements BotLeader {

@Override

public void fireBullets() {

if (countBullet() < 0) { reload(); }

System.out.println(“1 bullet fired”);

bulletCount--;

}

}

Page 8: Adapter Pattern (with Wreck It Ralph)

public class Ralph {

public void smash() {

moveHandsUp();

moveHandsDownWithForce();

}

}

public class RalphAdapter implements BotLeader {

private ralph;

public RalphAdapter (Ralph activeRalph) {

this.ralph = activeRalph;

}

@Override

public void fireBullets() {

ralph.smash();

}

}

Page 9: Adapter Pattern (with Wreck It Ralph)

public class StartGame {

public void main() {

Bot soldier = new Bot();

Ralph raf = new Ralph();

RalphAdapter rafAdapter = new RalphAdapter(raf);

System.out.println(“Commander commands fireBullets()”);

soldier.fireBullets();

rafAdapter.fireBullets();

System.out.println(“All cyborgs are killed!!! Medal won!!! ”);

}

}