说人话:发号施令的类, 初始化的时候就获得 实际执行者的指针,在类内部调用执行者的方法
银行管理者对柜员发号施令:
抽象管理类->管理类->大主管(发号施令者)
柜员(执行者)
.h
//干活的
class Banker
{
public:
void In_Money();
void Out_Money();
};
//抽象命令人
class AbstratCommand
{
public:
virtual void operateBehaivor() = 0;
};
//存钱业务经理
class Command_In :public AbstratCommand
{
public:
Command_In(Banker* B);
virtual void operateBehaivor() override;
void make_business();
private:
Banker* B;
};
//取钱业务经理
class Command_Out :public AbstratCommand
{
public:
Command_Out(Banker* B);
virtual void operateBehaivor() override;
void make_business();
private:
Banker* B;
};
//业务总管
class All_Command
{
public:
All_Command(AbstratCommand* AC);
void FaHaoShiLing(); //发号施令
private:
AbstratCommand* AC;
};
.cpp
int main()
{
Banker* B = new Banker;
Command_In* CI = new Command_In(B);
Command_Out* CO = new Command_Out(B);
All_Command* AC1 = new All_Command(CI);
All_Command* AC2 = new All_Command(CO);
AC1->FaHaoShiLing();
AC2->FaHaoShiLing();
delete AC1;
delete AC2;
delete CO;
delete CI;
delete B;
return 0;
}
void Banker::In_Money()
{
cout << "存钱" << endl;
}
void Banker::Out_Money()
{
cout << "取钱" << endl;
}
Command_In::Command_In(Banker* B)
{
this->B = B;
}
void Command_In::operateBehaivor()
{
make_business();
}
void Command_In::make_business()
{
this->B->In_Money();
}
Command_Out::Command_Out(Banker* B)
{
this->B = B;
}
void Command_Out::operateBehaivor()
{
make_business();
}
void Command_Out::make_business()
{
this->B->Out_Money();
}
All_Command::All_Command(AbstratCommand* AC)
{
this->AC = AC;
}
void All_Command::FaHaoShiLing()
{
this->AC->operateBehaivor();
}
- 后记:看着弯弯绕绕的,实际就是拿到别人的指针,让别人干活
- 后记:领导有多个层级,干活的就一个