Re: OO, Inheritance and Queue
- From: Jianwei Sun <jsunnewsgroup@xxxxxxxxx>
- Date: Wed, 02 Aug 2006 21:54:49 -0400
query_me2001@xxxxxxxxxxx wrote:
I think one of my concerns is how the classes Car, Bicycle and Plane
are passed through the system. Should I have 3 separate overloaded
methods to deal with each class? Or shoudl I have a single class that
deals with the base Class Transport and somehow deal with determining
the actual class within the method?
I will use the first option.
I know you have three different methods in each class, but you can do it
that way, you can declare a virtual/abstract method in the Transport class called
public virtual DoAnyThing();
And in car, you want to declare OpenWindow() as a private method, and then you want to override this method.
public override DoAnyThing()
{
//Here, you can call the private defined method OpenWindow():
}
>>> class Car : Transport { OpenWindow(); }
>>> class Bicycle : Transport { RingBell(); }
>>> class Plane : Transport { TakeNap(); }
>>>
.
I had a look at the Adapter and Facade patterns and I didn't really
pick up how they would help.
Also http://www.dofactory.com/Patterns/PatternAdapter.aspx#_self2
seemed to use w few switch statements.
sloan wrote:There are 2 ways to solve this.
1, as previously mentioned.. is that you have either an Interface or
abstract class with the method.
public interface IVehicle
void DoSomething()
That would mean that Car, Bicycle and Plane implement this interface (or
abstract class), and write their own implmentations of DoSomething
#2 (which piggy backs off of #1)
If you have the Car Bicycle or Transport objects already, then you can look
at one of these design patterns.
Fascade
Adapter
http://www.dofactory.com/Patterns/Patterns.aspx
I'd probably go Adapter.
The key is that you should never/seldom/only once in a blue moon be writing
case statement checking for object type.
At least you were wise enough to ask, instead of plugging ahead.
..
<query_me2001@xxxxxxxxxxx> wrote in message
news:1154530187.951716.29050@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I have a queue that holds a list of objects with a common base type. As
a simple and contrived example, I have a common type of transportation
methods and derived classes (with there own individual methods which
extend the common base class) of Car, Bicycle and Plane:-
class Car : Transport { OpenWindow(); }
class Bicycle : Transport { RingBell(); }
class Plane : Transport { TakeNap(); }
I want to add each of these classes to the buffer. So I can enqueue
with:
Queue transportQueue;
transportQueue.Enqueue(MyCar);
transportQueue.Enqueue(MyBicycle);
transportQueue.Enqueue(MyPlane);
Now this is where I have got concerns. When I dequeue I do not know
what type has been dequeued.
Transport transportMethod;
transportMethod = (Transport) transportQueue.Dequeue();
When I dequeue, I know that it is a transport class but I do not know
if it is a Car, Bicycle or Plane.
I suppose I could use:
switch (typeof(transportMethod))
{
case Car:
break;
case Bicycle:
break;
case Plane:
break;
}
But this does not seem very Object Oriented and not very maintainable.
Is there a better way of doing this?
- References:
- OO, Inheritance and Queue
- From: query_me2001
- Re: OO, Inheritance and Queue
- From: sloan
- Re: OO, Inheritance and Queue
- From: query_me2001
- OO, Inheritance and Queue
- Prev by Date: Re: Has anyone used the settings class?
- Next by Date: Re: FileSystemWatcher and file locking?
- Previous by thread: Re: OO, Inheritance and Queue
- Next by thread: Re: OO, Inheritance and Queue
- Index(es):
Relevant Pages
|