Design patterns: Composite vs. Composition

Composition This is a design concept (not really a pattern). This term is used when you want to describe one object containing another one. It occurs very often in Composition over inheritance discussion. Moreover, composition implies strong ownership. One objects owns (i.e. manages the lifecycle) of another object. When parent is destroyed, all children are …

Read more

Aggregation vs Composition vs Association vs Direct Association

Please note that there are different interpretations of the “association” definitions. My views below are heavily based on what you would read in Oracle Certification books and study guides. Temporary association A usage inside a method, its signature or as a return value. It’s not really a reference to a specific object. Example: I park …

Read more

Distinguishing between delegation, composition and aggregation (Java OO Design)

Delegation public class A { private B b = new B(); public void methodA() { b.methodB(); } } When clients of A call methodA, class A delegates the call to B‘s methodB. Rationale. Class A exposes behaviours that belong elsewhere. This can happen in single-inheritance languages where class A inherits from one class, but its …

Read more

Why use inheritance at all? [closed]

[note: This question was originally tagged as being language-agnostic. Based on that, this answer was written to be fairly language agnostic, so it discusses inheritance as it’s used across a wide range of languages, such as Smalltalk, C++, and Object Pascal. It’s since been re-tagged as being specifically about Java. Java is different in defining …

Read more