桥梁模式,将抽象部分与它的实现部分分离,使它们都可以独立地变化。实现分离的办法就是增加一个类,

11.1.解释

main(),客户

IProduct,产品接口

CHouse,房子

CIPod,ipod

CClothes,服装

CNewCorp,桥梁类,MakeMoney()是桥梁方法

CNewHouseCorp,只能生产房子,所以构造函数是CHouse*

CShanZhaiCorp,什么赚钱就生产什么,所以构造函数是IProduct*

说明:客户直接使用CNewHouseCorp和CShanZhaiCorp类,在main()函数里构造产品,然后传到这两个类里。这两个类的MakeMoney()函数,先调用基类的MakeMoney(),然后分别执行各自的逻辑。

注意:CNewCorp起到了桥梁的作用。可以分别增加产品和公司。

看代码:

//NewCorp.h

#pragma once
#include "IProduct.h"
class CNewCorp
{
public:
    CNewCorp(IProduct *pproduct);
    virtual ~CNewCorp(void);
    void MakeMoney();
private:
    IProduct *m_pProduct;
};

//NewCorp.cpp

#include "StdAfx.h"
#include "NewCorp.h"
CNewCorp::CNewCorp( IProduct *pproduct )
{
    this->m_pProduct = pproduct;
}
CNewCorp::~CNewCorp(void)
{
}
void CNewCorp::MakeMoney()
{
    //每个公司都是一样,先生产
    this->m_pProduct->BeProducted();

    //然后销售
    this->m_pProduct->BeSelled();
}

//NewHouseCorp.h

#pragma once
#include "newcorp.h"
#include "House.h"
class CNewHouseCorp :
    public CNewCorp
{
public:
    CNewHouseCorp(CHouse *pHouse);
    ~CNewHouseCorp(void);
    void MakeMoney();
};

//NewHouseCorp.cpp

#include "StdAfx.h"
#include "NewHouseCorp.h"
#include <iostream>
using std::cout;
using std::endl;
CNewHouseCorp::CNewHouseCorp(CHouse *pHouse) : CNewCorp(pHouse)
{
}
CNewHouseCorp::~CNewHouseCorp(void)
{
}
void CNewHouseCorp::MakeMoney()
{
    this->CNewCorp::MakeMoney();
    cout << "房地产公司赚大钱了..." << endl;
}

//ShanZhaiCorp.h

#pragma once
#include "newcorp.h"
#include "IProduct.h"
class CShanZhaiCorp :
    public CNewCorp
{
public:
    CShanZhaiCorp(IProduct *pproduct);
    ~CShanZhaiCorp(void);
    void MakeMoney();
};

//ShanZhaiCorp.cpp

#include "StdAfx.h"
#include "ShanZhaiCorp.h"
#include <iostream>
using std::cout;
using std::endl;
CShanZhaiCorp::CShanZhaiCorp(IProduct *pproduct) : CNewCorp(pproduct)
{
}
CShanZhaiCorp::~CShanZhaiCorp(void)
{
}
void CShanZhaiCorp::MakeMoney()
{
    this->CNewCorp::MakeMoney();
    cout << "我赚钱呀..." << endl;
}

//IProduct.h

#pragma once
class IProduct
{
public:
    IProduct(void)
    {
    }
    virtual ~IProduct(void)
    {
    }
    virtual void BeProducted() = 0;
    virtual void BeSelled() = 0;
};

//House.h

#pragma once
#include "iproduct.h"
class CHouse :
    public IProduct
{
public:
    CHouse(void);
    ~CHouse(void);
    void BeProducted();
    void BeSelled();
};

//House.cpp

#include "StdAfx.h"
#include "House.h"
#include <iostream>
using std::cout;
using std::endl;
CHouse::CHouse(void)
{
}
CHouse::~CHouse(void)
{
}
void CHouse::BeProducted()
{
    cout << "生产出的房子是这个样子的..." << endl;
}
void CHouse::BeSelled()
{
    cout << "生产出的房子卖出去了..." << endl;
}

//Clothes.h

#pragma once
#include "iproduct.h"
class CClothes :
    public IProduct
{
public:
    CClothes(void);
    ~CClothes(void);
    void BeProducted();
    void BeSelled();
};

//Clothes.cpp

#include "StdAfx.h"
#include "Clothes.h"
#include <iostream>
using std::cout;
using std::endl;
CClothes::CClothes(void)
{
}
CClothes::~CClothes(void)
{
}
void CClothes::BeProducted()
{
    cout << "生产出的衣服是这个样子的..." << endl;
}
void CClothes::BeSelled()
{
    cout << "生产出的衣服卖出去了..." << endl;
}

//IPod.h

#pragma once
#include "iproduct.h"
class CIPod :
    public IProduct
{
public:
    CIPod(void);
    ~CIPod(void);
    void BeProducted();
    void BeSelled();
};

//IPod.cpp

#include "StdAfx.h"
#include "IPod.h"
#include <iostream>
using std::cout;
using std::endl;
CIPod::CIPod(void)
{
}
CIPod::~CIPod(void)
{
}
void CIPod::BeProducted()
{
    cout << "生产出的ipod是这个样子的..." << endl;
}
void CIPod::BeSelled()
{
    cout << "生产出的ipod卖出去了..." << endl;
}

//

// Bridge.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "ClothesCorp.h"
#include "NewHouseCorp.h"
#include "Clothes.h"
#include "IPod.h"
#include "ShanZhaiCorp.h"
#include <iostream>
using std::cout;
using std::endl;

void DoNewRun1()
{
    cout << "----------房地产公司是这样运行的----------" << endl;
    CHouse house;
    CNewHouseCorp newHouseCorp(&house);
    newHouseCorp.MakeMoney();
    cout << endl;

    cout << "----------山寨公司是这样运行的----------" << endl;
    CClothes clothes;
    CShanZhaiCorp shanZhaiCorp(&clothes);
    shanZhaiCorp.MakeMoney();
    cout << endl;
}

void DoNewRun2()
{
    cout << "----------房地产公司是这样运行的----------" << endl;
    CHouse house;
    CNewHouseCorp newHouseCorp(&house);
    newHouseCorp.MakeMoney();
    cout << endl;

    cout << "----------山寨公司是这样运行的----------" << endl;
    CIPod ipod;
    CShanZhaiCorp shanZhaiCorp(&ipod);
    shanZhaiCorp.MakeMoney();
    cout << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //只有两家公司,一家是房地产公司,另一家公司是衣服赚钱就生产衣服
    DoNewRun1();

    //只有两家公司,一家是房地产公司,另一家公司是ipod赚钱就生产ipod
    DoNewRun2();

    _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
    _CrtDumpMemoryLeaks();
    return 0;
}

桥梁模式(又可以称做桥接模式)属于结构型模式。CNewCorp实现了桥梁(桥接)的作用。

posted on 2011-04-17 19:55  星晨_jqren  阅读(6708)  评论(2编辑  收藏  举报