設計模式入門,裝飾著模式,c++代碼實現

來源:互聯網
上載者:User

標籤:裝飾者模式   類型   設計模式   ring   off   argv   int   string類   new   

// test03.cpp : Defines the entry point for the console application.
//
//設計模式第3章 裝飾者模式
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <cstring>
using namespace std;
class Beverage
{
public:
    string description /*= "Unkown Beverage"*/;//只有靜態整型常量資料成員能在類中初始化

public:
    virtual string getDescription()//必須為虛函數
    {
        return description;
    }

    virtual double cost(){return 0;} ;//必須為虛函數

};

class CondimentDecorator : public Beverage
{
    virtual string getDescription(){return NULL;} ;
};

class Espresso : public Beverage
{
public:
    Espresso()
    {
        description = "Espresso";
    }

    double cost(){
        return 1.99;
    }
};

class HouseBlend : public Beverage
{
public:
    HouseBlend()
    {
        description = "House Blend Coffee";
    }

    double cost()
    {
        return .89;
    }
};

class DarkRoast : public Beverage
{
public:
    DarkRoast()
    {
        description = "Dark Roast Coffee";
    }

    double cost()
    {
        return .99;
    }
};

class Mocha : public CondimentDecorator
{
    Beverage* beverage;

public:
    Mocha(Beverage* beverage)//必須為指標
    {
        this->beverage = beverage;
    }

    string getDescription()
    {
        return beverage->getDescription() + ", Mocha";
    }

    double cost()
    {
        return .20 + beverage->cost();
    }
};

class Whip : public CondimentDecorator
{
    Beverage* beverage;

public:
    Whip(Beverage* beverage)
    {
        this->beverage = beverage;
    }

    string getDescription()
    {
        return beverage->getDescription() + ", Whip";
    }

    double cost()
    {
        return .10 + beverage->cost();
    }
};

class Soy : public CondimentDecorator
{
    Beverage* beverage;

public:
    Soy(Beverage* beverage)
    {
        this->beverage = beverage;
    }

    string getDescription()
    {
        return beverage->getDescription() + ", Soy";
    }

    double cost()
    {
        return .15 + beverage->cost();
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Beverage* beverage = new Espresso();
    cout<<beverage->getDescription() << " $ " << beverage->cost()<<endl;

    Beverage* beverage2 = new DarkRoast();
    beverage2 = new Mocha(beverage2);//必須傳遞指標
    beverage2 = new Mocha(beverage2);
    beverage2 = new Whip(beverage2);
    //printf("%s %$ %f",beverage2->getDescription(),beverage2->cost());//printf 不能輸出string類型
    cout<<beverage2->getDescription()<<" $ "<<beverage2->cost()<<endl;

    Beverage* beverage3 = new HouseBlend();
    beverage3 = new Soy(beverage3);
    beverage3 = new Mocha(beverage3);
    beverage3 = new Whip(beverage3);
    //printf("%s %$ %f",beverage3->getDescription(),beverage3->cost());
    cout<<beverage3->getDescription()<<" $ "<<beverage3->cost()<<endl;
    return 0;
}


設計模式入門,裝飾著模式,c++代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.