473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Conceptual - Communication between Objects

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

}

}

}


May 18 '07 #1
7 1787


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" <wa*************@yahoo.comwrote in message
news:u9**************@TK2MSFTNGP05.phx.gbl...
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);

}

}

}


May 18 '07 #2
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" <wa*************@yahoo.comwrote in message
news:u9**************@TK2MSFTNGP05.phx.gbl...
>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);

}

}

}


May 18 '07 #3
shh
On May 18, 10:31 am, "sloan" <s...@ipass.netwrote:
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.

"RSH" <way_beyond_o...@yahoo.comwrote in message

news:u9**************@TK2MSFTNGP05.phx.gbl...
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);
}
}
}- Hide quoted text -

- Show quoted text -
Here's a nice mini discussion on it as well.

http://www.codeproject.com/gen/desig...25#xx1282025xx
May 30 '07 #4
On May 18, 3:31 pm, "sloan" <s...@ipass.netwrote:

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

May 30 '07 #5
On May 30, 5:09 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 18, 3:31 pm, "sloan" <s...@ipass.netwrote:

<snip>
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
Hi,

Why is re-inventing the wheel more elegant?

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

Moty

May 30 '07 #6
On May 30, 3:17 pm, Moty Michaely <Moty...@gmail.comwrote:
On May 30, 5:09 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Out of interest, what do AddHandler/RemoveHandler not do that you want
them to? I'm sure I'm missing something...

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

May 30 '07 #7
On May 30, 5:27 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 30, 3:17 pm, Moty Michaely <Moty...@gmail.comwrote:
On May 30, 5:09 pm, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Out of interest, what do AddHandler/RemoveHandler not do that you want
them to? I'm sure I'm missing something...
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
Hi Jon,

The reply wasn't meant for you :)

I just firmed your words. :)

Moty

May 30 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ricardo Jasinski | last post by:
Hello people!! I'm trying to improve an application I recently developed, in order to release its version 2.0. One of my concerns is to make it more robust, stable and with error-recovering...
1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
1
by: MatthewRoberts | last post by:
Howdy All, I am having difficulty with two-way communication across AppDomains in an attempt to dynamically script applications. Everything works as expected, except when using ByRef parameters....
4
by: Daniel Ladd | last post by:
Hi, I have a problem with a conceptual graph in c++. I have a oist of structures like this: typedef struct Conceptual { char* Name;//Rappresenta la parola da mettere nel grafo Conceptual* Next;...
8
by: M_Mann | last post by:
Hello, Pls excuse if you consider this off-topic. Conceptual artists seek programmers here. We are authors of "Exhibition of Living Managers" (MANAGEX, www.managex.info) which is is global...
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
12
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
3
by: Dmitriy V'jukov | last post by:
Latest C++0x draft N2723, 2008-08-25: "1.9/7 When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects which are neither — of type volatile...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.