R
RSH
I am working through some design patterns, and one recurring theme for me is
the need to be able to communicate between objects while promoting
encapsulation and loose coupling between them.
Based on a couple design patterns I have built a sample app that seems to be
a pretty decent model (I think).
Is this solution a pretty decent implementation of a process where an x
number of objects need to be notified when an event is triggered by an
object of either the same or different type? I was thinking about using
Delegates and Events but based on this model I don't know if that is
necessary.
Any thoughts or advice would be greatly appriciated.
Thanks,
Ron
using System;
using System.Collections;
using System.Text;
namespace ObjectCommunication
{
class Program
{
static void Main(string[] args)
{
Builder b = new Builder();
}
}
class Builder
{
public Builder()
{
CommunicatorObject c1 = new CommunicatorObject("Communicator1");
CommunicatorObject c2 = new CommunicatorObject("Communicator2");
CommunicatorObject c3 = new CommunicatorObject("Communicator3");
ConcreteObject1 o1 = new ConcreteObject1("Object1");
ConcreteObject1 o2 = new ConcreteObject1("Object2");
ConcreteObject2 o3 = new ConcreteObject2("Object3");
ConcreteObject2 o4 = new ConcreteObject2("Object4");
ConcreteObject3 o5 = new ConcreteObject3("Object5");
ConcreteObject3 o6 = new ConcreteObject3("Object6");
Alertor.Add(o1);
Alertor.Add(o2);
Alertor.Add(o3);
Alertor.Add(o4);
Alertor.Add(o5);
Alertor.Add(o6);
Alertor.Add(c2);
c1.RaiseEvent();
Console.Read();
}
}
// Interface to ensure that all communication based objects implement the
appropriate field/method
public interface ICommunication
{
string Name { get;}
void Output(ICommunication objRaisedBy);
}
// Abstract class implementing base methods
public abstract class ObjectBase : ICommunication
{
protected string m_name;
public virtual string Name
{
get
{
return m_name;
}
}
public virtual void RaiseEvent()
{
Alertor.NotifyObjects(this);
}
public virtual void Output(ICommunication objRaisedBy)
{
Console.WriteLine(this.Name + " Received Event From Object" +
objRaisedBy.Name);
}
}
// Concrete classes
class ConcreteObject1 : ObjectBase
{
public ConcreteObject1(string Name)
{
m_name = Name;
}
}
class ConcreteObject2 : ObjectBase
{
public ConcreteObject2(string Name)
{
m_name = Name;
}
}
class ConcreteObject3 : ObjectBase
{
public ConcreteObject3(string Name)
{
m_name = Name;
}
}
// Alertor Class stores an Arraylist of objects to notify when an event
occurs
public abstract class Alertor
{
private static ArrayList m_list = new ArrayList();
public static void Add(ICommunication obj)
{
m_list.Add(obj);
}
public static void NotifyObjects(ICommunication objRaisedEvent)
{
foreach (ICommunication obj in m_list)
{
if (objRaisedEvent.Name != obj.Name)
{
obj.Output(objRaisedEvent);
}
}
}
}
// Communicator class - implements ICommunication to illustrate
communication between different types of objects
public class CommunicatorObject : ICommunication
{
private string m_name;
public CommunicatorObject(string Name)
{
m_name = Name;
}
public string Name
{
get
{
return m_name;
}
}
public void RaiseEvent()
{
Alertor.NotifyObjects(this);
}
public void Output(ICommunication objRaisedBy)
{
Console.WriteLine(this.Name + " Received Event From Object" +
objRaisedBy.Name);
}
}
}
the need to be able to communicate between objects while promoting
encapsulation and loose coupling between them.
Based on a couple design patterns I have built a sample app that seems to be
a pretty decent model (I think).
Is this solution a pretty decent implementation of a process where an x
number of objects need to be notified when an event is triggered by an
object of either the same or different type? I was thinking about using
Delegates and Events but based on this model I don't know if that is
necessary.
Any thoughts or advice would be greatly appriciated.
Thanks,
Ron
using System;
using System.Collections;
using System.Text;
namespace ObjectCommunication
{
class Program
{
static void Main(string[] args)
{
Builder b = new Builder();
}
}
class Builder
{
public Builder()
{
CommunicatorObject c1 = new CommunicatorObject("Communicator1");
CommunicatorObject c2 = new CommunicatorObject("Communicator2");
CommunicatorObject c3 = new CommunicatorObject("Communicator3");
ConcreteObject1 o1 = new ConcreteObject1("Object1");
ConcreteObject1 o2 = new ConcreteObject1("Object2");
ConcreteObject2 o3 = new ConcreteObject2("Object3");
ConcreteObject2 o4 = new ConcreteObject2("Object4");
ConcreteObject3 o5 = new ConcreteObject3("Object5");
ConcreteObject3 o6 = new ConcreteObject3("Object6");
Alertor.Add(o1);
Alertor.Add(o2);
Alertor.Add(o3);
Alertor.Add(o4);
Alertor.Add(o5);
Alertor.Add(o6);
Alertor.Add(c2);
c1.RaiseEvent();
Console.Read();
}
}
// Interface to ensure that all communication based objects implement the
appropriate field/method
public interface ICommunication
{
string Name { get;}
void Output(ICommunication objRaisedBy);
}
// Abstract class implementing base methods
public abstract class ObjectBase : ICommunication
{
protected string m_name;
public virtual string Name
{
get
{
return m_name;
}
}
public virtual void RaiseEvent()
{
Alertor.NotifyObjects(this);
}
public virtual void Output(ICommunication objRaisedBy)
{
Console.WriteLine(this.Name + " Received Event From Object" +
objRaisedBy.Name);
}
}
// Concrete classes
class ConcreteObject1 : ObjectBase
{
public ConcreteObject1(string Name)
{
m_name = Name;
}
}
class ConcreteObject2 : ObjectBase
{
public ConcreteObject2(string Name)
{
m_name = Name;
}
}
class ConcreteObject3 : ObjectBase
{
public ConcreteObject3(string Name)
{
m_name = Name;
}
}
// Alertor Class stores an Arraylist of objects to notify when an event
occurs
public abstract class Alertor
{
private static ArrayList m_list = new ArrayList();
public static void Add(ICommunication obj)
{
m_list.Add(obj);
}
public static void NotifyObjects(ICommunication objRaisedEvent)
{
foreach (ICommunication obj in m_list)
{
if (objRaisedEvent.Name != obj.Name)
{
obj.Output(objRaisedEvent);
}
}
}
}
// Communicator class - implements ICommunication to illustrate
communication between different types of objects
public class CommunicatorObject : ICommunication
{
private string m_name;
public CommunicatorObject(string Name)
{
m_name = Name;
}
public string Name
{
get
{
return m_name;
}
}
public void RaiseEvent()
{
Alertor.NotifyObjects(this);
}
public void Output(ICommunication objRaisedBy)
{
Console.WriteLine(this.Name + " Received Event From Object" +
objRaisedBy.Name);
}
}
}