static void Main(string[] args) { //BuilderExample(); //FactoryExample(); //FactoryMethodExample(); //FactoryMethodExample2(); //ProtoTypeExample(); //SingletonExample();
AdapterExample();
//Console.WriteLine("Hello World!"); Console.ReadKey(); }
static void AdapterExample() { Compound unknown = new Compound(Compound.CompoundTypes.NONE);
Compound water = new RichCompound(Compound.CompoundTypes.WATER); water.Display();
Compound benzene = new RichCompound(Compound.CompoundTypes.BENZENE); benzene.Display();
Compound ethanol = new RichCompound(Compound.CompoundTypes.ETHANOL); ethanol.Display(); }
main.cs |
using System; using System.Collections.Generic; using System.Text;
namespace DesignPatterns.Structural_Patterns.Adapter { public class Compound { public enum CompoundTypes { NONE = 0, WATER = 1, BENZENE = 2, ETHANOL = 3, }
public enum PointTypes { NONE = 0, M = 1, B = 2, }
protected CompoundTypes chemical; protected float boilingPoint; protected float meltingPoint; protected double molecularWeight; protected string molecularFormula;
public Compound(CompoundTypes chemical) { this.chemical = chemical; }
public virtual void Display() { Console.WriteLine("{0} : {1} -----", this.GetType().Name, this.chemical); } } }
Compound.cs |
using System;
namespace DesignPatterns.Structural_Patterns.Adapter { public class ChemicalDatabank { public float GetCriticalPoint(Compound.PointTypes pointType, Compound.CompoundTypes compoundType) { switch (pointType) { case Compound.PointTypes.M: return GetCriticalPointM(compoundType); case Compound.PointTypes.B: return GetCriticalPointB(compoundType); case Compound.PointTypes.NONE: default: Console.WriteLine("not support point type"); return 0.0f; } }
public string GetMolecularStructure(Compound.CompoundTypes compoundType) { switch (compoundType) { case Compound.CompoundTypes.WATER: return "H20"; case Compound.CompoundTypes.BENZENE: return "C6H6"; ; case Compound.CompoundTypes.ETHANOL: return "C2H5OH"; case Compound.CompoundTypes.NONE: default: return ""; } }
public double GetMolecularWeight(Compound.CompoundTypes compoundType) { switch (compoundType) { case Compound.CompoundTypes.WATER: return 18.015; case Compound.CompoundTypes.BENZENE: return 78.1134; case Compound.CompoundTypes.ETHANOL: return 46.0688; case Compound.CompoundTypes.NONE: default: return 0d; } }
private float GetCriticalPointM(Compound.CompoundTypes compoundType) { float value = 0f;
switch (compoundType) { case Compound.CompoundTypes.WATER: value = 0.0f; break; case Compound.CompoundTypes.BENZENE: value = 5.5f; break; case Compound.CompoundTypes.ETHANOL: value = -114.1f; break; case Compound.CompoundTypes.NONE: default: value = 0f; break; }
return value; }
private float GetCriticalPointB(Compound.CompoundTypes compoundType) { float value = 0f;
switch (compoundType) { case Compound.CompoundTypes.WATER: value = 100.0f; break; case Compound.CompoundTypes.BENZENE: value = 80.1f; break; case Compound.CompoundTypes.ETHANOL: value = 78.3f; break; case Compound.CompoundTypes.NONE: default: value = 0f; break; }
return value; } } }
ChemicalDatabank.cs |
using System;
namespace DesignPatterns.Structural_Patterns.Adapter { public class RichCompound : Compound { private ChemicalDatabank bank;
public RichCompound(Compound.CompoundTypes compoundType) : base(compoundType) { }
public override void Display() { bank = new ChemicalDatabank();
boilingPoint = bank.GetCriticalPoint(PointTypes.B, this.chemical); meltingPoint = bank.GetCriticalPoint(PointTypes.M, this.chemical); molecularWeight = bank.GetMolecularWeight(this.chemical); molecularFormula = bank.GetMolecularStructure(this.chemical);
base.Display(); Console.WriteLine(" Formula : {0}", molecularFormula); Console.WriteLine(" Weight : {0}", molecularWeight); Console.WriteLine(" Melting Pt : {0}", meltingPoint); Console.WriteLine(" Boling Pt : {0}", boilingPoint); } } }
RichCompound.cs |
참고 사이트
https://www.dofactory.com/net/adapter-design-pattern
'C# 디자인패턴' 카테고리의 다른 글
10# Composite패턴 Structural Patterns (0) | 2019.06.03 |
---|---|
09# Bridge패턴 Structural Patterns (0) | 2019.05.29 |
07# Singleton 패턴(thread safe) Creational Patterns (0) | 2019.05.24 |
06# Prototype 패턴 Creational Patterns (0) | 2019.05.23 |
05# Factory Method 패턴2 Creational Patterns (0) | 2019.05.23 |