관리 메뉴

프로그램 소스

[설계 패턴 C#] 7. 가교 패턴 본문

설계 패턴(C#)

[설계 패턴 C#] 7. 가교 패턴

Only 프로그램 소스 언제나휴일 2016.06.09 22:48

7. 가교 패턴


Bridge.zip


"본문 내용"은 언제나 휴일 본 사이트에 있습니다.

 

IImageProcessing.cs

namespace Bridge

{

    interface IImageProcessing

    {

        string ImageProcessing(string subject);

    }

}

 

VRModule.cs

namespace Bridge

{

    class VRModule:IImageProcessing

    {

        public string  ImageProcessing(string subject) //약속한 기능 구현

        {

            return subject.Replace("떨림","");

        }

}

 

PCModule.cs

namespace Bridge

{

    class PCModule:IImageProcessing

    {

        public string ImageProcessing(string subject)  //약속한 기능 구현

        {

            return subject.Replace("0", "1");

        }

    }

}

 

Lens.cs

using System.Collections.Generic;

namespace Bridge

{

    abstract class Lens

    {

        const int min_focuslevel=1;

        const int max_focuslevel=3;

        public int FocusLevel

        {

            get;

            private set;

        }

        public int ModuleIndex

        {

            get;

            private set;

        }

        List<IImageProcessing> modules = new List<IImageProcessing>();

        //피사체를 얻어와 영성처리하여 사진으로 변환하는 메서드 약속

        public abstract string Take(string subject, int distance);

        public int ModuleCount

        {

            get

            {

                return modules.Count;

            }

        }

       

        public Lens()

        {

            FocusLevel = min_focuslevel;

            ModuleIndex = -1;

        }

 

        public void AddModule(IImageProcessing module)

        {

            modules.Add(module); // 실제 영상 처리를 하는 모듈 장착

        }

 

        //장착된 영상 처리 모듈을 이용하여 영상 처리를 하는 메서드

        protected string ImageProcessing(string subject,int index)

        {

            if((index>=0)||(index<ModuleCount))

            {

                IImageProcessing module = modules[index];

                subject = module.ImageProcessing(subject);

            }

            return subject;

        }

        public virtual void FocusUp()

        {

            if(FocusLevel < max_focuslevel)

            {

                FocusLevel++;

            }

        }

        public virtual void FocusDown()

        {

            if(FocusLevel > min_focuslevel)

            {

                FocusLevel--;

            }

        }

 

        protected string MakePicture(string subject,int distance)

        {

            string s = distance.ToString();

            return subject.Replace(s,"선명");

        }

        //영상 처리에 사용할 모듈을 선택하는 메서드

        public void SetModuleIndex(int index)

        {

            if((index >=0 )&&(index<ModuleCount))

            {

                ModuleIndex = index;

            }

        }

    }

}

 

AFLens.cs

namespace Bridge

{

    class AFLens:Lens

    {

        public override string Take(string subject, int distance)

        {

            AutoFocusing(distance);

            subject = Processing(subject);

            return MakePicture(subject,distance);

        }

 

        void AutoFocusing(int distance)

        {

            //먼 거리에 있을 때 줌 레벨을 높인다.

            while (distance > FocusLevel)

            {

                FocusUp();

            }

 

            //가까운 거리에 있을 때 줌 레벨을 낮춘다.

            while(distance<FocusLevel)

            {

                FocusDown();

            }

        }

 

 

        string Processing(string subject)

        {

            int mcount = ModuleCount;

 

            //내부에 장착된 모듈들을 이용하여 영상 처리

            for(int i =0; i<mcount; i++)

            {

                subject = ImageProcessing(subject,i);

            }

            return subject;

        }

    }

}

 

MFLens.cs

using System;

namespace Bridge

{

    class MFLens:Lens

    {

        public override string Take(string subject, int distance)

        {

            int mindex = ModuleIndex;

            int mcount = ModuleCount;

            if((mindex>=0)&&(mindex<mcount))

            {

                subject = ImageProcessing(subject,mindex);

            }

            return MakePicture(subject, FocusLevel);

        }

        public override void FocusUp()

        {

            base.FocusUp();

            Console.WriteLine("현재 FocusLevel:{0}",FocusLevel);

        }

        public override void FocusDown()

        {

            base.FocusDown();

            Console.WriteLine("현재 FocusLevel:{0}",FocusLevel);

        }

    }

}

 

Camera.cs

using System;

namespace Bridge

{

    class Camera

    {

        Lens lens = null;

        public int ModuleCount

        {

            get

            {

                if( lens!=null )

                {

                    return lens.ModuleCount;

                }

                return -1;

            }

        }

 

        public void SetModuleIndex(int index)

        {

            if(lens!=null)

            {

                lens.SetModuleIndex(index);

            }

        }

 

        public void PutInLens(Lens lens)

        {

            this.lens = lens;

        }

        public void TakeAPicture(string subject,int distance)

        {

            Console.WriteLine("피사체:{0}",subject);

            string picture = "";

            if(lens!=null)

            {

               picture = lens.Take(subject,distance);

            }

            Console.WriteLine("사진:{0}",picture);

        }

        public void FocusUp()

        {

            if(lens!=null)

            {

                lens.FocusUp();

            }

        }

        public void FocusDown()

        {

            if(lens!=null)

            {

                lens.FocusDown();

            }

        }

    }

}

 

Program.cs

using System;

 

namespace Bridge

{

    class Program

    {

        static void Main(string[] args)

        { 

            Camera camera = new Camera();

            TestAFVRPCLens(camera);

            TestMFVRLens(camera);

        }

 

        static void TestAFVRPCLens(Camera camera)

        {

            Console.WriteLine("AFVRPCLens 테스트");

            VRModule vr = new VRModule();

            PCModule pc = new PCModule();

            Lens lens = new AFLens();

            lens.AddModule(vr);

            lens.AddModule(pc);

            camera.PutInLens(lens);

            camera.TakeAPicture("잎사귀떨림(0) 사람떨림(1) 건물(2) (3)", 1);           

        }

        static void TestMFVRLens(Camera camera)

        {

            Console.WriteLine("MFVRLens 테스트");

            IImageProcessing module = new VRModule();

            Lens lens = new MFLens();

            lens.AddModule(module);

            camera.PutInLens(lens);

            camera.TakeAPicture("잎사귀떨림(0) 사람떨림(1) 건물(2) (3)", 1);

            camera.FocusUp();

            camera.TakeAPicture("잎사귀떨림(0) 사람떨림(1) 건물(2) (3)", 1);

            camera.FocusDown();

            camera.SetModuleIndex(0);

            camera.TakeAPicture("잎사귀떨림(0) 사람떨림(1) 건물(2) (3)", 1);           

        }

    }

}

 

0 Comments
댓글쓰기 폼