473,715 Members | 5,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing messages between classes

bartonc
6,596 Recognized Expert Expert
I'm working on a scheme which reduces the interface between classed to a single method and a list messages that the co-operative class knows. I invite all comments and/or critiques/improvements: We could even turn this into an article!
Expand|Select|Wrap|Line Numbers
  1. class AnyClass(object):
  2.     """Demonstrate massage passing:
  3.         This is the subordinate class."""
  4.     def Funct1(self, arg1):
  5.         print "AnyClass.Funct1 got", arg1
  6.         self.Send("MESSAGE3", arg1, string1='hello', string2='world')
  7.  
  8.     def Funct2(self, arg2, arg3):
  9.         print arg2, arg3
  10.  
  11.     def Funct3(self, count, **kwargs):
  12.         """Must document the names of the kwargs here:
  13.            Funct3(count, string1, string2)"""
  14.         print count, kwargs['string1'], kwargs['string2']
  15.  
  16.  
  17.     # Of cource, the sender and the reciver would typically have different massages
  18.     DispatchDict = {'MESSAGE1':Funct1, 'MESSAGE2':Funct2, 'MESSAGE3':Funct3}
  19.  
  20.     def Receive(self, *args, **kwargs):
  21.         self.DispatchDict[args[0]](self, *args[1:], **kwargs)
  22.  
  23.     def Connect(self, callback):
  24.         self.Send = callback
  25.         return self.Receive
  26.  
  27.  
  28. class AnyOtherClass(object):
  29.     """Demonstrate massage passing:
  30.         This is the superior class."""
  31.     def __init__(self):
  32.         self.Worker = AnyClass()
  33.         self.Send = self.Worker.Connect(self.Receive)
  34.         self.Send('MESSAGE1', 5)
  35.  
  36.     def Funct1(self, arg1):
  37.         print arg1
  38.  
  39.     def Funct2(self, arg2, arg3):
  40.         print arg2, arg3
  41.  
  42.     def Funct3(self, count, **kwargs):
  43.         """Must document the names of the kwargs here:
  44.            Funct3(count, string1, string2)"""
  45.         print count, kwargs['string1'], kwargs['string2']
  46.  
  47.  
  48.     # Of cource, the sender and the reciver would typically have different massages
  49.     DispatchDict = {'MESSAGE1':Funct1, 'MESSAGE2':Funct2, 'MESSAGE3':Funct3}
  50.  
  51.     def Receive(self, *args, **kwargs):
  52.         self.DispatchDict[args[0]](self, *args[1:], **kwargs)
  53.  
  54. test = AnyOtherClass()
  55.  
Nov 3 '07 #1
0 1440

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1578
by: cold | last post by:
I'm trying to write an helper class that allows me to serialize objects to a socket (or a NetworkStream). I know there are plenty of .NET classes that help me to serialize object to an array of byte, but I can't undestand how I can send a block of bytes and being sure the receiver will receive only this block....I mean, if I write the bytes to a NetworkStream the receiver will get a stream of bytes and he won't have any way to know how to...
2
1874
by: Gvs | last post by:
Hi, I'm currently trying to pass messages into a queue. This all works fine, however, i'm trying to my program to stop sending messages to the queue when it reaches an upper threshold. At present this threshold is the number of chairs in a waiting room. This is an int represented by nChairs. So i want to compare this to the number of messages in my queue. I'm using the following lines of code to do this, however the queue is always...
3
4143
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API calls are needed! // using System; using System.Runtime.InteropServices;
5
2009
by: Adam | last post by:
Hi, How do i listen for windows messages in c# on compact.net? I have a window containing an instantiation of the HTML viewer control, which is a child of the main form. As the compact implementation of the form class does not have a wndproc function to override, i cannot get the messages. I cannot use a message window, as the window i am listening to will then not display. Is there another method of doing this?
11
3181
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to the Data Access Layer method -or- 2) Pass some kind of a unique id to the Data Access Layer method
3
8002
by: Curious | last post by:
Hi, I need some technique to communicate by passing messages with different threads. For my problem shared memory is not applicable. Can someone give me some help of what there is available in the .net. Thanks in Advance
10
3471
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
12
2681
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
0
875
by: AAaron123 | last post by:
On a Form I have a RichTextBox. I do the call for EM_SETEVENTMASK with ENM_REQUESTRESIZE The user control's WndProc writes to the consol to keep track of any EN_REQUESTRESIZE messages. As I type into the RichTextBox I see the results of the console writes in the Output window.
0
8823
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9343
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9198
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.