Conceptual - Communication between Objects

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);

}

}

}
 
S

sloan

DotNet has a "built in" Observer pattern.

It is Delegates and Events. (which you bring up).

I wouldn't re-invent it.

The only caveat is that vb.net (for whatever reason) doesn't allow

add
remove

on event registration. You have to go to mydelete = Delegate.Combine(
..... )

Between namespaces ( a default one and no auto inclusion when I start a new
class) and this idiotic omission with the add/remove, are the 2 biggest
things I dislike about vb.net as opposed to C#.

(Wait, this is the C# group, so why I am pointing this vb.net stuff out?
Well, I guess for future googlers out there)

Outside of my rant, I will would stick with Events/Delegates instead of
having to code up the Observer pattern.





RSH said:
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);

}

}

}
 
R

RSH

Oh that is good stuff...I see what you mean.

I found a tutorial on it and implemented that...a bit more elegent for sure!

Thanks!
Ron


RSH said:
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);

}

}

}
 
S

shh

DotNet has a "built in"Observer pattern.

It is Delegates and Events. (which you bring up).

I wouldn't re-invent it.

The only caveat is that vb.net (for whatever reason) doesn't allow

add
remove

on event registration. You have to go to mydelete = Delegate.Combine(
.... )

Between namespaces ( a default one and no auto inclusion when I start a new
class) and this idiotic omission with the add/remove, are the 2 biggest
things I dislike about vb.net as opposed to C#.

(Wait, this is the C# group, so why I am pointing this vb.net stuff out?
Well, I guess for future googlers out there)

Outside of my rant, I will would stick with Events/Delegates instead of
having to code up theObserver pattern.




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.

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");











// 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



return m_name;


public virtual void RaiseEvent()



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)



public static void NotifyObjects(ICommunication objRaisedEvent)

foreach (ICommunication obj in m_list)

if (objRaisedEvent.Name != obj.Name)






// 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



return m_name;


public void RaiseEvent()



public void Output(ICommunication objRaisedBy)

Console.WriteLine(this.Name + " Received Event From Object" +
objRaisedBy.Name);


}- Hide quoted text -

- Show quoted text -

Here's a nice mini discussion on it as well.

http://www.codeproject.com/gen/desi...100&forumid=231554&select=1282025#xx1282025xx
 
J

Jon Skeet [C# MVP]

Between namespaces ( a default one and no auto inclusion when I start a
new class) and this idiotic omission with the add/remove, are the 2 biggest
things I dislike about vb.net as opposed to C#.

Out of interest, what do AddHandler/RemoveHandler not do that you want
them to? I'm sure I'm missing something...

Jon
 
M

Moty Michaely

Out of interest, what do AddHandler/RemoveHandler not do that you want
them to? I'm sure I'm missing something...

Jon

Hi,

Why is re-inventing the wheel more elegant?

Object Oriented design should solve code-reuse problems....

Moty
 
J

Jon Skeet [C# MVP]

Why is re-inventing the wheel more elegant?

Object Oriented design should solve code-reuse problems....

I wasn't suggested reinventing the wheel. I was suggesting that the
features sloan feels are missing in VB.NET are actually there, in the
form of AddHandler and RemoveHandler.

Jon
 
M

Moty Michaely

I wasn't suggested reinventing the wheel. I was suggesting that the
features sloan feels are missing in VB.NET are actually there, in the
form of AddHandler and RemoveHandler.

Jon

Hi Jon,

The reply wasn't meant for you :)

I just firmed your words. :)

Moty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top