[Design Pattern] Adapter Design Pattern

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Design Pattern] Adapter Design Pattern相关的知识,希望对你有一定的参考价值。

interface EnemyAttacker 
  fireWeapon(): void;
  driveForward(): void;
  assignDriver(driverName: string): void;


// Target
class EnemyTank implements EnemyAttacker 
  fireWeapon() 
    const attackDamage = Math.round(Math.random() * 10 + 4);
    console.log(`Enemy Tank Does $attackDamage Damage`);
  
  driveForward() 
    const movement = Math.round(Math.random() * 5 + 2);
    console.log(`Enemy Tank moves $movement spaces`);
  
  assignDriver(driverName: string): void 
    console.log(`$driverName is driving the tank`);
  


// Adaptee
class EnemyRobot 
  smashWithHands() 
    const attackDamage = Math.round(Math.random() * 5 + 1);
    console.log(`Enemy Robot Does $attackDamage Damage With its hands`);
  
  walkForward() 
    const movement = Math.round(Math.random() * 3 + 1);
    console.log(`Enemy Robot walks forward $movement spaces`);
  
  reactToHuman(driverName: string) 
    console.log(`Enemy Robot Tramps on $driverName`);
  


// Adapter
class EnemyRobotAdapter implements EnemyAttacker 
  enemyRebot: EnemyRobot;

  constructor(robot: EnemyRobot) 
    this.enemyRebot = robot;
  

  fireWeapon(): void 
    this.enemyRebot.smashWithHands();
  
  driveForward(): void 
    this.enemyRebot.walkForward();
  
  assignDriver(driverName: string): void 
    this.enemyRebot.reactToHuman(driverName);
  


const tank = new EnemyTank();
const robot = new EnemyRobot();
const robotAdapter = new EnemyRobotAdapter(robot);

console.log(`The Robot`);
robot.reactToHuman(`Paul`);
robot.walkForward();
robot.smashWithHands();

console.log("Tank");
tank.assignDriver("Frank");
tank.driveForward();
tank.fireWeapon();

console.log("The Robot with the Adapter");
robotAdapter.assignDriver("Mark");
robotAdapter.driveForward();
robotAdapter.fireWeapon();

 

Client wants to use EnemyRobot the same way as use any other EnemyAttcker.

But EnemyRobot has different interface as EnemyAttcker.

Therefore we need EnemyRobotAdapter to help translate EnemyRobot interface to EnemyAttcker interface.

EnemyRobotAdapater use EnemyRobot and implements EnemyAttacker.

java design pattern - adapter pattern

场景

在编程中我们经常会遇到驴头不对马嘴的情况,比如以前是继承A的接口的对象,现在外部调用的时候需要该对象已B接口的形式来调用
,这个时候我们可以让对象同时集成A和B接口来达到目的,不过,这样又违反了开闭原则,这个时候我们可以使用适配器模式来达到目的。

适配器模式

适配器模式是一种结构模式,可以帮助我们把对象以不同的接口方式来调用。主要由3个角色组成:

  • Target 外部调用要求的接口方式
  • Adapter 中间用来协调的适配器
  • Adaptee 原始对象

    首先,我们让Adapter继承实现Target接口,其次在Adapter中构造Adaptee对象,然后在Target方法中调用Adaptee中相应的方法。过程非常简单。

技术图片

下面是适配器模式的一个简单实现:

  public interface Target {
    void request();
 大专栏  java design pattern - adapter pattern}
  public class Adaptee {
    public void doSomething() {
        System.out.println("Adaptee doSomething!!!");
    }
  }
  public class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        this.adaptee.doSomething();
    }
  }
  public class Test {
    public static void main(String[] args) {
        Target target = new Adapter(new Adaptee());
        target.request();
    }
  }

总结

适配器是一种非常简单的设计模式,一般是用来在后期调用时发现对象的接口不匹配的时候使用,相当于一种“补充”的手段
在双方都不太容易修改的时候可以使用适配器模式。

参考资料

Adapter pattern

Design Patterns - Adapter Pattern

以上是关于[Design Pattern] Adapter Design Pattern的主要内容,如果未能解决你的问题,请参考以下文章

java design pattern - adapter pattern

[Pattern] Adapter pattern

七适配器(Adapter)模式--结构模式(Structural Pattern)

MVVM design pattern

Adapter Pattern

Adapter Pattern