Article Preview
Buy Now
| Print: | |
| PDF: |
Object-Oriented Thinking
An EasyTCPMessage Class
Events In Action
Issue: 5.3 (March/April 2007)
Author: Charles Yeomans
Article Description: No description available.
Article Length (in bytes): 5,503
Starting Page Number: 39
RBD Number: 5314
Resource File(s): None
Related Web Link(s):
http://www.declareSub.com/
Known Limitations: None
Excerpt of article text...
Having spent a lot of time working with EasyTCPSockets, I have found it useful to write an EasyTCPMessage class. This provides some examples of the use of events.
Recall that an EasyTCPSocket message consists of a commandID and message data. So we will write an abstract EasyTCPMessage class and subclasses corresponding to the different messages.
There is no way around the need to write some sort of code that takes a commandID and creates an instance of the corresponding EasyTCPMessage subclass. While it is possible to dress this up in various patterns, so far I have found that a simple Select statement works, if written with an eye toward maintenance.
Let's begin by adding a property CommandID as Integer to EasyTCPMessage. Now the source of this property should be the EasyTCPMessage subclasses, so we presume that it will be set in each subclass. Now we refactor.
This quick implementation is unsatisfactory for several reasons. First, this property should be read-only by objects other than EasyTCPMessage objects. So we replace it with a public method CommandID() as Integer and a protected property pCommandID as Integer. In fact, there is no reason why this method should be settable in EasyTCPMessage code; only the subclass should be allowed to set it. This means that we cannot store the value in a property of EasyTCPMessage. Instead, we replace the protected property with an event GetCommandID() as Integer. Then EasyTCPMessage.CommandID simply calls the event handler and returns the value provided by the subclass.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|







