INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

UML Class Modeling Inheritance

UML Class Modeling Inheritance

(OP)
Hi,

I'm new to UML and... well... good class design purpleface. Anyway, I'm working on a Class diagram that has me puzzled. To make this easy, I'll use an analogy, rather than describe my project in great detail. Let's say I have a Use Case diagram, and on it, I have two actors:

CODE

Tenant
 /\
 |
<<inherits>>
 |
Landlord
So, essentially a tenant lives in a building and so does a landlord.

Now, let's say that Tenant can "Do X to Building" and "Do Y to Building", and Landlord can "Do Z to Building" because Landlord has extra special privileges.

Here's where I'm having a problem. Assuming that I have a Tenant class and a Building class as follows:

CODE

================
 Tenant
================
 -phoneNumber
================
 +getPhoneNumber()
 +setPhoneNumber()
================
| 0..*
|
| /\
|has
|
| 1
================
 Building
================
 -address
 -landLordPhoneNumber
================
 +doXtoBulding()
 +doYtoBulding()
 +doZtoBulding()
================
Is there really a need for a Landlord class? Assume for a moment that Tenant.phoneNumber was a unique key. Wouldn't it make sense to simply check who is calling Building.doZtoBuilding() and if that Tenant doesn't have the same phoneNumber as is stored in Building.landLordPhoneNumber then don't allow the method to execute any further? Since the "do" methods are acting upon building, it doesn't make sense to make them methods of Tenant or Landlord classes. And, if the "do" methods aren't methods of the Tenant or Landlord classes, then the Tenant and Landlord classes would essentially have the same properties and methods. And, since a Landlord is also a Tenant, having a Landlord class would be redundant using the above design.

I hope that made some sense. I'm trying to figure out if this is a bad design, and if there is a need for a Landlord class.

Thanks

RE: UML Class Modeling Inheritance

<Wouldn't it make sense to simply check who is [etc]

Well, you could do it that way, but the way you have it diagrammed is actually a great example of specialization.  The landlord "is a" tenant.  As such, he has all of the attributes and behaviors of a tenant.  You start from there, and add specialized attributes and behaviors that are only for a landlord.

I would NOT have a different "landLordPhoneNumber" attribute.  I would also have the address attribute be in the tenant class.  Finally, based on your description, doxtobuilding and doytobuilding should go in the tenant class.  The landlord does them due to the fact of being a tenant ("GetMail" leaps to mind) rather than the fact of being a landlord.

<Is there really a need for a Landlord class?

This is the same question as "is there really a need for inheritance?"  The point about inheritance is that it's an efficient means to reuse code in situations like these.  Create the tenant class, then inherit the landlord class and add the specialized functionality.

HTH

Bob

RE: UML Class Modeling Inheritance

(OP)
Thanks. That was very helpful.

RE: UML Class Modeling Inheritance

You're most welcome.  smile  Now, you also might want to have a look at things like polymorphism and overriding.  For an example of overriding, let's take "GetMail" again.  The landlord might get his own mail, and then also go and get the mail for the business.  So, you can override the GetMail method that the Tenant class exposes.  In the Landlord class's GetMail method, you would call the Parent class's (the Tenant class's) GetMail method, and then add specialized code to get the business mail.  Again, code reuse in another way.  If you ever needed to change the Tenant GetMail method, you wouldn't (well, hopefully you wouldn't--read up on "fragile base class problem") break the Landlord's GetMail method, since it would automatically call the new code.

Beautiful, isn't it?  smile

Bob

RE: UML Class Modeling Inheritance

(OP)
Thanks again. I'll consider implementing these OO features, as I'm still working on the design.

RE: UML Class Modeling Inheritance

Two good books: "UML Distilled" by Martin Fowler and "The UML Bible" by Tom Pender.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close