first exuse me please for my english, it is not a native.
here is my question:
say you have some class hierarchy
Expand|Select|Wrap|Line Numbers
- abstract class Warior
- {
- String Description;
- Armor armor;
- Weapon weapon;
- //Adittional Get/Set Properties
- }
- class HumanWarior : Warior
- {
- }
- class AlienWarior : Warior
- {
- }
- class GenericWarior : Warior
- {
- }
- abstract class Armor
- {
- }
- abstract class Weapon
- {
- }
- class AlienArmor : Armor
- {
- }
- class AlienWeapon : Weapon
- {
- }
- class HumanArmor : Armor
- {
- }
- class HumanWeapon : Weapon
- {
- }
Everyone of them can contain Armor and Weapon objects of two presented concrate types.
the problem is that not all of the posible combinations are logicaly true. for example
HumanWorior can't contain AlienArmor or AlienWeapon. Also GenericWarior can contain both types
of Armor and Weapon but not two of them togather. For example GenericWarior can't contain
AlienArmor with HumanWorior.
How can you ensure that all the objects of type HumanWorior, AlienWorior or GenericWorior will be consistent during their entire lifetime cycle. How can you enforce a creation of logicaly corrected objectes only and mantain their correct state later on, according to defined set of
rules like: HumanWorior will contain only human devices.
here some code examples:
Expand|Select|Wrap|Line Numbers
- HumanArmor letherArmor = new HumanArmor();
- HumanWeapon longBow = new HumanWeapon();
- AlienWeapon plasmaPistol = new AlienWeapon();
- AlienArmor MagneticShild = new AlienArmor();
- HumanWorior humanWorior = new HumanWorior("Bob", letherArmor, longBow);
- humanWorior.Weapon = plasmaPistol; // here i need some error throw, i guess it should be an exception but if you know some other suitable technique for that porpose, it is fine.
- GenericWorior genWorior1 = new GenericWorior("SuperMan", letherArmor, longBow); // OK
- GenericWorior genWorior2 = new GenericWorior("SuperMan", MagneticShild, plasmaPistol); // OK
- GenericWorior genWorior3 = new GenericWorior("SuperMan", MagneticShild, longBow); // Error
if there a solution early at compile time for that problem.
thnx in advance.