473,466 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Object Consistency

1 New Member
Hi all,

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
  1. abstract class Warior
  2. {
  3.     String Description;
  4.     Armor armor;
  5.     Weapon weapon;
  6.  
  7.  
  8.     //Adittional Get/Set Properties
  9.  
  10. }
  11.  
  12. class HumanWarior : Warior
  13. {
  14.  
  15. }
  16.  
  17. class AlienWarior : Warior
  18. {
  19.  
  20. }
  21.  
  22. class GenericWarior : Warior
  23. {
  24.  
  25. }
  26.  
  27.  
  28. abstract class Armor
  29. {
  30.  
  31. }
  32.  
  33. abstract class Weapon
  34. {
  35.  
  36. }
  37.  
  38.  
  39. class AlienArmor : Armor
  40. {
  41.  
  42. }
  43.  
  44. class AlienWeapon : Weapon
  45. {
  46.  
  47. }
  48.  
  49. class HumanArmor : Armor
  50. {
  51.  
  52. }
  53.  
  54. class HumanWeapon : Weapon
  55. {
  56.  
  57. }
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:

Expand|Select|Wrap|Line Numbers
  1. HumanArmor letherArmor = new HumanArmor();
  2. HumanWeapon longBow = new HumanWeapon();
  3. AlienWeapon plasmaPistol = new AlienWeapon(); 
  4. AlienArmor MagneticShild = new AlienArmor();
  5.  
  6. HumanWorior humanWorior = new HumanWorior("Bob", letherArmor, longBow);
  7. 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.
  8.  
  9.  
  10. GenericWorior genWorior1 = new GenericWorior("SuperMan", letherArmor, longBow); // OK
  11. GenericWorior genWorior2 = new GenericWorior("SuperMan", MagneticShild, plasmaPistol); // OK
  12. 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.
Mar 19 '08 #1
1 1109
nateraaaa
663 Recognized Expert Contributor
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
Mar 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
1
by: Sergio | last post by:
Hi everybody, I have the following scenario. Several computers with shared disk in a LAN. Each of these computer has a MySQL server that serves several databases. I have several clients that...
2
by: bruce strom | last post by:
I have a sql database I run dbcc checkdb and get no errors I go to backup the database in enterprise manager, and it won't restore because it has internal consistency errors I can go back to an...
19
by: Catherine Jo Morgan | last post by:
If one user enters "Laurel Drive" and another enters "Laurel Dr." and still another forgets the period and enters "Laurel Dr" - is there a way to enforce consistency so that "103 Laurel Drive"...
6
by: Daniel Klein | last post by:
A simple question hopefully... Can the Object GetHashCode() method be used to obtain a unique identifier for an instance of an object? To put this another way, what does the...
11
by: Richard Lewis Haggard | last post by:
Is it possible to put a reference to an object inside of a class? If so, what is the syntax? The reason I want to do this is so that I can make a copy of the original object, make modifications to...
20
by: asdf | last post by:
True or false ?
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.