Adapter Design Pattern in Rust

The adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under a structural pattern as this pattern combines the capability of two independent interfaces.

The adapter pattern makes two incompatible interfaces compatible without changing their existing code

struct Banner {
    string: String,
}

impl Banner {
    fn new(string: String) -> Banner {
        Banner {
            string: string,
        }
    }

    fn show_with_paren(&self) {
        println!("({})", self.string);
    }

    fn show_with_aster(&self) {
        println!("*{}*", self.string);
    }
}

struct PrintBanner {
    banner: Banner,   
}

impl PrintBanner {
    fn new(string: String) -> PrintBanner {
        PrintBanner {
            banner: Banner::new(string),
        }
    }

    fn print_weak(&self) {
        self.banner.show_with_paren();
    }

    fn print_strong(&self) {
        self.banner.show_with_aster();
    }
}

fn main() {
    let p = PrintBanner::new("Hello".to_string());
    p.print_weak();
    p.print_strong();
}
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions