外觀模式(facade)C#
外觀工廠模式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
plays = new List();
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;
}
}
}
}