Thursday 3 May 2018

What is composition?

Purpose:
  • It is likely that interviewers will ask basic object orientation questions just to test whether someone has a solid grasps of the principles behind the the languages.
  • Not being able to answer these questions are massive red flags surrounding a developer's education and grasp of the programming domain.
  • Composition is one of those terms that throws a lot of developers for some reason and is pretty good indicator of experience.

Potential Answer:
This simply refers to when one object is made of of other types. But that is not enough, what is actually being asked is for you to juxtapose this to other types of relationships like aggregation and inheritance. So something like:

  • Classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class. This forces the definition of interface representing behaviors that the system should exhibit. This gives the design higher flexibility. It is also more natural to express domain out of various components than to try and find commonality through a family tree.
  • A "owns" B = Composition : B has no meaning or purpose in the system without A
  • A "uses" B = Aggregation : B exists independently (conceptually) from A

No comments:

Post a Comment