|
An object called MovableObject can move through different methods.
MoveByLegs, wheels & Wings. When we choose one method, i.e MoveByLegs and pass Legs arguments, the other two will be unused.
Should I design it like this:
enum MoveType
{
MoveByLegs,
MoveByWheels,
MoveByWings
}
class MovableObject
{
List<leg> lstLegs;
List<Wheels> lstWheels;
List<Wing> lstWings;
MoveType m_Movetype;
EnableMovement(MoveType movetype_in, object obj_in)
{
m_Movetype = movetype_in;
switch (movetype_in)
{
case MoveByLegs:
lstLegs = List<Leg>(obj_in);
break;
case MoveByWheels:
lstWheels = List<Wheel>(obj_in);
break;
case MoveByWings:
lstWings = List<Wing>(obj_in);
break;
}
}
Move()
{
if(m_Movetype == MoveType.MoveByLegs)
{
}
similar case for MoveByWheels & Legs
}
}
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
Hmmm, you're mixing and matching things a lot here. As you're creating movable "things", you should consider the fact that each one of these is a separate movable type. This indicates that you should consider the fact that you're using an enum and change it to something like this:
public abstract class MovableObject
{
}
public class Legs : MovableObject
{
}
public class Wheels : MovableObject
{
} And there you have it - it's a lot cleaner and simpler to work with OO features.
|
|
|
|
|
Hmm let me explain it visually.
The movable object = An Egg.
Now you attach a set of legs, it walks through Legs.
If you attach a set of wheels, it rolls by wheels.
And when you attach a pair of wings, it flies by wings.
So We cannot inherit leg, wings, wheels from the Egg.
The Egg _has_ all these.. one at a time. Not all at the same time.
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
My design still works - change MovableObject to MovableEgg in my example and you see that it still stands (so to speak). Consider this example:
public abstract MovableEgg
{
public void Move();
}
public abstract EggWithLegs : MovableEgg
{
public virtual void Move()
{
Console.WriteLine("I'm walking");
}
}
public abstract EggWithWings : MovableEgg
{
public virtual void Move()
{
Console.WriteLine("I'm flying");
}
}
|
|
|
|
|
I guess the egg is an object with many things in it other than how it moves. Does the way it movers affects other things, like graphics etc? If yes look for "component design pattern".
If it is just a different way of movement then you can use this:
Interface IMovementMethod
{
public void Move(Egg AnEgg,...);
}
Class Egg
{
public IMovementMethod MovementMethod {get; set;}
public void Move(...)
{
MovementMethod.Move(this,...)
}
}
This way you can change the MovementMethod property if you want to change the way it moves.
|
|
|
|
|
Better example Here[^]
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
VuNic wrote: Should I design it like this:
Movement is an attribute not an entity.
So
Entity (not MovableObject) 'has' movement.
Thus objects would be
Entity
MovementWings
MovementLegs
MovementWheels.
And you set it by calling.
entity.setMovement(Movement)
The Movement itself is either enable/disabled (where 'enable' means to actually allow movement) so each Movement object would have a property to enable it. Then Entity could call that if it has Movement (if internal varible is not null.)
|
|
|
|
|
I need to explain more about the requirement. I'll come back and roll them down soon. thanks for your replies.
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
Better example Here[^]
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
Except of course that has nothing to do with movement.
|
|
|
|