Object Consistency

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zavulon
    New Member
    • Mar 2008
    • 1

    Object Consistency

    Hi all,

    first exuse me please for my english, it is not a native.

    here is my question:

    say you have some class hierarchy

    Code:
    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
    {
    
    }
    Given that hierarchy there can be many different objects of HumanWorior or AlienWorier type.
    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:

    Code:
    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
    i guess there is a solution at runtime with throwing an exeption but
    if there a solution early at compile time for that problem.

    thnx in advance.
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    It sounds like you need to create some kind of validation class where you will pass in the warrior, weapon, and armor. Then based on the rules for your game you will return if the combination of the 3 is valid or invalid. Then you will display a message to the user if they are trying to add weapons or armor to a warrior that cannot use the weapon or armor. If the combination is valid just allow the user to add the selected items.

    Nathan

    Comment

    Working...