factory pattern

remotes/origin/master
syneffort 3 years ago
parent c6d59b8c4b
commit f29f2ef0d2
  1. 15
      DesignPattern/Factory/AbstractFactory/AbstractFactory.cs
  2. 21
      DesignPattern/Factory/AbstractFactory/ConcreteFactory1.cs
  3. 21
      DesignPattern/Factory/AbstractFactory/ConcreteFactory2.cs
  4. 12
      DesignPattern/Factory/AbstractFactory/ConcreteProduct1A.cs
  5. 12
      DesignPattern/Factory/AbstractFactory/ConcreteProduct1B.cs
  6. 12
      DesignPattern/Factory/AbstractFactory/ConcreteProduct2A.cs
  7. 12
      DesignPattern/Factory/AbstractFactory/ConcreteProduct2B.cs
  8. 12
      DesignPattern/Factory/AbstractFactory/ProductA.cs
  9. 12
      DesignPattern/Factory/AbstractFactory/ProductB.cs
  10. 18
      DesignPattern/Factory/Factory.csproj
  11. 23
      DesignPattern/Factory/FactoryMethod/DbLog.cs
  12. 18
      DesignPattern/Factory/FactoryMethod/DbLogFactory.cs
  13. 13
      DesignPattern/Factory/FactoryMethod/ILog.cs
  14. 35
      DesignPattern/Factory/FactoryMethod/LogFactory.cs
  15. 23
      DesignPattern/Factory/FactoryMethod/XmlLog.cs
  16. 18
      DesignPattern/Factory/FactoryMethod/XmlLogFactory.cs
  17. 29
      DesignPattern/Factory/Program.cs
  18. 17
      DesignPattern/Factory/SimpleFactory/DbLogger.cs
  19. 20
      DesignPattern/Factory/SimpleFactory/LoggerFactory.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();
}
}

@ -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();
}
}
}

@ -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();
}
}
}

@ -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
{
}
}

@ -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
{
}
}

@ -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
{
}
}

@ -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
{
}
}

@ -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
{
}
}

@ -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
{
}
}

@ -34,6 +34,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
@ -43,8 +44,25 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractFactory\AbstractFactory.cs" />
<Compile Include="AbstractFactory\ConcreteFactory1.cs" />
<Compile Include="AbstractFactory\ConcreteFactory2.cs" />
<Compile Include="AbstractFactory\ConcreteProduct1A.cs" />
<Compile Include="AbstractFactory\ConcreteProduct1B.cs" />
<Compile Include="AbstractFactory\ConcreteProduct2A.cs" />
<Compile Include="AbstractFactory\ConcreteProduct2B.cs" />
<Compile Include="AbstractFactory\ProductA.cs" />
<Compile Include="AbstractFactory\ProductB.cs" />
<Compile Include="FactoryMethod\DbLog.cs" />
<Compile Include="FactoryMethod\DbLogFactory.cs" />
<Compile Include="FactoryMethod\ILog.cs" />
<Compile Include="FactoryMethod\LogFactory.cs" />
<Compile Include="FactoryMethod\XmlLog.cs" />
<Compile Include="FactoryMethod\XmlLogFactory.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SimpleFactory\DbLogger.cs" />
<Compile Include="SimpleFactory\LoggerFactory.cs" />
<Compile Include="StaticFactoryMethod\ILogger.cs" />
<Compile Include="StaticFactoryMethod\Logger.cs" />
<Compile Include="StaticFactoryMethod\LoggerFactory.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)
{
//...
}
}
}

@ -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);
}
}
}

@ -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);
}
}

@ -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();
}
}
}
}

@ -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)
{
//...
}
}
}

@ -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);
}
}
}

@ -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();
}
}
}

@ -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; }
}
}

@ -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;
}
}
}
Loading…
Cancel
Save