composite,pattern

Erhan 8/7/2016 0

Composite Pattern

C#
It provides a better
approach in addressing communication with individual and group of objects. As
usual we can start with the Challenge  Solution approach.
Challenge
You are working on a distributed application containing Head Office and Branch
Offices. Each office will be having 2 Clock Controls which needed to be updated
globally during Daylight Saving times.

The above diagram shows the current structure. There will be only 1 Head Office
and multiple branches under it. The head office and each branch will be having 2
clock controls. The time changing process starts from the head office and in the
current approach, the head office has to remember all branches and all clocks inside
each branch. This is very tedious and will break when there is consolidation of
branches under one branch group.
How to provide a better solution?
Definition
GoF Definition: "Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and compositions of
objects uniformly"
Implementation
We can use the Composite Design Pattern in the above scenario. Once our solution
is implemented the Head Office needs to think about only the Branches. The
Branches will think about their respective Clocks.
Classes.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Clock; namespace CompositePattern { interfaceIComponent { void Add(IComponent notifier); void SetTime(DateTime time); } classOffice : IComponent { privateIList< IComponent> _list = new List<IComponent >(); publicvoid Add(IComponent notifier) { _list.Add(notifier); } publicvoid SetTime(DateTime time) { foreach (IComponent n in _list) n.SetTime(time); } } classClock : IComponent { publicClockControl ClockControl; publicvoid Add(IComponent notifier) { thrownewApplicationException( "You cannot add IClock!"); } publicvoid SetTime(DateTime time) { ClockControl.CurrentTime = time; } } } MainForm.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace CompositePattern { publicpartialclassMainForm : Form { public MainForm() { InitializeComponent(); CreateForms(); } privateOffice _headOffice; privatevoid CreateForms() { // Create Forms ClockForm hForm = new ClockForm(); hForm.Show(); ClockForm b1Form = new ClockForm(); b1Form.clock1.ClockBackColor = Color.Red; b1Form.clock2.ClockBackColor = Color.LightGreen; b1Form.Show(); ClockForm b2Form = new ClockForm(); b2Form.clock1.ClockBackColor = Color.Red; b2Form.clock2.ClockBackColor = Color.LightGreen; b2Form.Show(); // Create Composite Classes and assign Clock Control instances _headOffice = newOffice(); _headOffice.Add( newClock() { ClockControl = hForm.clock1 }); _headOffice.Add( newClock() { ClockControl = hForm.clock2 }); Office branch1 = new Office(); branch1.Add( newClock() { ClockControl = b1Form.clock1 }); branch1.Add( newClock() { ClockControl = b1Form.clock2 }); _headOffice.Add(branch1); Office branch2 = new Office(); branch2.Add( newClock() { ClockControl = b2Form.clock1 }); branch2.Add( newClock() { ClockControl = b2Form.clock2 }); _headOffice.Add(branch2); } privatevoid SetTimeButton_Click(object sender, EventArgs e) { _headOffice.SetTime( newDateTime( DateTime.Today.Year, DateTime .Today.Month, DateTime.Today.Day, 10, 0, 0)); } } }

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments