【C#】Creational Patterns Simple Factory Mode

简单工厂模式,顾名思义就是抽象工厂的简单版~

共有三个环节~ 分别是 抽象产品、具体产品、生产产品~


学习目标: 简单工厂模式的概念及实务

学习难度: ☆☆☆


using System;namespace ConsoleApp1{    abstract class Game //抽象类别定义产品概念    {        public abstract string Name { get; set; }        public abstract int Price { get; set; }        public abstract void Demo();    }    class AOE3 : Game //描述产品的细节    {        public override string Name { get; set; } = "AOE3";        public override int Price { get; set; } = 300;        public override void Demo()        {            Console.WriteLine("AOE3 is Playing");        }    }    static class GameFactory //工厂生产中...    {        public static Game GetGame(string name) //型别是Game,因为要生产Game,所以回传Game        {            switch (name)            {                case "AOE3":                return new AOE3();                default:                throw new Exception("My factory doesn't' has this game") ;            }        }    }    static class MainProgram //算是Clinet端?!    {        public static void Main()        {            Game AOE3 = GameFactory.GetGame("AOE3");            AOE3.Demo();        }    }}

参考资料:

https://raychiutw.github.io/2019/%E9%9A%A8%E6%89%8B-Design-Pattern-3-%E7%B0%A1%E5%96%AE%E5%B7%A5%E5%BB%A0%E6%A8%A1%E5%BC%8F-Simple-Factory-Pattern/


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章