Click to See Complete Forum and Search --> : Possible to 'hot-swap' Objects?


DeepT
February 10th, 2005, 08:13 AM
Ok this may seem bizzar but I need something like this:

Class Alpha has method Pet.
The Implementation of Pet manipulates a Cat.
Lets say the exact nature of Cat is a Tabby.

Class Beta Derives from Alpha.

Beta does NOT have an implementation for Pet, it uses Alpha's implementation.

However, when I call 'pet' from Beta, I want the Exact nature of the Cat to be a Calico.


Now why do I want to do this? I have to Test the busniess logic of Alpha. I can not modify it in any way. The method I am testing, Pet makes a decision based on Cat's response. For testing purposes I want to make my own 'cat' who gives known responses, or more imporantly respones I want it to give. I can't really test method Pet without this.

Typically posts like this I make never get answered. If no one knows, is there some other forums I can use that have people more knowledable in such areas, espeically writting unit testable C++ code?

TheCPUWizard
February 10th, 2005, 08:34 AM
Alpha should have been architected originally to support dynamic replacement of the Cat.

The most common textnique is to not drectlly use "new" for creating members. Rather ALWAYS use a factory, then Beta could have instructed to produce a Calico when Alpha asked for a Cat.

Without knowing the exact implementation of Alpha, it is basically impossible to give a "real" solution [which is also why abstract questions like this do not get as much attention]

DeepT
February 10th, 2005, 08:53 AM
So there is no trick to this?

In general you suggest using factories for everything? So a class should not have member variables that are objects, but instead, pointers to objects?

How wouild constructors work?

For example, in Alpha's constructor:
{
Cat = new Tabby;
}

and in Beta's constructor id I have to delete Tabby and allocate a new Calico, such :
{
if (Cat)
{
delete Cat; // should always happen
}

Cat = new Calico;
}


------------------------------------------------------------------

In this case, however, I was a bit misleading. Alpha doesn't actually own a Cat. Cat has a static function I am using, so in implementation in Alpha.Pet its more like, if (Cat::Speak() == Meow).

I am still new to writting 'unit testable' code, so I do not have any feel for best practicies for writting code like this yet. All the stuff I find on this is Java specific, and Java lets you pull a few tricks that you can't do, or I do not know how to do in C++. Namely, you can hot-swap classes like this in Java.

cma
February 10th, 2005, 09:14 AM
Namely, you can hot-swap classes like this in Java.
Can you show a short small example of this or refer me to an example?

It'll be best if you can show some code on what you're trying to do, it doesn't have to be the exact code you're working on, but it should be similiar.

TheCPUWizard
February 10th, 2005, 09:39 AM
I *usually* use pointers to (data) members rather than instanciating the member implicitly during construction. A little more overhead, but it the pointers are all "smart" then no more real work on my part.

It solves alot of issues, and the performance hit of an extra level of indirection is very very very rarely a measurable issue.

As far as designing for unit testing, the best thing to do is:

1) Design the class on paper.
2) Define the public interface, and write an empty skeleton
3) Write the Test Code and make sure you can gain access to everything you need for a comprehensive test.
4) Only after the above are complete do you put the first line of code into the body of any of the classs methods.


(this is especially useful if you are just starting out with unit testing)