标签:创建 实体类 creat 设计模式 客户端 return int ctf class
1.创建抽象工厂类
public abstract class AbstractFactory { public abstract Connect CreatConnet(); public abstract Select CreatSelect(); }
2.创建两个抽象类
public abstract class Connect { public abstract void Print(); } public abstract class Select { public abstract void Print(); }
3.创建两个实体类
public class SqlConnect : Connect { public override void Print() { //throw new NotImplementedException(); Console.Write("Sql Connect"); } } public class SqlSelect : Select { public override void Print() { Console.Write("Sql Select"); } } public class MysqlConnect : Connect { public override void Print() { Console.Write("MySql Connect"); } } public class MysqlSelect : Select { public override void Print() { Console.WriteLine("MySql Select"); } }
4.创建实体工厂类
public class SqlFactory : AbstractFactory { public override Connect CreatConnet() { return new SqlConnect(); } public override Select CreatSelect() { return new SqlSelect(); } } public class MySqlFactory : AbstractFactory { public override Connect CreatConnet() { return new MysqlConnect(); } public override Select CreatSelect() { return new MysqlSelect(); } }
5.客户端调用
static void Main(string[] args) { AbstractFactory sqlFactory = new SqlFactory(); AbstractFactory mysqlFactory = new MySqlFactory(); Connect sqlConnect = sqlFactory.CreatConnet(); Connect mysqlConnect = mysqlFactory.CreatConnet(); sqlConnect.Print(); mysqlConnect.Print(); Select sqlSelect = sqlFactory.CreatSelect(); Select mysqlSelect = mysqlFactory.CreatSelect(); sqlSelect.Print(); mysqlSelect.Print(); Console.Read(); }
标签:创建 实体类 creat 设计模式 客户端 return int ctf class
原文地址:http://www.cnblogs.com/yizhituoxie/p/7851759.html