Thursday, October 16, 2014

Object Relationships of Modeling

Understanding the object relationships goes a long way towards a solid object model design. I've gathered few notes on this subject and produced the following brief summary for reference purposes.

Is a kind of  A Car is a kind of Vehicle, An Orange is a kind of Fruit
  • Implies inheritance
  • Typically this relationship is called is-a, however that is just not as clear. Using is-a often results in a poor model with things like a Boss is-a Employee which leads to roles being implemented via inheritance, etc.

Is Part Of / Is Made Up Of /Composition – A page is part of a book
  • A book is made up of / composed of a Cover and Pages
  • Implies that the part does not exist outside of the whole. If you destroy a book, you destroy the cover and pages.
  • Part-of relationships suggest that internal objects may be best left hidden and accessed via methods on the parent
  • Book.TurnPage() instead of Book.Pages.TurnPage()

Containment / Aggregation – Point of Interests and Paths are contained in a Map
  • One object acts as a container, grouping other objects together for a purpose. The parts are not dependent on the whole. If we destroy a Map, the Points of Interest are not destroyed.

Associated With  An author is associated with a book and vice versa
  • Associated With relationships suggest that the related object is not owned by the parent like the part of relationship, and is often accessible via navigation through the domain model
  • Book.GetAuthors()
  • RaceCar.GetDriver()

Helps Describe / Identifies  A Title helps describe a Book
  • Implies that the object is inherent to the parent object and there is often less danger in exposing the object outside of the parent

Feature of / Implementation - Features of a Book include its Width and Height
  • Unlike an object that helps describe or identify another object, a feature of an object is one that should rarely if ever be exposed
  • Business logic is naturally written around features of objects. In fact, if an implementation-related object is not referenced in the business logic of the parent object we should consider removing the object

Uses  A Book uses the Pages to get the number of times a word appears in the book
  • Clarifies collaboration among objects
  • The uses relationships is often another relationship aside from how two objects might be related (Part of, Associated with, etc).

Note 1 – One might expect to find the relationship has-a in the list, but it is too vague to be helpful. Part Of, Associated With and Helps Describe are all often referred to under the fuzzy has-a umbrella.

Note 2 – These are logical relationships. Based on external factors, for example, we might decide to physically contain an object that is logically an association.

Note 3 – You may well find as I have that the terms Containment, Aggregation, and Composition are often used interchangeably. However, I think it is important to understand the types of relationships regardless of what they are called.

No comments:

Post a Comment