473,803 Members | 4,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design of small related classes

I have some existing code that I am trying to upgrade to c++. The data in
question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits represent
the "category" of the data, and determine how the rest of the two bytes are
interpreted.

The existing code extracts various details from the data, but always uses a
switch statement on the category.

I could write a single class in c++, but I would have a switch statement in
every member function. This doesn't feel right somehow.
I could have a class for each category (inheriting from an ABC) but how do I
use them in practise? Do I need some kind of factory class that creates the
correct class, based on the category it extracts from the data?
Jul 22 '05 #1
7 1201
Richard Pope wrote:
I have some existing code that I am trying to upgrade to c++. The data in
question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits represent the "category" of the data, and determine how the rest of the two bytes are interpreted.
The existing code works. Leave it alone.

If you need the ability to easily change the code, write an emulator for
this hardware, then write lots of tests

If you need to improve other code's ability to call this code, wrap it in
"Facade" from the book /Design Patterns/.

There is no benefit to making a design "more OO" just for the sake of
changing it. And hardware generally dictates the design of code accessing
it. Just make sure that all other code in your system only calls the
interface of your Facade, and never gets its paws on any of those low-level
variables.
The existing code extracts various details from the data, but always uses a switch statement on the category.

I could write a single class in c++, but I would have a switch statement in every member function. This doesn't feel right somehow.
I could have a class for each category (inheriting from an ABC) but how do I use them in practise? Do I need some kind of factory class that creates the correct class, based on the category it extracts from the data?


Oh. Okay, in that case, you are correctly following this design rule: Never
use switch statements, except at an applications' boundary. Use either
Abstract Factory, Class Factory or Prototype Design Pattern to convert that
data, once, into an object. That will be the last switch statement. From
then on, only call virtual methods. Look up the refactor "Replace Type Code
with Subclasses" from the book /Refactoring/.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces

Jul 22 '05 #2
"Richard Pope" <ri**********@p arasyn.com.au> wrote:
I have some existing code that I am trying to upgrade to c++. The data in
question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits represent
the "category" of the data, and determine how the rest of the two bytes are
interpreted.

The existing code extracts various details from the data, but always uses a
switch statement on the category.

I could write a single class in c++, but I would have a switch statement in
every member function. This doesn't feel right somehow.
I could have a class for each category (inheriting from an ABC) but how do I
use them in practise? Do I need some kind of factory class that creates the
correct class, based on the category it extracts from the data?


Assuming that a particular bit of data never changes its "category" then
yes, I would probably use inheritance. You should have a factory method
that contains a switch statement that constructs a particular sub-type
based on the category. This function should return an auto_ptr<Base>
object.
Jul 22 '05 #3
"Phlip" <ph*******@yaho o.com> wrote in message news:rBNTb.3211
Richard Pope wrote:
I have some existing code that I am trying to upgrade to c++. The data in question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits
represent the "category" of the data, and determine how the rest of the

two bytes
The existing code works. Leave it alone.


But it is properly factored? In each member function he has a switch
statement on the two bytes that represent the type.

--
+++++++++++
Siemel Naran
Jul 22 '05 #4
"Daniel T." <po********@eat hlink.net> wrote in message news:postmaster-
"Richard Pope" <ri**********@p arasyn.com.au> wrote:
I have some existing code that I am trying to upgrade to c++. The data in question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits represent the "category" of the data, and determine how the rest of the two bytes are interpreted.

Assuming that a particular bit of data never changes its "category" then
yes, I would probably use inheritance. You should have a factory method
that contains a switch statement that constructs a particular sub-type
based on the category. This function should return an auto_ptr<Base>
object.


Fine. Note that the factory method can use a table lookup (eg. a map<id,
object*>) instead of a switch. And a counterargument : the virtual pointer
is 4 bytes on a typical Intel/AMD box, whereas the original type is 2 bytes
(though who knows -- maybe there were 2 bytes padding).

--
+++++++++++
Siemel Naran
Jul 22 '05 #5
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote:
"Phlip" <ph*******@yaho o.com> wrote in message news:rBNTb.3211
Richard Pope wrote:

I have some existing code that I am trying to upgrade to c++. The data in question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits
represent the "category" of the data, and determine how the rest of the

two bytes
The existing code works. Leave it alone.


But it is properly factored? In each member function he has a switch
statement on the two bytes that represent the type.


I think that depends... How many switch statements are there off of this
type, and are they all in the same class or spread out all over the
place?

I personally would probably factor the switch statements out just
because I don't like the code duplication, but depending on time
constraints and the likelihood that more types will be added in the
future, I may leave the switch statements in and call it "good enough".

Refactoring is great for making code easer to change, however if there
is no pressure for the code to change, there is little need to refactor.
Jul 22 '05 #6
"Siemel Naran" <Si*********@RE MOVE.att.net> wrote:
"Daniel T." <po********@eat hlink.net> wrote in message news:postmaster-
"Richard Pope" <ri**********@p arasyn.com.au> wrote:
I have some existing code that I am trying to upgrade to c++. The data in question is two bytes, which are used to identify what information is
requested from an industrial computer (a PLC). The first few bits represent the "category" of the data, and determine how the rest of the two bytes are interpreted.

Assuming that a particular bit of data never changes its "category" then
yes, I would probably use inheritance. You should have a factory method
that contains a switch statement that constructs a particular sub-type
based on the category. This function should return an auto_ptr<Base>
object.


Fine. Note that the factory method can use a table lookup (eg. a map<id,
object*>) instead of a switch.


I would only go that far if for some reason I needed to dynamicly change
what kind of object needs to be created for each type. Otherwise, one
switch statement does not constitute code duplication.

And a counterargument : the virtual pointer
is 4 bytes on a typical Intel/AMD box, whereas the original type is 2 bytes
(though who knows -- maybe there were 2 bytes padding).


Good point, for this small of an object. The OP said the entire chunk of
data was only 2 bytes, the identifier was just a "few bits". Going with
inheritance would jack up the size of the object by quite a bit. More
than double...
Jul 22 '05 #7


Daniel T. wrote:

Refactoring is great for making code easer to change, however if there
is no pressure for the code to change, there is little need to refactor.


Refactoring also offers the opportunity for adding new bugs. If the code
works it don't need fixing.

Only refactor code if it requires changing anyway. Then integrate the
refactored code into the product, running all the tests, before
embarking on the task that called for the refactoring in the first
place. Don't combine refactoring with bug fixing or code enhancement, if
it all goes tits-up you'll not know whether it was the refactoring or
the other changes that were responsible.

Jul 22 '05 #8

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

Similar topics

8
2596
by: Eric Veltman | last post by:
Hello everyone, I've posted this question before, but got no answer, so I'll try to reformulate the question, maybe it helps :-) By the way, this is not intended as the start of an ASP.NET flamewar. Before looking at PHP, I've used ASP.NET extensively for about a year and some things that I like a lot about it : - Design and code can be easily kept separate.
9
2466
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking the wrong questions about the wrong 'topic' (polymorphism) I have posted this new question. Please don't see this as a spurious attempt to repost :-) As mentioned previously, there is a very real problem I am trying to solve, but I have reduced...
13
2105
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
6
2122
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
4
1790
by: scottrm | last post by:
I am fairly new to oo design and I am looking at developing an object oriented asp.net application which will be built on top of a relational database. I have read quite a bit of the theory but find it hard to put it into practice. In particular I am confused in terms of interacting with the database. It seems to me classes map quite closely to database tables and I end up with a bunch of methods in each class which simply call stored...
17
2723
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
11
2570
by: Peter M. | last post by:
Hi all, I'm currently designing an n-tier application and have some doubts about my design. I have created a Data Access layer which connects to the database (SQL Server) and performs Select, update, delete and inserts. I use dataset objects to pass data to and from the DAL. In my GUI (windows forms), I use databinding to bind controls to a datatable
9
2826
by: Grizlyk | last post by:
Somebody have offered std colors to C++ in the msg here: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd The main objection to the including "colors markup" is useless of the marks, the marks is unneccessary extra information. The most used way to make colored view of C++ programs is usage of classes of C++ language words (already included in C++): reserved words, user...
7
1806
by: apollonius2 | last post by:
Greetings, I have been working on a little project today to help me better understand classes in Python (I really like Python). I am a self taught programmer and consider myself to fall in the "beginner" category for sure. It was initially sparked by reading about "state machines". This was my attempt at it however I feel it is not quite where it should be: ON = "ON"
0
9703
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
9564
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
10548
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
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
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.