Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
When I moved to OO from 2GL choose my objects as follows:
Each screen or database table got a class.

But it still reeks of the heavily normalized 2GL that I learned in the 80s.

E.G
I have classes for Persons, Users, Roles, Features, RoleFeatures, UserRoles, Passengers, Routes, Stages, RouteStages, Tickets, RouteTickets and so on.

In a proper OO world the Person can sit at the top of a tree, Passenger and User can inherit from it as they have the "Is a" criterium. But User also needs to draw from roles who inherit from Features.

Similarly the RouteStage class draws inherits aspects from both Route and Stage, and thats before we factor in Ticket Attributes.

Then for good measure Passenger and Ticket 'collide' when the user makes a purchase.

I am reading up on Interfaces - is this the path to take or is this a classic case of its not broken... ???
Posted
Updated 7-Mar-12 17:38pm
v3
Comments
Herman<T>.Instance 7-Mar-12 15:46pm    
what have you read already?
Ger Hayden 8-Mar-12 3:35am    
Ivor Hortons Intro to C++ using VS2008, and Stephen Fraser's Introdcing Pro C++/CLI using .NET 3.5 under VS2008. But they were a couple of years back. I'm now workng through Andrew Troelsons C# for VS2010 tome. Just reaching the class chapters this weekend.

Inheriting interfaces. You can inherit one class, and multiple interfaces. Alternatively, you can create an inheritance chain. I suppose you could also use dependency injection to get around it.

When using MFC (unmanaged C++), you couldn't multiple inherit from anything derived from CObject. It was (and still is?) a royal pain in the ass.
 
Share this answer
 
v3
Another conceptual point to look at is the difference between "is" and "has": a User is a Person, while a User has some Roles. Consequently, Role is property of User. For a Role, there are some Users; if you make Users a property of Role, then you'd get into a crazy loop. I'd create a function GetUsersForRole(Role role) if such a thing is needed.
 
Share this answer
 
Comments
Ger Hayden 8-Mar-12 3:38am    
Excellent train of thinking and similarly a Route has Stages...

But a stage is a constituent of the route!
CPallini 8-Mar-12 3:44am    
'Composition'. My 5.
Interfaces are definitely the way to go. You shouldn't really use inheritance any more (apart from interface), as it causes more problems that it's worth. You should go for composition and when you need shared functionality, move that to a service class that works over the interface to do what you need. That way you get re-usability without the inheritance.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900