-1

i have a question about software engineering best practice.

Let's consider a class "User", with 2 subclasses "Student" and "Teacher" if we need to specify some data for "University special council" that could be consider a sort of attribute both for student and teacher, how will i set it up? is that an abstract class? a class extender? category? or what?

another example, to better understand the matter. If i'm writing a software for a food distributor, i have two classes "meat" and "fish", i need a kind of class which should be "freezable", would that be an astract class?

which one would be the right uml for the two examples and which the best implementation in code?

  • 2
    "*Let's consider a class "User", with 2 subclasses "Student" and "Teacher"*". Well there goes "software engineering best practice", right there. If you want to adopt good practice, don't use inheritance. – David Arno Oct 11 '16 at 10:45
  • 1
    To address your question though, you need to read up on composition, as that addresses both of your examples. – David Arno Oct 11 '16 at 10:46
  • why not using inheritance? what u mean about composition? can you please address me somewhere to read more? – antolaravel Oct 11 '16 at 10:48
  • 2
    Possible duplicate of [Why should I prefer composition over inheritance?](http://programmers.stackexchange.com/questions/134097/why-should-i-prefer-composition-over-inheritance) – gnat Oct 11 '16 at 10:54
  • *If you want to adopt good practice, don't use inheritance.* This is the poster child for the perversion of the guideline "prefer composition over inheritance." – radarbob Oct 11 '16 at 12:24
  • The answer depends on what "university special council" (or "freezable") are. Is this a new behaviour? Is this simply a dumb data attribute. The best practice is to look at how *behaviour* of different classes changes. I suspect "Student" and "Teacher" have different behaviour based on what "special council" is set to. That different behaviour is far more important that an attribute, and should lead the design. Doing 'data lead' design in OO is doing design wrong way around. Start with behaviour first, and when you have behaviour defined look at what data is needed to implement said behaviour – Cormac Mulhall Oct 11 '16 at 17:24

1 Answers1

0

which [would be] the best implementation...?

The best answer is "it depends on the business/domain space and your design."

The University Special Council may be its own thing; a whole context of data + behavior unto itself. This is for requirements analysis to decide. Then it is a matter of how a thing is used throughout the application.

A class is data + behavior. A class is not merely "identity." If you're designing sub-classes with no overridden or specialized behavior then take a fresh look at your design.

Anything made in the real world is a compromise. How does the totality of class design best meets requirements as informed by OO design principles?

I look for the fundamental concepts and entities and make simple classes of these. From there I have flexibility for both inheritance and composition. I can design higher-level classes in different ways with the same constituent parts. Apply this idea at all levels of abstraction.

If you think more about the API, the messages, the communication you want between objects, then implementation details tend to feel less problematic.


Abstract classes are fantastic providing common logic and processes but leaving placeholders for specialized behavior. The template pattern and template methods are what I'm talking about.

In contrast, composition can help when we want to do the same thing but in substantially different ways. For example .net has "data adapters" for various databases. A "wrapper" class gives you a consistent API while the contained classes talk to the DB itself. The buzzword for the consistent interface is a facade. Indeed these data adapters are both "adapters" and "facades".

"Interfaces" (as in the keyword interface in C# and Java) is a good way to declare specialized behavior across unrelated classes.

radarbob
  • 5,808
  • 18
  • 31