迪米特法则

shienh 2020年01月19日 221次浏览

迪米特法则:最少知识法则。如用户找房,不需要自己找,让中介帮你找。
用户只需要告诉中介需要什么样的房子

#include <iostream>
#include <vector>
#include <string>
using namespace std;
 
class AbstractBuding{
public:
    virtual void sale()=0;
    virtual string getquality()=0;
};
 
class BudingA:public AbstractBuding{
public:
    BudingA(){
        quality="high";
    }
 
    virtual void sale(){
        cout<<quality<<endl;
    }
 
    virtual string getquality(){
        return quality;
    }
 
private:
    string quality;
 
};
 
class BudingB:public AbstractBuding{
public:
    BudingB(){
        quality="low";
    }
 
    virtual void sale(){
         cout<<quality<<endl;
    }
 
    virtual string getquality(){
        return quality;
    }
 
private:
    string quality;
 
};
 
//中介类
class Mediatot{
public:
    Mediatot(){
        AbstractBuding* building = new BudingA;
        vBuild.push_back(building);
 
        building = new BudingB;
        vBuild.push_back(building);
    }
 
    ~Mediatot(){
        for(vector<AbstractBuding*>::iterator it = vBuild.begin(); it != vBuild.end();it++){
            if(NULL!=*it){
                delete *it;
            }
        }
    }
 
    //对外接口 找楼盘
    AbstractBuding* findBuilding(string quality){
 
        for(vector<AbstractBuding*>::iterator it = vBuild.begin(); it != vBuild.end();it++){
           if(quality == (*it)->getquality()){
                return *it;
           }
        }
        return NULL;
 
    }
 
private:
    vector<AbstractBuding*>vBuild;
};
 
int main(int argc, char *argv[])
{
    Mediatot* media = new Mediatot;
    //让中介找房
    AbstractBuding* buding = media->findBuilding("high");
    if(NULL!=buding){
        buding->sale(); 
}
    return 0;
}