Jimmypony的技术汇总区
很多都不会,很多都不懂,不要浮躁,静心学习

感觉就是策略模式强化版,但是绝非简单的通过状态来管理行为之间的逻辑关系,而是把行为当作状态,简单的说在什么状态下决定执行什么行为是有效的,同时改变下一行为(状态),还有一点是所有代码中没有IF,让当前行为决定做那些事情是有效的.

书上是这么说的:允许对象在内部状态改变是改变他的行为,对象看起来好像是修改了它的类.

以下以冰箱里面有一头鹿怎样把大象放进冰箱的状态关系为例说明:

  1. 打开冰箱
  2. 把鹿拿出冰箱
  3. 把大象塞进去
  4. 把冰箱门关上
Code
#region 接口

/// <summary>
/// 动物接口
/// </summary>
public interface IAnimal
{
String Name {
get; }
}

/// <summary>
/// 行为接口
/// </summary>
public interface IAction
{
void AddAnimal(IAnimal animal);
void RemoveAnimal(IAnimal animal);
void OpenIceBox();
void ClosedIceBox();

}

/// <summary>
/// 冰箱状态
/// </summary>
public interface IIceBox
{
IAction Status {
get; set; }
IAnimal Animal {
get; set; }

IAction OpenIcebox {
get; set; }
IAction InsertAnimal {
get; set; }
IAction DeleteAnimal {
get; set; }
IAction CloseIcebox {
get; set; }
}

#endregion

#region Status

/// <summary>
/// 打开冰箱
/// </summary>
public class OpenIcebox : IAction
{
IIceBox icebox;
public OpenIcebox(IIceBox icebox)
{
this.icebox = icebox;
}

#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
"请先打开冰箱!");
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
"请先打开冰箱!");
}

public void OpenIceBox()
{
Console.WriteLine(
"我打开了冰箱!");
icebox.Status
= icebox.DeleteAnimal;
}

public void ClosedIceBox()
{
Console.WriteLine(
"冰箱还没打开呢!");
}

#endregion
}
/// <summary>
/// 放入动物
/// </summary>
public class InsertAnimal : IAction
{
IIceBox icebox;
public InsertAnimal(IIceBox icebox)
{
this.icebox = icebox;
}
#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
string.Format("把{0}塞入了冰箱!", animal.Name));
icebox.Animal
= animal;
icebox.Status
= icebox.CloseIcebox;
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
"冰箱里面没动物啊!");
}

public void OpenIceBox()
{
Console.WriteLine(
"冰箱已经打开了,不能重复打开!");
}

public void ClosedIceBox()
{
Console.WriteLine(
"还没放动物呢!!");
}

#endregion
}
/// <summary>
/// 拿出动物
/// </summary>
public class DeleteAnimal : IAction
{
IIceBox icebox;
public DeleteAnimal(IIceBox icebox)
{
this.icebox = icebox;
}

#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
"冰箱里面有动物了啊!!");
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
string.Format("移除动物{0}", animal.Name));
icebox.Animal
= null;
icebox.Status
= icebox.InsertAnimal;
}

public void OpenIceBox()
{
Console.WriteLine(
"冰箱已经打开了,不能重复打开!");
}

public void ClosedIceBox()
{
Console.WriteLine(
"还没有移除动物呢");
}

#endregion
}
/// <summary>
/// 关闭冰箱
/// </summary>
public class CloseIcebox : IAction
{
IIceBox icebox;
public CloseIcebox(IIceBox icebox)
{
this.icebox = icebox;
}

#region IAction Members

public void AddAnimal(IAnimal animal)
{
Console.WriteLine(
"冰箱已经塞了动物了!!");
}

public void RemoveAnimal(IAnimal animal)
{
Console.WriteLine(
"现在应该关门了");
}

public void OpenIceBox()
{
Console.WriteLine(
"门是开着的,不能重复打开");
}

public void ClosedIceBox()
{
Console.WriteLine(
"关上了冰箱门");
icebox.Status
= icebox.OpenIcebox;
}

#endregion
}

#endregion

#region 实体

public class Elephant : IAnimal
{

#region IAnimal Members

public string Name
{
get { return "大象"; }
}

#endregion
}

public class Deer : IAnimal
{
#region IAnimal Members

public string Name
{
get { return "中国麋鹿"; }
}

#endregion
}

public class IceBox : IIceBox
{
IAction openIcebox;

public IAction OpenIcebox
{
get { return openIcebox; }
set { openIcebox = value; }
}
IAction insertAnimal;

public IAction InsertAnimal
{
get { return insertAnimal; }
set { insertAnimal = value; }
}
IAction deleteAnimal;

public IAction DeleteAnimal
{
get { return deleteAnimal; }
set { deleteAnimal = value; }
}
IAction closeIcebox;

public IAction CloseIcebox
{
get { return closeIcebox; }
set { closeIcebox = value; }
}

IAction status;

IceBox icebox;
IAnimal animal;

public IceBox()
{
openIcebox
= new OpenIcebox(this);
insertAnimal
= new InsertAnimal(this);
deleteAnimal
= new DeleteAnimal(this);
closeIcebox
= new CloseIcebox(this);

status
= openIcebox;
animal
= new Deer();
}

#region IIceBox Members

public IceBox Status
{
get { return icebox; }
set { icebox = value; }
}

public IAnimal Animal
{
get { return animal; }
set { animal = value; }
}

IAction IIceBox.Status
{
get
{
return status; }
set
{ status
= value; }
}

#endregion

public void Open()
{
status.OpenIceBox();
}
public void Remove(IAnimal animal)
{
status.RemoveAnimal(animal);
}
public void Add(IAnimal animal)
{
status.AddAnimal(animal);
}
public void Close()
{
status.ClosedIceBox();
}

}

#endregion



class Program
{
static void Main(string[] args)
{
Elephant e_1
= new Elephant();
Deer d_1
= new Deer();

IceBox icebox
= new IceBox();

icebox.Open();
Console.WriteLine(
"----------尝试错误第二次的打开门-----------");
icebox.Open();
icebox.Remove(d_1);
Console.WriteLine(
"----------尝试错误再移除大象-----------");
icebox.Remove(d_1);
icebox.Add(e_1);
Console.WriteLine(
"----------尝试错误再添加一头大象-----------");
icebox.Add(e_1);
icebox.Close();
Console.WriteLine(
"----------再关一次门-----------");
icebox.Close();
Console.WriteLine(
"----------关门放大象试试-----------");
icebox.Add(e_1);
Console.Read();
}
}

 

posted on 2008-09-28 14:30  Jim~  阅读(346)  评论(0编辑  收藏  举报