Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Consistency

Status
Not open for further replies.

bugmenot2

Technical User
Jan 23, 2008
12
0
0
CH
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.
 
In pure modeling terms, you're talking about constraints. If you are using UML to model your class diagram, then you would represent a constraint as a dashed line between two association lines, with a description of the constraint as an annotation between two curly brackets, such as {or}. (Googling "UML constraints" will give numerous examples.)

For your model, my first thought is to associate as follows: Warrior has an association with armor and another with weapon. (You set up the association between the abstract classes, since it doesn't vary in the inherited classes.) You then place a {not} constraint between AlienWeapon and HumanArmor, as well as between the reverse. You then place an {or} constraint between human armor and alien armor, and another for human weapon and alien weapon. Another possibility is to replace the {not} constraints with {xor} constraints and remove the {or} constraints. This is more concise but perhaps less clear.

In practical terms, you typically apply the modeled constraints (evaluate the types of the proposed weapon and armor, throw an exception if they don't satisfy the constraint) in the constructor for a given class where possible. When not possible (the association is created after the fact of construction), you will apply the constraint in a method, which could be a property set method if desired. Your bottom code window gives an example of each:
Code:
humanWarrior.Weapon = plasmaPistol; 
//replace with
humanWarrior.EquipWeapon(plasmaPistol);
//and throw the error in EquipWeapon, or you can also use a property get/set and do it that way, depending on the language you're using.

GenericWarrior genWarrior3 = new GenericWarrior("SuperMan", MagneticShild, longBow);
//do this one in the constructor for GenericWarrior.

I'm not familiar with a means of throwing such an error at compile time, but I would think that you would need to handle this at runtime anyway, given you want a user to be able to assign weapons or armor.

HTH

Bob
 
p. s. If you don't understand some of my English, please ask me to explain and I'll be glad to see if I can do that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top