public abstract class NormalActor { } public abstract class FlyActor { } public abstract class WaterActor { }
上面的三个类都是抽象类,自然会有具体的类来继承这些抽象类
1 2 3
public class NormalActorA : NormalActor { } public class FlyActorA : FlyActor { } public class WaterActorA : WaterActor { }
在游戏运行中就会去使用这些角色的具体类,如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class GameSystem { public void Run() { NormalActorA normalActor1 = new NormalActorA(); NormalActorA normalActor2 = new NormalActorA(); NormalActorA normalActor3 = new NormalActorA(); NormalActorA normalActor4 = new NormalActorA(); NormalActorA normalActor5 = new NormalActorA();
FlyActorA flyActor1 = new FlyActorA(); FlyActorA flyActor2 = new FlyActorA();
WaterActorA waterActor1 = new WaterActorA(); WaterActorA waterActor2 = new WaterActorA(); } }
public abstract class NormalActor { public abstract NormalActor Clone(); } public abstract class FlyActor { public abstract FlyActor Clone(); } public abstract class WaterActor { public abstract WaterActor Clone(); }
public class NormalActorA : NormalActor { public override NormalActor Clone() { return (NormalActor)this.MemberwiseClone(); } } public class FlyActorA : FlyActor { public override FlyActor Clone() { return (FlyActor)this.MemberwiseClone(); } } public class WaterActorA : WaterActor { public override WaterActor Clone() { return (WaterActor)this.MemberwiseClone(); } }
至此,原型模式的代码结构基本完成,下面看看在程序中试怎么来调用的
1 2 3 4 5 6 7 8
public class App { public static void Main() { GameSystem gameSystem = new GameSystem(); gameSystem.Run(new NormalActorA(), new FlyActorA(), new WaterActorA()); } }