본문 바로가기
C# 디자인패턴

08# Adapter 패턴 Structural Patterns

by NaHyungMin 2019. 5. 29.

 

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

 

Adapter .NET Design Pattern in C# and VB - dofactory.com

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.     The classes and objects participating in this pattern are: This structural code demon

www.dofactory.com