《星际争霸》与设计模式(3)——观察者模式

  昨天我们讲到人族毫无准备的情况下被神族空降金甲虫偷袭了一把,损失惨重,于是人族高层召开会议,总结教训,商讨应对方法,经过商讨,决定在各处设置警报点,并且给该警报点警卫一份名单,要求一旦警报点发出警报时,警卫立即通知名单上的所有部队!而且随着不同局势的变化,可以对名单上的成员进行修改!这一系列部署程序上称为抽象主题。符合这一主题的警报点有很多,比如位置a点的防空塔,这个防空塔程序上称为具体主题,此时要求所有部队,一旦接到警报通知,马上作出防御或还击,没有接到通知的就各干各的事,在程序里就是定义了一个接口或抽象类,制定了一条对警报作出反应的规定,我们称为抽象观察者,a点防空塔的警卫将符合这条规定的4部分人马:scv,坦克1队,坦克2对,隐形战机加入了通知名单(他们都来自不同的类,但都属于抽象观察者的之类,符合接口,能对警报作出反应),只要发生警报就通知者4部分人马!程序里我们称这4部分人马为具体观察者。这样就可以以不变应万变!

下面看看类图

None.gif using  System;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  starcraft.ObserverPattern
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//------------抽象主题角色------------
InBlock.gif

InBlock.gif    
abstract class Alarm    //警报,警报的抽象定义
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
protected string alarmsignal="";    //代表警报器的状态,通常状况下无信号!
InBlock.gif
        protected string place;     //发生警报的位置;
InBlock.gif
        private ArrayList Armys = new ArrayList();   //发生警报时要通知作出反应的部队列表
InBlock.gif
        public Alarm(string where)   //事例化一个警报,代表在某地方设置警报监视器
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.place = where;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Add(ITerranArmy army)    //将某一个部队加入通知列表,可以是坦克,战机……
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Armys.Add(army);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Remove(ITerranArmy army)  //将某一个部队从通知列表中移出
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            Armys.Remove(army);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Notify()     //将警报发给所有要通知的部队,他们将作出相应行动!
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
foreach (ITerranArmy Army in Armys)
InBlock.gif                Army.Forestall(
this);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string Alarmsignal
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn alarmsignal; }
InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                alarmsignal 
= value;
InBlock.gif                Notify();          
//当警报器状态发生警报时,发出警报通知
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string Place
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn place; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ place = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//------------具体主题角色------------
InBlock.gif

InBlock.gif    
class MissileTurret : Alarm   //一个具体的警报,防空塔警报
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public MissileTurret(string where): base(where) dot.gif{ }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//------------抽象观察者角色------------
InBlock.gif

InBlock.gif    
interface ITerranArmy //一个抽象的人族部队
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
void Forestall(Alarm larum);   //当接到某一种警报是作出相应行动来应对警报!
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
//------------3类具体观察者角色------------
InBlock.gif

InBlock.gif    
class TankArmy : ITerranArmy  //坦克部队,一个具体的人族部队
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
private String name ;  //部队名
InBlock.gif
        private String state = "在路口巡逻";   //正常时,坦克部队的状态;
InBlock.gif
        public void Forestall(Alarm larum)   //当接到通知后改变状态,前往警报地点
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.state = "到达" + larum.Place + ",展开,准备开火";
InBlock.gif            Console.WriteLine(
this.Name + "" + this.State);     //将状态输出
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn state; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ state = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public TankArmy(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.name = name;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class WraithArmy : ITerranArmy  //幽灵战机部队,一个具体的人族部队
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
private String name;  //部队名        
InBlock.gif
        private String state = "在前线待命";   //正常时,战机部队的状态;      
InBlock.gif
        public void Forestall(Alarm larum)   //当接到通知后改变状态,前往警报地点
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.state = "飞往" + larum.Place + ",隐形,准备开火";
InBlock.gif            Console.WriteLine(
this.Name + "" + this.State);     //将状态输出
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn state; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ state = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public WraithArmy(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.name = name;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class SCV : ITerranArmy  //采矿农民,一个具体的人族部队
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
private String name;  //部队名
InBlock.gif
        private String state = "采矿";   //正常时,战机部队的状态;
InBlock.gif
        public void Forestall(Alarm larum)   //当接到通知后改变状态,远离警报地点
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.state = "远离" + larum.Place + ",到达安全地区";
InBlock.gif            Console.WriteLine(
this.Name + "" + this.State);     //将状态输出
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn state; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ state = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public SCV(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.name = name;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//----------------------------------------
InBlock.gif

InBlock.gif    
class game   //具体游戏情景
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Alarm alarm 
= new MissileTurret("A点");  //在A点设置一个警报器;
InBlock.gif            
//分别实例化4个人族部队作为我们的观察对象,并记录各个部队的状态,方便大家进行前后对比
InBlock.gif
            SCV scv = new SCV("SCV");
InBlock.gif            TankArmy tank1 
= new TankArmy("坦克部队1");
InBlock.gif            TankArmy tank2 
= new TankArmy("坦克部队2");
InBlock.gif            WraithArmy wraith 
= new WraithArmy("隐形战机");
InBlock.gif
InBlock.gif            Console.WriteLine(
"-----正常状态下各个部队的状态");
InBlock.gif            Console.WriteLine(scv.Name 
+ "" + scv.State);
InBlock.gif            Console.WriteLine(tank1.Name 
+ "" + tank1.State);
InBlock.gif            Console.WriteLine(tank2.Name 
+ "" + tank2.State);
InBlock.gif            Console.WriteLine(wraith.Name 
+ "" + wraith.State);
InBlock.gif            
InBlock.gif            
//将这4个对象加入到警报通知列表;
InBlock.gif
            alarm.Add(scv);
InBlock.gif            alarm.Add(tank1);
InBlock.gif            alarm.Add(tank2);
InBlock.gif            alarm.Add(wraith);
InBlock.gif            
InBlock.gif            Console.WriteLine(
"-----在A点设置的警报器发生了警报信号");
InBlock.gif            alarm.Alarmsignal 
= "发现神族运输机"; //防空塔发现了神族运输机,状态改变;
InBlock.gif            
//大家仔细看看这一步,随着防空塔状态的改变,以上几个部队的状态都改变了!
InBlock.gif
            Console.WriteLine("-----人族部队未损失一兵一卒,击落来侵犯的所有敌军!");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

运行结果:


经过一来一回两次交锋,双方互有胜负,战成平手!局面进入僵持阶段!那么随着游戏的发展人族是如何把握战局,占据主动,迎来最后的胜利,我们慢慢说来!

转载于:https://www.cnblogs.com/dircmpt/archive/2006/12/06/583699.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值