diff --git a/DesignPattern/Decorator/App.config b/DesignPattern/Decorator/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/DesignPattern/Decorator/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/DesignPattern/Decorator/Circle.cs b/DesignPattern/Decorator/Circle.cs new file mode 100644 index 0000000..67443e3 --- /dev/null +++ b/DesignPattern/Decorator/Circle.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator +{ + class Circle : Shape + { + public Circle(Point origin, int radius) + { + X = origin.X; + Y = origin.Y; + Width = radius * 2; + Height = radius * 2; + } + + public override void Draw(Graphics g) + { + g.DrawEllipse(Pens.Black, X, Y, Width, Height); + } + } +} diff --git a/DesignPattern/Decorator/Client.cs b/DesignPattern/Decorator/Client.cs new file mode 100644 index 0000000..9f9a764 --- /dev/null +++ b/DesignPattern/Decorator/Client.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Decorator +{ + class Client + { + public static void HowToTest() + { + Form form = new Form(); + form.Show(); + + Graphics gr = form.CreateGraphics(); + + Shape shape = new Circle(new Point(100, 100), 50); + FillShapeDecorator deco = new FillShapeDecorator(shape); + deco.Draw(gr); + } + } +} diff --git a/DesignPattern/Decorator/Decorator.csproj b/DesignPattern/Decorator/Decorator.csproj new file mode 100644 index 0000000..260b607 --- /dev/null +++ b/DesignPattern/Decorator/Decorator.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {4BDF9142-7984-485F-AA62-77B7B7359966} + Exe + Decorator + Decorator + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DesignPattern/Decorator/FillShapeDecorator.cs b/DesignPattern/Decorator/FillShapeDecorator.cs new file mode 100644 index 0000000..ccda239 --- /dev/null +++ b/DesignPattern/Decorator/FillShapeDecorator.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator +{ + class FillShapeDecorator : ShapeDecorator + { + public FillShapeDecorator(Shape shape) : base(shape) + { + + } + + public override void Draw(Graphics g) + { + component.Draw(g); + + g.FillEllipse(Brushes.Green, component.X, component.Y, component.Width, component.Height); + } + } +} diff --git a/DesignPattern/Decorator/Program.cs b/DesignPattern/Decorator/Program.cs new file mode 100644 index 0000000..4d9a108 --- /dev/null +++ b/DesignPattern/Decorator/Program.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator +{ + /* + * 카테고리: 구조 패턴 + * 개요: 기존 객체의 구조를 그대로 둔 채, 부가적인 기능을 동적으로 추가함 + * 일반적으로 상속등으로 서브클래스를 만들어 사용하는 것의 대안적인 방법으로, + * 너무 많은 서브클래스들이 만들어지는 경우 유용하게 사용될 수 있음 + * 서브클래스는 컴파일 타임에 결정되지만, 데코레이터는 런타임에서 동적으로 추가확장 됨 + */ + class Program + { + static void Main(string[] args) + { + Client.HowToTest(); + + Console.ReadKey(); + } + } +} diff --git a/DesignPattern/Decorator/Properties/AssemblyInfo.cs b/DesignPattern/Decorator/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..483191e --- /dev/null +++ b/DesignPattern/Decorator/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("Decorator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Decorator")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("4bdf9142-7984-485f-aa62-77b7b7359966")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/DesignPattern/Decorator/Shape.cs b/DesignPattern/Decorator/Shape.cs new file mode 100644 index 0000000..c9f0d1f --- /dev/null +++ b/DesignPattern/Decorator/Shape.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator +{ + public abstract class Shape + { + public abstract void Draw(Graphics g); + + public int X { get; set; } + public int Y { get; set; } + public int Width { get; set; } + public int Height { get; set; } + } +} diff --git a/DesignPattern/Decorator/ShapeDecorator.cs b/DesignPattern/Decorator/ShapeDecorator.cs new file mode 100644 index 0000000..0b4008a --- /dev/null +++ b/DesignPattern/Decorator/ShapeDecorator.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Decorator +{ + public abstract class ShapeDecorator : Shape + { + protected Shape component; + + public ShapeDecorator(Shape shape) + { + component = shape; + } + } +} diff --git a/DesignPattern/DesignPattern.sln b/DesignPattern/DesignPattern.sln index 11fbf5f..9658118 100644 --- a/DesignPattern/DesignPattern.sln +++ b/DesignPattern/DesignPattern.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge", "Bridge\Bridge.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Composite\Composite.csproj", "{52E6E6B5-8A12-4A92-8A38-8697247F4CA2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decorator", "Decorator\Decorator.csproj", "{4BDF9142-7984-485F-AA62-77B7B7359966}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -57,6 +59,10 @@ Global {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Debug|Any CPU.Build.0 = Debug|Any CPU {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Release|Any CPU.ActiveCfg = Release|Any CPU {52E6E6B5-8A12-4A92-8A38-8697247F4CA2}.Release|Any CPU.Build.0 = Release|Any CPU + {4BDF9142-7984-485F-AA62-77B7B7359966}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4BDF9142-7984-485F-AA62-77B7B7359966}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4BDF9142-7984-485F-AA62-77B7B7359966}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4BDF9142-7984-485F-AA62-77B7B7359966}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE