Skip to content

Commit 6710e20

Browse files
committed
add composite pattern
1 parent f440d5c commit 6710e20

12 files changed

+217
-28
lines changed

DesignPatterns.sln

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adapter", "Structural\Adapt
4343
EndProject
4444
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge", "Structural\Bridge\Bridge.csproj", "{FECD9206-68C5-4F31-A7AB-AD876E05ABDA}"
4545
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Structural\Composite\Composite.csproj", "{F8ED6676-6701-4476-9606-4D150EF4A537}"
47+
EndProject
4648
Global
4749
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4850
Debug|Any CPU = Debug|Any CPU
@@ -161,6 +163,12 @@ Global
161163
{FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Release|Any CPU.ActiveCfg = Release|x86
162164
{FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Release|x86.ActiveCfg = Release|x86
163165
{FECD9206-68C5-4F31-A7AB-AD876E05ABDA}.Release|x86.Build.0 = Release|x86
166+
{F8ED6676-6701-4476-9606-4D150EF4A537}.Debug|Any CPU.ActiveCfg = Debug|x86
167+
{F8ED6676-6701-4476-9606-4D150EF4A537}.Debug|x86.ActiveCfg = Debug|x86
168+
{F8ED6676-6701-4476-9606-4D150EF4A537}.Debug|x86.Build.0 = Debug|x86
169+
{F8ED6676-6701-4476-9606-4D150EF4A537}.Release|Any CPU.ActiveCfg = Release|x86
170+
{F8ED6676-6701-4476-9606-4D150EF4A537}.Release|x86.ActiveCfg = Release|x86
171+
{F8ED6676-6701-4476-9606-4D150EF4A537}.Release|x86.Build.0 = Release|x86
164172
EndGlobalSection
165173
GlobalSection(SolutionProperties) = preSolution
166174
HideSolutionNode = FALSE
@@ -184,5 +192,6 @@ Global
184192
{14333E87-2DCC-42A7-91FF-2DFEABF2AA95} = {B9EDC98E-1E00-4320-A39F-7D64F44A2D3C}
185193
{72F9E83E-462A-4A2F-88BB-1A8A1AE33AE0} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
186194
{FECD9206-68C5-4F31-A7AB-AD876E05ABDA} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
195+
{F8ED6676-6701-4476-9606-4D150EF4A537} = {05993A61-BAEE-4BA2-81F0-25B789A15F6E}
187196
EndGlobalSection
188197
EndGlobal

Structural/Adapter/Adaptee.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Adapter {
4+
5+
class Adaptee {
6+
7+
public void PerformRequest() {
8+
Console.WriteLine("Addaptee perform request.");
9+
}
10+
11+
}
12+
}

Structural/Adapter/Adapter.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Adapter {
2+
3+
class Adapter : ITarget {
4+
5+
private readonly Adaptee _adaptee;
6+
7+
public Adapter(Adaptee adaptee) {
8+
this._adaptee = adaptee;
9+
}
10+
11+
void ITarget.Request() {
12+
this._adaptee.PerformRequest();
13+
}
14+
}
15+
}

Structural/Adapter/Adapter.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<Reference Include="System.Xml" />
4444
</ItemGroup>
4545
<ItemGroup>
46+
<Compile Include="Adaptee.cs" />
47+
<Compile Include="Adapter.cs" />
48+
<Compile Include="ITarget.cs" />
4649
<Compile Include="Program.cs" />
4750
<Compile Include="Properties\AssemblyInfo.cs" />
4851
</ItemGroup>

Structural/Adapter/ITarget.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Adapter {
2+
3+
interface ITarget {
4+
5+
void Request();
6+
7+
}
8+
}

Structural/Adapter/Program.cs

-28
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,6 @@
44
using System.Text;
55

66
namespace Adapter {
7-
8-
interface ITarget {
9-
10-
void Request();
11-
12-
}
13-
14-
class Adapter : ITarget {
15-
16-
private readonly Adaptee _adaptee;
17-
18-
public Adapter(Adaptee adaptee) {
19-
this._adaptee = adaptee;
20-
}
21-
22-
void ITarget.Request() {
23-
this._adaptee.PerformRequest();
24-
}
25-
}
26-
27-
class Adaptee {
28-
29-
public void PerformRequest() {
30-
Console.WriteLine("Addaptee perform request.");
31-
}
32-
33-
}
34-
357
class Program {
368

379
static void Main(string[] args) {

Structural/Composite/Composite.csproj

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{F8ED6676-6701-4476-9606-4D150EF4A537}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Composite</RootNamespace>
12+
<AssemblyName>Composite</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="CompositeGraphic.cs" />
47+
<Compile Include="Ellipse.cs" />
48+
<Compile Include="IGraphic.cs" />
49+
<Compile Include="Program.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
3+
namespace Composite {
4+
5+
class CompositeGraphic : IGraphic {
6+
7+
private readonly IList<IGraphic> _childGraphics = new List<IGraphic>();
8+
9+
public void Print() {
10+
foreach (var childGraphic in this._childGraphics) {
11+
childGraphic.Print();
12+
}
13+
}
14+
15+
public void Add(IGraphic graphic) {
16+
this._childGraphics.Add(graphic);
17+
}
18+
19+
public void Remove(IGraphic graphic) {
20+
this._childGraphics.Remove(graphic);
21+
}
22+
23+
}
24+
}

Structural/Composite/Ellipse.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Composite {
4+
5+
class Ellipse : IGraphic {
6+
7+
public void Print() {
8+
Console.WriteLine("Ellipse");
9+
}
10+
}
11+
}

Structural/Composite/IGraphic.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Composite {
2+
3+
interface IGraphic {
4+
5+
void Print();
6+
7+
}
8+
}

Structural/Composite/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
3+
namespace Composite {
4+
5+
class Program {
6+
7+
static void Main(string[] args) {
8+
var ellipse1 = new Ellipse();
9+
var ellipse2 = new Ellipse();
10+
var ellipse3 = new Ellipse();
11+
var ellipse4 = new Ellipse();
12+
13+
var graphic = new CompositeGraphic();
14+
var graphic1 = new CompositeGraphic();
15+
var graphic2 = new CompositeGraphic();
16+
17+
graphic1.Add(ellipse1);
18+
graphic1.Add(ellipse2);
19+
graphic1.Add(ellipse3);
20+
21+
graphic2.Add(ellipse4);
22+
23+
graphic.Add(graphic1);
24+
graphic.Add(graphic2);
25+
26+
graphic.Print();
27+
28+
Console.ReadKey();
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Composite")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Composite")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("d78ae913-063c-4985-8610-b9520f267348")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)