标签:ar sp for on art bs ad ef as
外观工厂模式c#上简单例子
功能:玩家有各自的移动和跳跃功能,通过外观模式实现,将所有玩家移动组合在功能1,将所有玩家跳跃功能组合在功能2中
namespace facade
{
public partial class Form1 : Form
{
private void btnFacade_Click(object sender, EventArgs e)
{
facadeFactory ff = new facadeFactory();//外观模式隐藏了玩家,用户不知道有几个玩家
ff.move();//实现外观功能1
this.listBox1.Items.Add(ff.movestring);
ff.jump();//实现外观功能2
this.listBox1.Items.Add(ff.jumpstring);
}
}
public abstract class play//对所有玩家进行抽象
{
public string movestring { get; set; }
public string jumpstring { get; set; }
public abstract void move();
public abstract void jump();
}
public class play1 : play//玩家1
{
public override void move()
{
movestring = "play1 move";
}
public override void jump()
{
jumpstring = "play1 jump";
}
}
public class play2 : play//玩家2
{
public override void move()
{
movestring = "play2 move";
}
public override void jump()
{
jumpstring = "play2 jump";
}
}
public class facadeFactory//外观模式工厂实现
{
public string movestring;
public string jumpstring;
List<play> plays = new List<play>();
play playone = new play1();
play playtwo = new play2();
public facadeFactory()
{
plays.Add(playone);
plays.Add(playtwo);
}
public void move()//外观模式功能一:两个玩家组队一起移动
{
foreach (play playt in plays)
{
playt.move();
movestring += playt.movestring;
}
}
public void jump()//外观模式功能二:两个玩家组队一起跳跃
{
foreach (play playt in plays)
{
playt.jump();
jumpstring += playt.jumpstring;
}
}
}
}
标签:ar sp for on art bs ad ef as
原文地址:http://www.cnblogs.com/yuyanbian/p/4150261.html