adapter pattern (with wreck it ralph)

Post on 16-Mar-2018

181 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

fireBullets()

fireBullets()

fireBullets()

Smash()

fireBullets()

Smash()

fireBullets()

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

}

}

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

}

}

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!!! ”);

}

}

top related