Skip to content

Commit 842e7f5

Browse files
committed
Composite Pattern
1 parent 48297fd commit 842e7f5

10 files changed

Lines changed: 266 additions & 1 deletion

File tree

CompositePattern/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>

CompositePattern/Client.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace CompositePattern
2+
{
3+
public class Client
4+
{
5+
private readonly MenuComponent _menus;
6+
7+
public Client(MenuComponent menus)
8+
{
9+
_menus = menus;
10+
}
11+
12+
public void Print()
13+
{
14+
_menus.Print();
15+
}
16+
}
17+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{0D5AA731-03F0-4CF6-AEA3-254F06472911}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>CompositePattern</RootNamespace>
10+
<AssemblyName>CompositePattern</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Client.cs" />
46+
<Compile Include="Menu.cs" />
47+
<Compile Include="MenuComponent.cs" />
48+
<Compile Include="MenuItem.cs" />
49+
<Compile Include="Program.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<None Include="App.config" />
54+
</ItemGroup>
55+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
56+
</Project>

CompositePattern/Menu.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace CompositePattern
5+
{
6+
public class Menu : MenuComponent
7+
{
8+
List<MenuComponent> _components = new List<MenuComponent>();
9+
10+
public Menu(string name, string description)
11+
{
12+
Name = name;
13+
Description = description;
14+
15+
}
16+
17+
public override void Add(MenuComponent component)
18+
{
19+
_components.Add(component);
20+
}
21+
22+
public override void Remove(MenuComponent component)
23+
{
24+
_components.Remove(component);
25+
}
26+
27+
public override MenuComponent GetChild(int i)
28+
{
29+
return _components[i];
30+
}
31+
32+
public override string Name { get; }
33+
34+
public override string Description { get; }
35+
36+
public override void Print()
37+
{
38+
Console.WriteLine(Name);
39+
Console.WriteLine("___________");
40+
foreach (var menuComponent in _components)
41+
{
42+
menuComponent.Print();
43+
}
44+
Console.WriteLine();
45+
}
46+
}
47+
}

CompositePattern/MenuComponent.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace CompositePattern
4+
{
5+
public class MenuComponent
6+
{
7+
public virtual void Add(MenuComponent component)
8+
{
9+
throw new NotImplementedException();
10+
}
11+
12+
public virtual void Remove(MenuComponent component)
13+
{
14+
throw new NotImplementedException();
15+
}
16+
17+
public virtual MenuComponent GetChild(int i)
18+
{
19+
throw new NotImplementedException();
20+
}
21+
22+
public virtual string Name { get; }
23+
public virtual string Description { get; }
24+
public virtual bool Vegetarian { get; }
25+
public virtual double Price { get; }
26+
27+
public virtual void Print()
28+
{
29+
throw new NotImplementedException();
30+
}
31+
}
32+
}

CompositePattern/MenuItem.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace CompositePattern
4+
{
5+
public class MenuItem : MenuComponent
6+
{
7+
public MenuItem(string name, string description, double price, bool isveg)
8+
{
9+
Name = name;
10+
Description = description;
11+
Price = price;
12+
Vegetarian = isveg;
13+
}
14+
15+
public override string Name { get; }
16+
17+
public override string Description { get; }
18+
19+
public override double Price { get; }
20+
21+
public override bool Vegetarian { get; }
22+
23+
public override void Print()
24+
{
25+
Console.WriteLine($"{Name} : {Price} {(Vegetarian ? '+' : '*')} \n {Description}");
26+
}
27+
}
28+
}

CompositePattern/Program.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace CompositePattern
2+
{
3+
static class Program
4+
{
5+
public static void Main()
6+
{
7+
8+
var breakfast = new Menu("Breakfast", "Pancake House");
9+
var lunch = new Menu("Lunch", "Deli Diner");
10+
var dinner = new Menu("Dinner","Dinneroni");
11+
12+
var dessert = new Menu("Dessert", "Ice Cream");
13+
14+
var menu = new Menu("All", "McDonalds");
15+
16+
breakfast.Add(new MenuItem("Waffles","Butterscotch waffles",140,false));
17+
breakfast.Add(new MenuItem("Corn Flakes","Kellogs",80,true));
18+
19+
lunch.Add(new MenuItem("Burger", "Cheese and Onion Burger", 250, true));
20+
lunch.Add(new MenuItem("Sandwich", "Chicken Sandwich", 280, false));
21+
22+
dinner.Add(new MenuItem("Pizza", "Cheese and Tomato Pizza", 210, true));
23+
dinner.Add(new MenuItem("Pasta", "Chicken Pasta", 280, false));
24+
25+
dessert.Add(new MenuItem("Ice Cream", "Vanilla and Chocolate", 120, true));
26+
dessert.Add(new MenuItem("Cake", "Choclate Cake Slice",180, false));
27+
28+
dinner.Add(dessert);
29+
menu.Add(breakfast);
30+
menu.Add(lunch);
31+
menu.Add(dinner);
32+
33+
menu.Print();
34+
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
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("CompositePattern")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CompositePattern")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
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("0d5aa731-03f0-4cf6-aea3-254f06472911")]
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")]

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplatePattern", "Template
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{A3B8C12F-5DAC-4CEA-B039-C2CBD7A5C9AD}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompositePattern", "CompositePattern\CompositePattern.csproj", "{0D5AA731-03F0-4CF6-AEA3-254F06472911}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,10 @@ Global
6971
{A3B8C12F-5DAC-4CEA-B039-C2CBD7A5C9AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
7072
{A3B8C12F-5DAC-4CEA-B039-C2CBD7A5C9AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
7173
{A3B8C12F-5DAC-4CEA-B039-C2CBD7A5C9AD}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{0D5AA731-03F0-4CF6-AEA3-254F06472911}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{0D5AA731-03F0-4CF6-AEA3-254F06472911}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{0D5AA731-03F0-4CF6-AEA3-254F06472911}.Release|Any CPU.ActiveCfg = Release|Any CPU
77+
{0D5AA731-03F0-4CF6-AEA3-254F06472911}.Release|Any CPU.Build.0 = Release|Any CPU
7278
EndGlobalSection
7379
GlobalSection(SolutionProperties) = preSolution
7480
HideSolutionNode = FALSE

IteratorPattern/DinnerMenu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class DinnerMenu
77
{
88
private const int Max = 1;
99

10-
private int _count = 0;
10+
private int _count;
1111
private Menu[] _items;
1212

1313
public IEnumerable Items

0 commit comments

Comments
 (0)