static void Main(string[] args) { /*此处将简单工厂模式升级为工厂模式, 在简单工厂模式中,将所有对象的创建都 * 集中在一个类中,增加了if-else的嵌套,而在工厂模式中,将工厂Factory设置 * 为抽象类,在Factory的子类中实现具体的某个对象的创建,从而减少if-else的 * 嵌套,另外,此时如果增加其他的日志文件类型,只需要增加一个新的Factory的 * 子类即可,而不用更改其他的类 */ ILoggerFactory factory1, factory2; ILogger logger1, logger2; factory1 = new FileLoggerFactory(); logger1 = factory1.createLogger(); logger1.writeLogger(); factory2 = new CountLoggerFactory(); logger2 = factory2.createLogger(); logger2.writeLogger(); }
static void Main(string[] args) { ILoggerFactory fileLoggerFactory = new FileLoggerFactory(); ILoggerFactory databaseLoggerFactory = new DatabaseLoggerFactory(); //针对抽象工厂接口编程 ILogger fileLogger = fileLoggerFactory.CreateLogger(); ILogger databaseLogger = databaseLoggerFactory.CreateLogger(); fileLogger.Log(); databaseLogger.Log(); IImageReaderFactory gifReaderFactory = new GifReaderFactory(); IImageReaderFactory jpgReaderFactory = new JPGReaderFactory(); IImageReaderFactory bmpReaderFactory = new BMPReaderFactory(); IImageReader gifReader = gifReaderFactory.GetImageReader(); IImageReader jpgReader = jpgReaderFactory.GetImageReader(); IImageReader bmpReader = bmpReaderFactory.GetImageReader(); gifReader.Load(@"E:\Media\Images\壁纸\ (1).jpg"); jpgReader.Load(@"E:\Media\Images\壁纸\ (1).jpg"); bmpReader.Load(@"E:\Media\Images\壁纸\ (1).jpg"); Console.ReadKey(); }