Tuesday, 27 August 2013

What is the best way to model this (simple composition like strategy pattern or generics)?

What is the best way to model this (simple composition like strategy
pattern or generics)?

For the ease communicating the question properly, i tried to use common
things so that everyone can correlate, comprehend quickly and help me to
find proper resolution :). feel free to take a peek at the code straight
away as probably comprehending code and reading comments is better than my
explanation. let me try any way.
Say, I have carbase which represents bases class for all cars like honda,
toyota, ford etc..). Similarly say, i also have truck base. (all different
vehicle categories). And also please note that each honda vehicle have
some common attributes which the hondabaseobject represents.
Now, i want to define a HondaCivic. ideally it will be hondacivic :
hondacar, hondabaseobject.
but since .net doesnt allow multiple inheritance i am pondering over
various ways to model it.
Objects -
I want to access all honda vehicles with honda base object, all cars with
carbase, all trucks with truckbase etc...
Please see the below model i have come up with. i am OK with it, but for
some reason i dont like the interface i defined (IHondaBaseObject)
I just defined it so that i can handble all honda vehicles with it as i
know all honda vehicles composes honda base object and implements this
contract.
Is there better way to handle it?
Probably somehting like HondaCivic : HondaCar - but handling is difficult
as i cannot simply convert HondaCivic as HondaBaseObject - with the
interaface i can simply interchange types. Hope i communicated the
question properly. Time to sleep as its already morning - will try to make
some edits tomorrow if the question itself is confusing based on feedback.
thanks for help and suggestiosns :)



looks like its similar to Multiple inheritance in C# basically achieving
multiple inheritance in c#
i.e. HondaCivic : CarBase, HondaBaseObject <=> HondaCivic : CarBase,
IHondabaseObject { HondaBaseObject Honda; }
class vehicle
{
}
/// <summary>
/// Base class for all the cars (common fields and operations of car)
/// </summary>
class CarBase : vehicle
{
//parts
virtual steering steering {get;}
virtual void StartingTheCar(...)
//engine
}
class HondaCar : CarBase
{
//overrides
//specializations
}
class ToyotaCar : CarBase
{
//overrides
//specializations
}
class TruckBase : vehicle
{
//overrides
//specializations
}
class HondaTruck : TruckBase
{
//overrides
//specializations
}
class ToyotaTruck : TruckBase
{
//overrides
//specializations
}
//Is it necessary? - i introduces this so that as i want to access
all types of hoda vehicles with a single type
//but having interface, just for the sake of handling doesnt make
sense?
//i am not sure - but, looks like something is wrong.
interface IHondaBaseObject
{
}
/// <summary>
/// Object which contains the common attributes for each honda
vehicle (say cars, suvs, trucks, bikes etc...)
/// </summary>
class HondaBaseObject
: IHondaBaseObject
{
string identity { get; set; }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
//Now what is the best way to model it? should i use generics? or
just a simple stragey pattern?
class HondaCivic : HondaCar, IHondaBaseObject
{
HondaCivic(HondaBaseObject ho)
{
}
public HondaBaseObject { get; }
}

No comments:

Post a Comment