c++ 桥接模式

#ifndef Product_hpp
#define Product_hpp

#include 
#include 
using namespace std;
class Product {
public:
    Product() {};
    ~Product() {};
    virtual void info();
};

#endif /* Product_hpp */

#include "Product.hpp"

void Product::info() {
    printf("生成一般product\n");
}

#ifndef House_hpp
#define House_hpp

#include 
#include "Product.hpp"
class House: public Product{
public:
    House() {};
    ~House();
    virtual void info();
};
#endif /* House_hpp */

#include "House.hpp"
void House::info() {
    printf("我是房子\n");
}

#ifndef Clothes_hpp
#define Clothes_hpp

#include 
#include "Product.hpp"
class Clothes: public Product{
public:
    Clothes() {};
    ~Clothes() {};
    virtual void info();
};
#endif /* Clothes_hpp */

#include "Clothes.hpp"

void Clothes::info() {
    printf("我是衣服\n");
}

#ifndef Factory_hpp
#define Factory_hpp

#include 
#include "Product.hpp"
class Factory {
public:
    Factory() {};
    ~Factory();
    virtual void info(Product* product);
protected:
    virtual void buy();
    virtual void sell();
};
#endif /* Factory_hpp */

#include "Factory.hpp"
void Factory::info(Product* product) {
    this->buy();
    product->info();
    this->sell();
}

void Factory::buy() {
    printf("购买\n");
}
void Factory::sell() {
    printf("卖出\n");
}

#include 
#include "Factory.hpp"
#include "Clothes.hpp"
#include "House.hpp"
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, 桥接模式!\n";
    Factory* factory = new Factory();
    Clothes* clt = new Clothes();
    factory->info(clt);
    House* house = new House();
    factory->info(house);
    return 0;
}

你可能感兴趣的:(c++学习)