-
1
- #1
EdwardJS024
Programmer
I wanted to kick off an interesting oo topic to possibly get users feedback on their way of thinking during oo design.
Like many of you, oo books lay read on my desk. None of them have really ever presented any formal principal for assigning appropriate responsibilities/behavior to objects. Or, not that I can recall.
Books, articles and people I talk to say it just comes with experience. Knowing what responsibilites an object is capable of is an interesting thinking process and I wish to share my approach to design. I sparked this session off because I stumbled across a thread that reminded me of a similar situation. The user states his problem as:
"....If I have a 'Staff' class in a Library System and a staff member is able to carry out operations to update the library catalogue (ex: add book, delete book, etc), where exactly do I put these operations? Do they go in the 'Staff' class, or do they go in the 'Book' class?"
--
I applaud this user for posting such a question. Many times there are engineers who may not ask because they feel it's just too simplistic and commen sense will kick in sooner or later.
Here is a similar Q in the problem domain that i had some time ago:
"I have a student and a class object. The student needs to register with the class. Who should be assigned the Register behavior? Should the student.Register(withClass) or should the Class.Register(aStudent) ??"
Now, some might say that this is a newbie's learning curve to the big game, and they are wrong. As simple as it seem, what principal is there to follow to assign the proper responsibility to a given set of collobrating objects? This simple example scratches the surface of even more complex systems. I really do want you guys to give me feedback on your approach for correct responsibility assignment.
I'll get the ball rolloing by stating my principal for behavioral assignment in containment/collection relationships.
"Objects that controls policy making decisions in a problem domain will have appropriate behavior to act accordingly."
Simply stated, If an object has knowledge of making decisions and controling the policy, then let that object get the behavior to act.
I'll like to mention that this follows true for alot of containment relationships. I am still researching and understanding more about this concept.
Let's take the student class relationship again. In this problem, I realized that student controls no policy on how the registration process works. The class is the object that makes those decisions. The class knows how to register and controls the main policies in this abstraction. Student does not care how registration occurs in the system, student just needs to get into the class.
In the above "Staff member vs. LibraryCatalouge" system. Who controls the policy of how books are added and removed? The engineer further mentions if he should place the adding/removing behavior in the Book or Staff class.
I eliminated Book immediately. Book cannot add itself to the catalouge. A book is a book, standalone and has no knowledge of adding algorithms. Staff members are simply controllers or drivers in the system, they accept books and delegating the real adding to the Catalouge [picture a triangle]. It does not make sense to have staff memers know specifically how to add items to the cataloug. Thats where the delegation comes in, the staff member can delegate the adding to the Catalouge, it's the catalouge that really controls how catalouging, indexing and catagorizing works.
Imagine a staff member handing over a book to the catalouge. The catalouge determins that there are already 20 duplicates in the system and no more should be added, so the book is rejected. Here the Catalouge makes that decision.
What are your opinions ideas? Here i have presented a principal that holds true in containment relationships, Do youguys have any other?
EdwardJS
Like many of you, oo books lay read on my desk. None of them have really ever presented any formal principal for assigning appropriate responsibilities/behavior to objects. Or, not that I can recall.
Books, articles and people I talk to say it just comes with experience. Knowing what responsibilites an object is capable of is an interesting thinking process and I wish to share my approach to design. I sparked this session off because I stumbled across a thread that reminded me of a similar situation. The user states his problem as:
"....If I have a 'Staff' class in a Library System and a staff member is able to carry out operations to update the library catalogue (ex: add book, delete book, etc), where exactly do I put these operations? Do they go in the 'Staff' class, or do they go in the 'Book' class?"
--
I applaud this user for posting such a question. Many times there are engineers who may not ask because they feel it's just too simplistic and commen sense will kick in sooner or later.
Here is a similar Q in the problem domain that i had some time ago:
"I have a student and a class object. The student needs to register with the class. Who should be assigned the Register behavior? Should the student.Register(withClass) or should the Class.Register(aStudent) ??"
Now, some might say that this is a newbie's learning curve to the big game, and they are wrong. As simple as it seem, what principal is there to follow to assign the proper responsibility to a given set of collobrating objects? This simple example scratches the surface of even more complex systems. I really do want you guys to give me feedback on your approach for correct responsibility assignment.
I'll get the ball rolloing by stating my principal for behavioral assignment in containment/collection relationships.
"Objects that controls policy making decisions in a problem domain will have appropriate behavior to act accordingly."
Simply stated, If an object has knowledge of making decisions and controling the policy, then let that object get the behavior to act.
I'll like to mention that this follows true for alot of containment relationships. I am still researching and understanding more about this concept.
Let's take the student class relationship again. In this problem, I realized that student controls no policy on how the registration process works. The class is the object that makes those decisions. The class knows how to register and controls the main policies in this abstraction. Student does not care how registration occurs in the system, student just needs to get into the class.
In the above "Staff member vs. LibraryCatalouge" system. Who controls the policy of how books are added and removed? The engineer further mentions if he should place the adding/removing behavior in the Book or Staff class.
I eliminated Book immediately. Book cannot add itself to the catalouge. A book is a book, standalone and has no knowledge of adding algorithms. Staff members are simply controllers or drivers in the system, they accept books and delegating the real adding to the Catalouge [picture a triangle]. It does not make sense to have staff memers know specifically how to add items to the cataloug. Thats where the delegation comes in, the staff member can delegate the adding to the Catalouge, it's the catalouge that really controls how catalouging, indexing and catagorizing works.
Imagine a staff member handing over a book to the catalouge. The catalouge determins that there are already 20 duplicates in the system and no more should be added, so the book is rejected. Here the Catalouge makes that decision.
What are your opinions ideas? Here i have presented a principal that holds true in containment relationships, Do youguys have any other?
EdwardJS