冬季促销!
适配器

Rust 适配器模式讲解和代码示例

适配器是一种结构型设计模式 它能使不兼容的对象能够相互合作

适配器可担任两个对象间的封装器 它会接收对于一个对象的调用 并将其转换为另一个对象可识别的格式和接口

Adapter in Rust

In this example, the trait SpecificTarget is incompatible with a call function which accepts trait Target only.

fn call(target: impl Target);

The adapter helps to pass the incompatible interface to the call function.

let target = TargetAdapter::new(specific_target);
call(target);

adapter.rs

use crate::{adaptee::SpecificTarget, Target};

/// Converts adaptee's specific interface to a compatible `Target` output.
pub struct TargetAdapter {
    adaptee: SpecificTarget,
}

impl TargetAdapter {
    pub fn new(adaptee: SpecificTarget) -> Self {
        Self { adaptee }
    }
}

impl Target for TargetAdapter {
    fn request(&self) -> String {
        // Here's the "adaptation" of a specific output to a compatible output.
        self.adaptee.specific_request().chars().rev().collect()
    }
}

adaptee.rs

pub struct SpecificTarget;

impl SpecificTarget {
    pub fn specific_request(&self) -> String {
        ".tseuqer cificepS".into()
    }
}

target.rs

pub trait Target {
    fn request(&self) -> String;
}

pub struct OrdinaryTarget;

impl Target for OrdinaryTarget {
    fn request(&self) -> String {
        "Ordinary request.".into()
    }
}

main.rs

mod adaptee;
mod adapter;
mod target;

use adaptee::SpecificTarget;
use adapter::TargetAdapter;
use target::{OrdinaryTarget, Target};

/// Calls any object of a `Target` trait.
///
/// To understand the Adapter pattern better, imagine that this is
/// a client code, which can operate over a specific interface only
/// (`Target` trait only). It means that an incompatible interface cannot be
/// passed here without an adapter.
fn call(target: impl Target) {
    println!("'{}'", target.request());
}

fn main() {
    let target = OrdinaryTarget;

    print!("A compatible target can be directly called: ");
    call(target);

    let adaptee = SpecificTarget;

    println!(
        "Adaptee is incompatible with client: '{}'",
        adaptee.specific_request()
    );

    let adapter = TargetAdapter::new(adaptee);

    print!("But with adapter client can call its method: ");
    call(adapter);
}

Output

A compatible target can be directly called: 'Ordinary request.'
Adaptee is incompatible with client: '.tseuqer cificepS'
But with adapter client can call its method: 'Specific request.'

适配器在其他编程语言中的实现

C# 适配器模式讲解和代码示例 C++ 适配器模式讲解和代码示例 Go 适配器模式讲解和代码示例 Java 适配器模式讲解和代码示例 PHP 适配器模式讲解和代码示例 Python 适配器模式讲解和代码示例 Ruby 适配器模式讲解和代码示例 Swift 适配器模式讲解和代码示例 TypeScript 适配器模式讲解和代码示例