封装对象创建的变化 类LogFactory是所有工厂类的抽象父类,专门负责Log对象的创建. Code:public abstract class Log { public abstract void Write(string target, string logValue); }public class DBLog:Log { public override void Write(string target, string logValue) { Console.WriteLine("Logging {0} to DB {1}.", logValue, target); } }public abstract class LogFactory { public abstract Log Create(); }public class DBLogFactory:LogFactory { public override Log Create() { return new DBLog(); } }class Program { static void Main(string[] args) { LogFactory factory = new DBLogFactory(); Log lg = factory.Create(); lg.Write("DBLogFactory", "DBLogFactoryValue"); Console.ReadLine(); } } 转载于:https://www.cnblogs.com/el-net/articles/1108876.html