扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
意图
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
场景
还是上次说的那个网络
我们知道,这样的一个游戏不可能只有一张地图,而且地图的数量肯定是会一直增加的。如果游戏在初始化
示例代码
以下是引用片段: using System; using System.Reflection; namespace AbstractFactoryExample { class Program { static void Main(string[] args) { Patrix patrix = new Patrix(); patrix.LoadScene("HalfPaper"); patrix.LoadScene("Matrix"); } } class Patrix { private PatrixSceneFactory GetGameScene(string gameSceneName) { return (PatrixSceneFactory)Assembly.Load("AbstractFactoryExample").CreateInstance("AbstractFactoryExample." + gameSceneName); } public void LoadScene(string gameSceneName) { PatrixSceneFactory psf = GetGameScene(gameSceneName); Texture texture = psf.CreateTexture(); Model model = psf.CreateModel(); model.FillTexture(texture); } } abstract class PatrixSceneFactory { public abstract Model CreateModel(); public abstract Texture CreateTexture(); } abstract class Model { public abstract void FillTexture(Texture texture); } abstract class Texture { } class HalfPaper : PatrixSceneFactory { public override Model CreateModel() { return new HalfPaperModel(); } public override Texture CreateTexture() { return new HalfPaperTexture(); } } class HalfPaperModel : Model { public HalfPaperModel() { Console.WriteLine("HalfPaper Model Created"); } public override void FillTexture(Texture texture) { Console.WriteLine("HalfPaper Model is filled Texture"); } } class HalfPaperTexture : Texture { public HalfPaperTexture() { Console.WriteLine("HalfPaper Texture Created"); } } class Matrix : PatrixSceneFactory { public override Model CreateModel() { return new MatrixModel(); } public override Texture CreateTexture() { return new MatrixTexture(); } } class MatrixModel : Model { public MatrixModel() { Console.WriteLine("Matrix Model Created"); } public override void FillTexture(Texture texture) { Console.WriteLine("Matrix Model is filled Texture"); } } class MatrixTexture : Texture { public MatrixTexture() { Console.WriteLine("Matrix Texture Created"); } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者