diff --git a/DesignPattern/Factory/AbstractFactory/AbstractFactory.cs b/DesignPattern/Factory/AbstractFactory/AbstractFactory.cs new file mode 100644 index 0000000..8232a33 --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/AbstractFactory.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + // Abstract Factory + public abstract class AbstractFactory + { + public abstract ProductA CreateProductA(); + public abstract ProductB CreateProductB(); + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ConcreteFactory1.cs b/DesignPattern/Factory/AbstractFactory/ConcreteFactory1.cs new file mode 100644 index 0000000..d8ba646 --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ConcreteFactory1.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ConcreteFactory1 : AbstractFactory + { + public override ProductA CreateProductA() + { + return new ConcreteProduct1A(); + } + + public override ProductB CreateProductB() + { + return new ConcreteProduct1B(); + } + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ConcreteFactory2.cs b/DesignPattern/Factory/AbstractFactory/ConcreteFactory2.cs new file mode 100644 index 0000000..ebe870f --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ConcreteFactory2.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ConcreteFactory2 : AbstractFactory + { + public override ProductA CreateProductA() + { + return new ConcreteProduct2A(); + } + + public override ProductB CreateProductB() + { + return new ConcreteProduct2B(); + } + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ConcreteProduct1A.cs b/DesignPattern/Factory/AbstractFactory/ConcreteProduct1A.cs new file mode 100644 index 0000000..3168966 --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ConcreteProduct1A.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ConcreteProduct1A : ProductA + { + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ConcreteProduct1B.cs b/DesignPattern/Factory/AbstractFactory/ConcreteProduct1B.cs new file mode 100644 index 0000000..77ec775 --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ConcreteProduct1B.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ConcreteProduct1B : ProductB + { + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ConcreteProduct2A.cs b/DesignPattern/Factory/AbstractFactory/ConcreteProduct2A.cs new file mode 100644 index 0000000..f1ce30d --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ConcreteProduct2A.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ConcreteProduct2A : ProductA + { + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ConcreteProduct2B.cs b/DesignPattern/Factory/AbstractFactory/ConcreteProduct2B.cs new file mode 100644 index 0000000..f323987 --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ConcreteProduct2B.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ConcreteProduct2B : ProductB + { + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ProductA.cs b/DesignPattern/Factory/AbstractFactory/ProductA.cs new file mode 100644 index 0000000..1e889ea --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ProductA.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ProductA + { + } +} diff --git a/DesignPattern/Factory/AbstractFactory/ProductB.cs b/DesignPattern/Factory/AbstractFactory/ProductB.cs new file mode 100644 index 0000000..736f2ac --- /dev/null +++ b/DesignPattern/Factory/AbstractFactory/ProductB.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.AbstractFactory +{ + public class ProductB + { + } +} diff --git a/DesignPattern/Factory/Factory.csproj b/DesignPattern/Factory/Factory.csproj index 179b490..840695b 100644 --- a/DesignPattern/Factory/Factory.csproj +++ b/DesignPattern/Factory/Factory.csproj @@ -34,6 +34,7 @@ + @@ -43,8 +44,25 @@ + + + + + + + + + + + + + + + + + diff --git a/DesignPattern/Factory/FactoryMethod/DbLog.cs b/DesignPattern/Factory/FactoryMethod/DbLog.cs new file mode 100644 index 0000000..db64333 --- /dev/null +++ b/DesignPattern/Factory/FactoryMethod/DbLog.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.FactoryMethod +{ + class DbLog : ILog + { + private string connString; + + public DbLog(string connString) + { + this.connString = connString; + } + + public void write(string s) + { + //... + } + } +} diff --git a/DesignPattern/Factory/FactoryMethod/DbLogFactory.cs b/DesignPattern/Factory/FactoryMethod/DbLogFactory.cs new file mode 100644 index 0000000..6f86ed0 --- /dev/null +++ b/DesignPattern/Factory/FactoryMethod/DbLogFactory.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.FactoryMethod +{ + class DbLogFactory : LogFactory + { + protected override ILog GetLog() + { + string connString = ConfigurationManager.AppSettings["DBConn"]; + return new DbLog(connString); + } + } +} diff --git a/DesignPattern/Factory/FactoryMethod/ILog.cs b/DesignPattern/Factory/FactoryMethod/ILog.cs new file mode 100644 index 0000000..830a323 --- /dev/null +++ b/DesignPattern/Factory/FactoryMethod/ILog.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.FactoryMethod +{ + interface ILog + { + void write(string s); + } +} diff --git a/DesignPattern/Factory/FactoryMethod/LogFactory.cs b/DesignPattern/Factory/FactoryMethod/LogFactory.cs new file mode 100644 index 0000000..8be4a2e --- /dev/null +++ b/DesignPattern/Factory/FactoryMethod/LogFactory.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.FactoryMethod +{ + abstract class LogFactory + { + // factory method 패턴 + protected abstract ILog GetLog(); + + public void Log(string message) + { + ILog logger = GetLog(); // 어떤 로그타입을 사용할 지 결정되지 않음 + logger.write($"{DateTime.Now}: {message}"); + } + + public static LogFactory GetLogger() + { + string logType = ConfigurationManager.AppSettings["LogType"]; + switch (logType) + { + case "DB": + return new DbLogFactory(); + case "XML": + return new XmlLogFactory(); + default: + throw new ApplicationException(); + } + } + } +} diff --git a/DesignPattern/Factory/FactoryMethod/XmlLog.cs b/DesignPattern/Factory/FactoryMethod/XmlLog.cs new file mode 100644 index 0000000..bc89154 --- /dev/null +++ b/DesignPattern/Factory/FactoryMethod/XmlLog.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.FactoryMethod +{ + class XmlLog : ILog + { + private string xmlFile; + + public XmlLog(string xmlFile) + { + this.xmlFile = xmlFile; + } + + public void write(string s) + { + //... + } + } +} diff --git a/DesignPattern/Factory/FactoryMethod/XmlLogFactory.cs b/DesignPattern/Factory/FactoryMethod/XmlLogFactory.cs new file mode 100644 index 0000000..4b16e8d --- /dev/null +++ b/DesignPattern/Factory/FactoryMethod/XmlLogFactory.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.FactoryMethod +{ + class XmlLogFactory : LogFactory + { + protected override ILog GetLog() + { + string logfile = ConfigurationManager.AppSettings["XmlFile"]; + return new XmlLog(logfile); + } + } +} diff --git a/DesignPattern/Factory/Program.cs b/DesignPattern/Factory/Program.cs index f29d8b5..3bd4754 100644 --- a/DesignPattern/Factory/Program.cs +++ b/DesignPattern/Factory/Program.cs @@ -1,4 +1,4 @@ -using Factory.StaticFactoryMethod; + using System; using System.Collections.Generic; using System.Linq; @@ -11,15 +11,40 @@ namespace Factory * 카테고리: 생성패턴 * 개요: 객체를 직접 생성하는 대신 생성하는 기능을 가진 Factory 클래스를 통해 객체를 생성함. * + * static factory method: 정적 메서드가 클래스 또는 서브클래스를 리턴 + * + * simple factory: 공식적은 디자인 패턴은 아니지만, 객체를 생성하는 과정이 복잡한 경우 new를 사용하여 팩토리 생성 후 + * 클래스 또는 서브클래스를 리턴. + * + * factory method: 객체를 생성하는 메서드가 구현 클래스가 아닌 인터페이스 또는 추상클래스에 정의됨. + * 객체를 생성하는 factory 서브클래스가 여러개일 때 유용. + * + * abstract factory: 연관되거나 의존적인 여러 클래스 객체들을 생성하기 위한 인터페이스를 제공함. + * 여러개의 서로 연관된 객체를 복수 개 생성해야 할 때 유용. */ class Program { static void Main(string[] args) { // static factory method - ILogger logger = LoggerFactory.Create(LoggerType.DB); + StaticFactoryMethod.ILogger staticFactoryLogger = StaticFactoryMethod.LoggerFactory.Create(StaticFactoryMethod.LoggerType.DB); + + // simple factory + SimpleFactory.LoggerFactory simpleFactory = new SimpleFactory.LoggerFactory(); + SimpleFactory.DbLogger simpleFactoryLogger = simpleFactory.CreateDbLogger(); + + // factory method1 + FactoryMethod.LogFactory factoryLogger1 = new FactoryMethod.XmlLogFactory(); + factoryLogger1.Log("something to be logged"); + // factory method2 + FactoryMethod.LogFactory factoryLogger2 = FactoryMethod.LogFactory.GetLogger(); + factoryLogger2.Log("something to be logged"); + // abstract factory method + AbstractFactory.AbstractFactory abstractFactorty = new AbstractFactory.ConcreteFactory2(); + AbstractFactory.ProductA prodA = abstractFactorty.CreateProductA(); + AbstractFactory.ProductB prodB = abstractFactorty.CreateProductB(); } } } diff --git a/DesignPattern/Factory/SimpleFactory/DbLogger.cs b/DesignPattern/Factory/SimpleFactory/DbLogger.cs new file mode 100644 index 0000000..08f6095 --- /dev/null +++ b/DesignPattern/Factory/SimpleFactory/DbLogger.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.SimpleFactory +{ + delegate void ErrorDeleagte(); + + class DbLogger + { + public int Timeout { get; set; } + + public ErrorDeleagte Error { get; set; } + } +} diff --git a/DesignPattern/Factory/SimpleFactory/LoggerFactory.cs b/DesignPattern/Factory/SimpleFactory/LoggerFactory.cs new file mode 100644 index 0000000..a85698e --- /dev/null +++ b/DesignPattern/Factory/SimpleFactory/LoggerFactory.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Factory.SimpleFactory +{ + class LoggerFactory + { + public DbLogger CreateDbLogger() + { + DbLogger db = new DbLogger(); + db.Timeout = 60; + db.Error += () => { }; + + return db; + } + } +}