版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】设计模式5行为模式
设计模式5行为模式行为模式,目录:
模式方法模式
命令模式
策略模式观察者模式模板方法模式:冲咖啡,冲茶水chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
//抽象的制作饮料方法
class
MakeDrink
{
public:
//1
把水煮开
void
boil()
{
cout
<<
"把水煮开"
<<
endl;
}
//2
冲某物
virtual
void
brew()
=
0;
//3
从大杯倒入小杯
void
putInCup()
{
cout
<<
"把冲泡好的饮料
从大杯倒入小杯"
<<
endl;
}
//4
加一些酌料
virtual
void
addThings()
=
0;
//钩子函数,
hook
virtual
bool
CustomWantAddThings()
{
return
true;
}
//业务的逻辑的统一模板
void
make()
{
boil();
brew();
//子类
putInCup();
if
(CustomWantAddThings()
==
true)
{
addThings();
//子类的多态
}
}
};
//制作咖啡
class
MakeCoffee
:public
MakeDrink
{
public:
MakeCoffee(bool
isAdd)
{
this->isAdd
=
isAdd;
}
//2
冲某物
virtual
void
brew()
{
cout
<<
"冲泡咖啡豆"
<<
endl;
}
//4
加一些酌料
virtual
void
addThings()
{
cout
<<
"添加糖和牛奶"
<<
endl;
}
virtual
bool
CustomWantAddThings()
{
return
isAdd;
}
private:
bool
isAdd;
};
//冲泡茶叶
class
MakeTea
:public
MakeDrink
{
public:
MakeTea(bool
isAdd)
{
this->isAdd
=
isAdd;
}
//2
冲某物
virtual
void
brew()
{
cout
<<
"冲泡
茶叶"
<<
endl;
}
//4
加一些酌料
virtual
void
addThings()
{
cout
<<
"添加
柠檬
或者
菊花"
<<
endl;
}
virtual
bool
CustomWantAddThings()
{
return
isAdd;
}
private:
bool
isAdd;
};
int
main(void)
{
MakeDrink
*makeCoffee
=
new
MakeCoffee(true);
makeCoffee->make();
cout
<<
"
"
<<
endl;
MakeDrink
*makeTea
=
new
MakeTea(false);
makeTea->make();
return
0;
}
chunli@linux:~$
g++
main.cpp
&&
./a.out
把水煮开
冲泡咖啡豆
把冲泡好的饮料
从大杯倒入小杯
添加糖和牛奶
把水煮开
冲泡
茶叶
把冲泡好的饮料
从大杯倒入小杯
chunli@linux:~$模板方法模式中的角色和职责
AbstractClass(抽象类):在抽象类中定义了一系列基本操作(PrimitiveOperations),这些基本操作可以是具体的,也可以是抽象的,每一个基本操作对应算法的一个步骤,在其子类中可以重定义或实现这些步骤。同时,在抽象类中实现了一个模板方法(TemplateMethod),用于定义一个算法的框架,模板方法不仅可以调用在抽象类中实现的基本方法,也可以调用在抽象类的子类中实现的基本方法,还可以调用其他对象中的方法。
ConcreteClass(具体子类):它是抽象类的子类,用于实现在父类中声明的抽象基本操作以完成子类特定算法的步骤,也可以覆盖在父类中已经实现的具体基本操作。
模板方法的优缺点
优点:
(1)在父类中形式化地定义一个算法,而由它的子类来实现细节的处理,在子类实现详细的处理算法时并不会改变算法中步骤的执行次序。
(2)模板方法模式是一种代码复用技术,它在类库设计中尤为重要,它提取了类库中的公共行为,将公共行为放在父类中,而通过其子类来实现不同的行为,它鼓励我们恰当使用继承来实现代码复用。
(3)可实现一种反向控制结构,通过子类覆盖父类的钩子方法来决定某一特定步骤是否需要执行。
(4)在模板方法模式中可以通过子类来覆盖父类的基本方法,不同的子类可以提供基本方法的不同实现,更换和增加新的子类很方便,符合单一职责原则和开闭原则。
缺点:
需要为每一个基本方法的不同实现提供一个子类,如果父类中可变的基本方法太多,将会导致类的个数增加,系统更加庞大,设计也更加抽象。适用场景
(1)具有统一的操作步骤或操作过程;
(2)具有不同的操作细节;
(3)存在多个具有同样操作步骤的应用场景,但某些具体的操作细节却各不相同;
在抽象类中统一操作步骤,并规定好接口;让子类实现接口。这样可以把各个具体的子类和操作步骤解耦合。
命令模式:病人看病直接找医生,耦合度太高.
chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
class
Doctor
{
public:
void
treatEyes()
{
cout
<<"treat
eyes
"
<<
endl;
}
void
treatNose()
{
cout
<<"treat
nose"
<<
endl;
}
};
int
main(void)
{
Doctor
*doctor
=
new
Doctor;
doctor->treatEyes();
doctor->treatNose();
return
0;
}
chunli@linux:~$
g++
main.cpp
&&
./a.out
treat
eyes
treat
nose
chunli@linux:~$拿着病单来看医生,医生只跟病单打交道
chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
class
Doctor
{
public:
void
treatEyes()
{
cout
<<"treat
eyes
"
<<
endl;
}
void
treatNose()
{
cout
<<"treat
nose"
<<
endl;
}
};
class
CommandEyes
{
public:
CommandEyes(Doctor*
doctor)
{
this->doctor
=
doctor;
}
~CommandEyes()
{
if(this->doctor
!=
NULL)
{
delete
this->doctor;
this->doctor
=
NULL;
}
}
void
treat()
{
this->doctor->treatEyes();
}
private:
Doctor
*doctor;
};
class
CommandNose
{
public:
CommandNose(Doctor*
doctor)
{
this->doctor
=
doctor;
}
~CommandNose()
{
if(this->doctor
!=
NULL)
{
delete
this->doctor;
this->doctor
=
NULL;
}
}
void
treat()
{
this->doctor->treatNose();
}
private:
Doctor
*doctor;
};
int
main(void)
{
//拿着病单来看医生
CommandEyes
*commandEyes
=new
CommandEyes(new
Doctor);
commandEyes->treat();
CommandNose
*commandNose
=new
CommandNose(new
Doctor);
commandNose->treat();
return
0;
}
chunli@linux:~$
g++
main.cpp
&&
./a.out
treat
eyes
treat
nose
chunli@linux:~$将病单抽象出来
chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
class
Doctor
{
public:
void
treatEyes()
{
cout
<<"treat
eyes
"
<<
endl;
}
void
treatNose()
{
cout
<<"treat
nose"
<<
endl;
}
};
class
Command
{
public:
Command(Doctor
*doctor)
{
this->doctor
=
doctor;
}
~Command()
{
if(this->doctor
!=
NULL)
{
delete
this->doctor;
this->doctor
=
NULL;
}
}
virtual
void
treat()
=
0;
protected:
Doctor
*doctor;
};
class
CommandEyes:public
Command
{
public:
CommandEyes(Doctor*
doctor):Command(doctor)
{}
void
treat()
{
this->doctor->treatEyes();
}
};
class
CommandNose:public
Command
{
public:
CommandNose(Doctor*
doctor):Command(doctor)
{}
void
treat()
{
this->doctor->treatNose();
}
};
int
main(void)
{
Command
*comd1
=new
CommandEyes(new
Doctor);
comd1->treat();
Command
*comd2
=new
CommandNose(new
Doctor);
comd2->treat();
return
0;
}
chunli@linux:~$
g++
main.cpp
&&
./a.out
treat
eyes
treat
nose
chunli@linux:~$让护士接病单,通知病人来看病:
chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
class
Doctor
{
public:
void
treatEyes()
{
cout
<<"treat
eyes
"
<<
endl;
}
void
treatNose()
{
cout
<<"treat
nose"
<<
endl;
}
};
class
Command
{
public:
Command(Doctor
*doctor)
{
this->doctor
=
doctor;
}
~Command()
{
if(this->doctor
!=
NULL)
{
delete
this->doctor;
this->doctor
=
NULL;
}
}
virtual
void
treat()
=
0;
protected:
Doctor
*doctor;
};
class
CommandEyes:public
Command
{
public:
CommandEyes(Doctor*
doctor):Command(doctor)
{}
void
treat()
{
this->doctor->treatEyes();
}
};
class
CommandNose:public
Command
{
public:
CommandNose(Doctor*
doctor):Command(doctor)
{}
void
treat()
{
this->doctor->treatNose();
}
};
class
Nurse
{
public:
Nurse(Command
*cmd)
{
this->cmd
=
cmd;
}
~Nurse()
{
if(this->cmd
!=
NULL)
{
delete
this->cmd;
this->cmd
=
NULL;
}
}
void
notify()
{
this->cmd->treat();
}
private:
Command
*cmd;
};
int
main(void)
{
Nurse
*nurse
=
new
Nurse(new
CommandEyes(new
Doctor));
nurse->notify();
Nurse
*nurse2
=
new
Nurse(new
CommandNose(new
Doctor));
nurse2->notify();
return
0;
}
chunli@linux:~$
g++
main.cpp
&&
./a.out
treat
eyes
treat
nose
chunli@linux:~$把病单全部给护士长,让护士长下发
chunli@linux:~$
cat
main.cpp
#include
<iostream>
#include
<list>
using
namespace
std;
class
Doctor
{
public:
void
treatEyes()
{
cout
<<"treat
eyes
"
<<
endl;
}
void
treatNose()
{
cout
<<"treat
nose"
<<
endl;
}
};
class
Command
{
public:
Command(Doctor
*doctor)
{
this->doctor
=
doctor;
}
virtual
~Command()
{
if(this->doctor
!=
NULL)
{
delete
this->doctor;
this->doctor
=
NULL;
}
}
virtual
void
treat()
=
0;
protected:
Doctor
*doctor;
};
class
CommandEyes:public
Command
{
public:
CommandEyes(Doctor*
doctor):Command(doctor)
{}
void
treat()
{
this->doctor->treatEyes();
}
};
class
CommandNose:public
Command
{
public:
CommandNose(Doctor*
doctor):Command(doctor)
{}
void
treat()
{
this->doctor->treatNose();
}
};
class
Nurse
{
public:
Nurse(Command
*cmd)
{
this->cmd
=
cmd;
}
~Nurse()
{
if(this->cmd
!=
NULL)
{
delete
this->cmd;
this->cmd
=
NULL;
}
}
void
notify()
{
this->cmd->treat();
}
private:
Command
*cmd;
};
class
NurseBoss
{
public:
NurseBoss()
{
m_list.clear();
}
~NurseBoss()
{
m_list.clear();
}
void
setCmd(Command
*cmd)
{
m_list.push_back(cmd);
}
void
notify()
{
for(list<Command*>::iterator
it
=
m_list.begin();it!=m_list.end();it++)
{
(*it)->treat();
}
}
private:
list<Command*>
m_list;
};
int
main(void)
{
NurseBoss
*woman
=
new
NurseBoss;
Command
*cmd1
=
new
CommandEyes(new
Doctor);
Command
*cmd2
=
new
CommandNose(new
Doctor);
woman->setCmd(cmd1);
woman->setCmd(cmd2);
woman->notify();
return
0;
}
chunli@linux:~$
g++
main.cpp
-Wall
&&
./a.out
treat
eyes
treat
nose
chunli@linux:~$命令模式,路边吃烤串案例
chunli@linux:~$
cat
main.cpp
#include
<iostream>
#include
<list>
using
namespace
std;
class
Cooker
{
public:
void
makeChuaner()
{
cout
<<
"正在烤串
"
<<
endl;
}
void
makeChicken()
{
cout
<<
"正在烤鸡翅
"
<<
endl;
}
};
class
Command
{
public:
Command(Cooker
*
cooker)
{
this->cooker
=
cooker;
}
virtual
~Command()
{
if(this->cooker
!=
NULL)
{
delete
this->cooker;
this->cooker
=
NULL;
}
}
virtual
void
execute()
=
0;
protected:
Cooker
*
cooker;
};
class
CommandChuaner:public
Command
{
public:
CommandChuaner(Cooker
*
cooker):Command(cooker)
{}
virtual
void
execute()
{
this->cooker->makeChuaner();
}
};
class
CommandChicken:public
Command
{
public:
CommandChicken(Cooker
*
cooker):Command(cooker)
{}
virtual
void
execute()
{
this->cooker->makeChicken();
}
};
class
Waitress
{
public:
Waitress()
{
this->m_list.clear();
}
~Waitress()
{
this->m_list.clear();
}
void
setCmd(Command
*cmd)
{
this->m_list.push_back(cmd);
}
void
notify()
{
list<Command*>::iterator
it
=
m_list.begin();
for(;it
!=
m_list.end();it++)
{
(*it)->execute();//在此发生了多态
}
}
private:
list<Command*>
m_list;
};
int
main(void)
{
Waitress
*mm
=
new
Waitress; //喊一个服务器
Command
*chuaner
=
new
CommandChuaner(new
Cooker);//点菜单
Command
*chicken
=
new
CommandChicken(new
Cooker);//点菜单
mm->setCmd(chuaner);//服务员登记
mm->setCmd(chicken);
mm->notify(); //服务员端菜
return
0;
}
chunli@linux:~$
g++
main.cpp
-Wall
&&
./a.out
正在烤串
正在烤鸡翅
chunli@linux:~$命令模式中的角色和职责[图]
Command(抽象命令类):抽象命令类一般是一个抽象类或接口,在其中声明了用于执行请求的execute()等方法,通过这些方法可以调用请求接收者的相关操作。
ConcreteCommand(具体命令类):具体命令类是抽象命令类的子类,实现了在抽象命令类中声明的方法,它对应具体的接收者对象,将接收者对象的动作绑定其中。在实现execute()方法时,将调用接收者对象的相关操作(Action)。
Invoker(调用者):调用者即请求发送者,它通过命令对象来执行请求。一个调用者并不需要在设计时确定其接收者,因此它只与抽象命令类之间存在关联关系。在程序运行时可以将一个具体命令对象注入其中,再调用具体命令对象的execute()方法,从而实现间接调用请求接收者的相关操作。
Receiver(接收者):接收者执行与请求相关的操作,它具体实现对请求的业务处理。
5.2.3命令模式的优缺点
优点:
(1)降低系统的耦合度。由于请求者与接收者之间不存在直接引用,因此请求者与接收者之间实现完全解耦,相同的请求者可以对应不同的接收者,同样,相同的接收者也可以供不同的请求者使用,两者之间具有良好的独立性。
(2)新的命令可以很容易地加入到系统中。由于增加新的具体命令类不会影响到其他类,因此增加新的具体命令类很容易,无须修改原有系统源代码,甚至客户类代码,满足“开闭原则”的要求。
(3)可以比较容易地设计一个命令队列或宏命令(组合命令)。
缺点:
使用命令模式可能会导致某些系统有过多的具体命令类。因为针对每一个对请求接收者的调用操作都需要设计一个具体命令类,因此在某些系统中可能需要提供大量的具体命令类,这将影响命令模式的使用。
5.2.4适用场景
(1)系统需要将请求调用者和请求接收者解耦,使得调用者和接收者不直接交互。请求调用者无须知道接收者的存在,也无须知道接收者是谁,接收者也无须关心何时被调用。
(2)系统需要在不同的时间指定请求、将请求排队和执行请求。一个命令对象和请求的初始调用者可以有不同的生命期,换言之,最初的请求发出者可能已经不在了,而命令对象本身仍然是活动的,可以通过该命令对象去调用请求接收者,而无须关心请求调用者的存在性,可以通过请求日志文件等机制来具体实现。
(3)系统需要将一组操作组合在一起形成宏命令。
===========================================策略模式,英雄更换装备战斗场景
chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
class
AbstractStrategy//抽象的策略
{
public:
virtual
void
useWeapon()
=
0;
};
class
KnifeStrategy:public
AbstractStrategy
{
public:
virtual
void
useWeapon()
{
cout
<<
"使用
×××
"
<<
endl;
}
};
class
AKStrategy:public
AbstractStrategy
{
public:
virtual
void
useWeapon()
{
cout
<<
"使用
AK47
"
<<
endl;
}
};
class
Hero
{
public:
Hero()
{
strategy
=
NULL;
}
void
setStrategy(AbstractStrategy
*
strategy)
{
this->strategy
=
strategy;
}
void
fight()
{
cout
<<
"英雄开始战斗"
<<
endl;
this->strategy->useWeapon();
}
private:
AbstractStrategy
*strategy;
};
int
main(void)
{
AbstractStrategy
*
knife
=
new
KnifeStrategy;
AbstractStrategy
*
ak47
=
new
AKStrategy;
Hero
*hero
=
new
Hero
;
cout
<<
"兵快来了,换AK47"
<<
endl;
hero->setStrategy(ak47);
hero->fight();
cout
<<
"近距离搏斗,换AK47"
<<
endl;
hero->setStrategy(knife);
hero->fight();
return
0;
}
chunli@linux:~$
g++
main.cpp
-Wall
&&
./a.out
兵快来了,换AK47
英雄开始战斗
使用
AK47
近距离搏斗,换AK47
英雄开始战斗
使用
×××
chunli@linux:~$策略模式,不同时间不同的销售模式
chunli@linux:~$
cat
main.cpp
#include
<iostream>
using
namespace
std;
class
AbstractStrategy
{
public:
virtual
double
getPrice(double
price)
=
0;
};
class
StrategyA:public
AbstractStrategy//8折策略
{
public:
virtual
double
getPrice(double
price)
{
return
price*0.8;
}
};
class
StrategyB:public
AbstractStrategy
//满200减100策略
{
public:
virtual
double
getPrice(double
price)
{
if(price
>
200)
{
price
-=
100;
}
return
price;
}
};
class
Item
{
public:
Item(string
name,double
price)
{
this->name
=
name;
this->price
=
price;
}
double
SellPrice()
{
return
this->strategy->getPrice(this->price);
}
void
setStrategy(AbstractStrategy
*
strategy)
{
this->strategy
=
strategy;
}
private:
string
name;
double
price;
AbstractStrategy
*
strategy;
};
int
main(void)
{
Item
it("nike鞋",201);
AbstractStrategy
*sa
=
new
StrategyA;
AbstractStrategy
*sb
=
new
StrategyB;
cout
<<
"上午,全场8折"
<<
endl;
it.setStrategy(sa);
cout
<<
"nike鞋应该卖"
<<it.SellPrice()
<<
endl;
cout
<<
"下午,全场满200减100"
<<
endl;
it.setStrategy(sb);
cout
<<
"nike鞋应该卖"
<<it.SellPrice()
<<
endl;
return
0;
}
chunli@linux:~$
g++
main.cpp
-Wall
&&
./a.out
上午,全场8折
nike鞋应该卖160.8
下午,全场满200减100
nike鞋应该卖101
chunli@linux:~$策略模式中的角色和职责[图]
Context(环境类):环境类是使用算法的角色,它在解决某个问题(即实现某个方法)时可以采用多种策略。在环境类中维持一个对抽象策略类的引用实例,用于定义所采用的策略。
Strategy(抽象策略类):它为所支持的算法声明了抽象方法,是所有策略类的父类,它可以是抽象类或具体类,也可以是接口。环境类通过抽象策略类中声明的方法在运行时调用具体策略类中实现的算法。
ConcreteStrategy(具体策略类):它实现了在抽象策略类中声明的算法,在运行时,具体策略类将覆盖在环境类中定义的抽象策略类对象,使用一种具体的算法实现某个业务处理。
5.3.3策略模式的优缺点
优点:
(1)策略模式提供了对“开闭原则”的完美支持,用户可以在不修改原有系统的基础上选择算法或行为,也可以灵活地增加新的算法或行为。
(2)
使用策略模式可以避免多重条件选择语句。多重条件选择语句不易维护,它把采取哪一种算法或行为的逻辑与算法或行为本身的实现逻辑混合在一起,将它们全部硬编码(HardCoding)在一个庞大的多重条件选择语句中,比直接继承环境类的办法还要原始和落后。
(3)
策略模式提供了一种算法的复用机制。由于将算法单独提取出来封装在策略类中,因此不同的环境类可以方便地复用这些策略类。
缺点:
(1)
客户端必须知道所有的策略类,并自行决定使用哪一个策略类。这就意味着客户端必须理解这些算法的区别,以便适时选择恰当的算法。换言之,策略模式只适用于客户端知道所有的算法或行为的情况。
(2)
策略模式将造成系统产生很多具体策略类,任何细小的变化都将导致系统要增加一个新的具体策略类。
5.3.4适用场景
准备一组算法,并将每一个算法封装起来,使得它们可以互换。
=========================================观察者模式,班长给学生放风,学生抄作业
chunli@linux:~$
cat
main.cpp
#include
<iostream>
#include
<list>
using
namespace
std;
//
//抽象层的观察者
class
Listener//学生
{
public:
virtual
void
DoSomething()
=
0;//该干的
virtual
void
DoOtherthing()
=
0;//不该干的
};
//抽象的被观察者
class
Notify//班长
{
public:
virtual
void
addlistener(Listener
*listener)
=
0;//添加学生
virtual
void
dellistener(Listener
*listener)
=
0;//删除学生
virtual
void
notify()
=
0;
//通知学生
};
//
class
Student:public
Listener//学生
{
public:
Student(string
name,string
badthing)
{
this->name
=
name;
this->badthing
=
badthing;
}
virtual
void
DoSomething()
{
cout
<<
"学生"
<<name
<<
"停止"<<
badthing
<<
",改为写作业
"
<<
endl;
}
virtual
void
DoOtherthing()
{
cout
<<
"学生"
<<name
<<
"目前正在"<<
badthing
<<
endl;
}
private:
string
name;
string
badthing;
};
class
Monitor:public
Notify
{
public:
virtual
void
addlistener(Listener
*listener)
{
this->m_list.push_back(listener);
}
virtual
void
dellistener(Listener
*listener)
{
this->m_list.remove(listener);
}
virtual
void
notify()
{
list<Listener*>::iterator
it
=
m_list.begin();
for(;it!=
m_list.end();it++)
{
(*it)->DoSomething();
}
}
private:
list<Listener*>
m_list;
};
int
main(void)
{
Listener
*s1
=
new
Student("吴用","智取生辰纲");
Listener
*s2
=
new
Student("杨志","买刀");
Listener
*s3
=
new
Student("武松","醉打蒋门神");
Notify
*master
=
new
Monitor;
master->addlistener(s1);
master->addlistener(s2);
master->addlistener(s3);
cout
<<
"老师没有来,大家可以抄作业"
<<
endl;
s1->DoOtherthing();
s2->DoOtherthing();
s3->DoOtherthing();
cout
<<
"老师来了,大家该干啥干啥"
<<
endl;
master->notify();
return
0;
}
chunli@linux:~$
chunli@linux:~$
g++
main.cpp
-Wall
&&
./a.out
老师没有来,大家可以抄作业
学生吴用目前正在智取生辰纲
学生杨志目前正在买刀
学生武松目前正在醉打蒋门神
老师来了,大家该干啥干啥
学生吴用停止智取生辰纲,改为写作业
学生杨志停止买刀,改为写作业
学生武松停止醉打蒋门神,改为写作业
chunli@linux:~$观察者模式,武林高手对决场景
chunli@linux:~$
cat
main.cpp
#include
<iostream>
#include
<string>
#include
<list>
using
namespace
std;
//前置声明notifier
class
Notifier;
//抽象的观察者
class
Linstenner
{
public:
//当朋友被揍了我改怎么办
virtual
void
onFriendBeFight(Linstenner
*one,
Linstenner
*another/*,
Notifier
*baixiao*/)
=
0;
//one是打别人的人,
another
是被揍的
virtual
void
fighting(Linstenner
*another,
Notifier
*notifier)
=
0;
virtual
string
getName()
=
0;
virtual
string
getParty()
=
0;
};
//抽象的通知者
class
Notifier
{
public:
//添加观察者
virtual
void
addListenner(Linstenner
*listenner)
=
0;
//删除观察者
virtual
void
delListenner(Linstenner
*listenner)
=
0;
//通知观察者
virtual
void
notify(Linstenner
*one,
Linstenner
*another)
=
0;
};
//
武林中的人物
class
Hero
:public
Linstenner
{
public:
Hero(string
name,
string
party)
{
this->name
=
name;
this->party
=
party;
}
//当我发现一个消息之后我该怎么办
virtual
void
onFriendBeFight(Linstenner
*one,
Linstenner
*another/*,
Notifier
*baixiao*/)
{
if
(another->getName()
!=
this->name
&&one->getName()
!=
this->name)
{
//不是当事人
//如果不是当事人,需要判断打人的pary
和
被打的party是不是我自己哥们
if
(one->getParty()
==
this->party)
{
//自己人把
比人揍了
cout
<<
name
<<
"发现自己人把别人揍了,
笑了
,
拍手叫好"
<<
endl;
}
else
if
(another->getParty()
==
this->party){
//自己人被揍了
cout
<<
name
<<
"发现自己人被别人走了,
出手援救"
<<
endl;
//this->fighting(one,
baixiao);
}
}
else
{
//当事人
//如果是当事人,什么都不敢
}
}
//揍人的方法
virtual
void
fighting(Linstenner
*another,
Notifier
*notifier)
{
cout
<<
name
<<
"["
<<
this->party
<<
"]"
<<
"
把
"
<<
another->getName()
<<
"["
<<
another->getParty()
<<
"]"
<<
"给揍了"
<<
endl;
//
揍完之后,这个事件应该让百晓生知晓
//应该调用百晓生
的notify方法
notifier->notify(this,
another);
}
string
getName()
{
return
this->name;
}
string
getParty()
{
return
this->party;
}
private:
string
name;
string
party;
};
class
Baixiao
:public
Notifier
{
public:
//添加观察者
virtual
void
addListenner(Linstenner
*listenner)
{
this->l_list.push_back(listenner);
}
//删除观察者
virtual
void
delListenner(Linstenner
*listenner)
{
this->l_list.remove(listenner);
}
//通知观察者
virtual
void
notify(Linstenner
*one,
Linstenner
*another)
{
for
(list<Linst
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高三年级下册一模大联考语文试题(含有答案)
- 小议中学语文阅读教学艺术
- 电话营销技巧与开场白
- 七年级下册英语Unit1-12全册课文翻译
- 中班奥尔夫音乐教案大全
- 辽宁省大连市2024届高三年级下册第一次模拟考试数学试卷(含答案解析)
- 模块化分工条件下网络状产业链的基本构造与运行机制研究
- 钢筋施工安全技术交底
- 制造业服务化研究成因、机理与效应
- 北斗卫星导航系统美国全球定位系统载波相位相对定位全球精度分析
- 展览布展方案
- 盐焗鸡的商业计划书
- 薪酬沟通方案
- 工程监理专业认识实习课件
- 自动驾驶未来发展趋势报告
- 选择性必修3 《逻辑与思维》(主观题答题模版)高考政治答题技巧与模板构建
- AI人工智能实验室整体规划建设方案
- 肺良性肿瘤教学查房
- 历史是一群喵
- 软件内测报告
- 2023年12月江苏省如皋市委宣传部和卫健系统部分单位下半年公开招聘10名合同制工作人员笔试历年高频考点-难、易错点荟萃附答案带详解
评论
0/150
提交评论