design_pattern_adapter

This post implements a relative easy and straight forward pattern in <Head First Design Pattern>:Adapter Pattern. The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. The general procedure is as follows

  1. Identify the players: the component(s) that want to be accommodated (i.e. the client), and the component that needs to adapt (i.e. the adaptee).
  2. Identify the interface that the client requires.
  3. Design a "wrapper" class that can "impedance match" the adaptee to the client.
  4. The adapter/wrapper class "has a" instance of the adaptee class.
  5. The adapter/wrapper class "maps" the client interface to the adaptee interface.
  6. The client uses (is coupled to) the new interface

#include<stdlib.h>

class Duck {
public:
 	virtual void fly() const = 0;
	virtual void quack() const = 0;
};

class MallardDuck : public Duck {
public:
    void fly() const {
		std::cout << "I'm flying" << std::endl;
	}

	void quack() const {
		std::cout << "Quack" << std::endl;
	}
};

class Turkey {
public:
	virtual void gobble() const = 0;
 	virtual void fly() const = 0;
};

class WildTurkey : public Turkey {
public:
    void fly() const {
		std::cout << "I'm flying a short distance" << std::endl;
	}

	void gobble() const {
		std::cout << "Gobble gobble" << std::endl;
	}
};

class DuckAdapter : public Turkey {
private:
    Duck* duck;
	int random;

	DuckAdapter(const DuckAdapter& ); // Disable copy constructor
	void operator=(const DuckAdapter& ); // Disable assignment operator

public:
    DuckAdapter(Duck* duck_):duck(duck_) {
		srand(123);
		random = rand() % 5;
		if(random == 0) random = 1;
	}

	void fly() const {
		for(int i = 0; i<random; i++ ) {
			duck->fly();
		}
	}

	void gobble() const {
		duck->quack();
	}

	~DuckAdapter(){
        delete duck;
        duck = NULL;
	}
};

class TurkeyAdapter : public Duck {
private:
    Turkey* turkey;

	TurkeyAdapter( const TurkeyAdapter& ); // Disable copy constructor
	void operator=( const TurkeyAdapter& ); // Disable assignment operator

public:
    TurkeyAdapter(Turkey* turkey_):turkey(turkey_) {}

 	void fly() const {
		for(int i = 0; i < 5; i++ ) {
			turkey->fly();
		}
	}

	void quack() const {
		turkey->gobble();
	}
};
Main function is

#include <iostream>
#include <./duck_geese.hpp>

int main()
{
    std::cout << "For duck and duck in turkey disguise: " << std::endl;
    MallardDuck* duck = new MallardDuck();
    Turkey* duckAdapter = new DuckAdapter(duck);
    duck->fly();
    duck->quack();
    duckAdapter->fly();
    duckAdapter->gobble();

    std::cout << "For turkey and turkey in duck disguise: " << std::endl;
    WildTurkey* turkey = new WildTurkey();
	Duck* turkeyAdapter = new TurkeyAdapter(turkey);
    turkey->fly();
    turkey->gobble();
    turkeyAdapter->fly();
    turkeyAdapter->quack();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值