1 / 8

Composition , Aggregation, and Delegation

Composition , Aggregation, and Delegation. COMP 401 Spring 2014 Lecture 11 2/ 20/2014. Layers of Abstraction. Simple objects Object state (i.e., fields) are basic data types As objects become more complex…

rhett
Télécharger la présentation

Composition , Aggregation, and Delegation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Composition, Aggregation, and Delegation COMP 401 Spring 2014 Lecture 11 2/20/2014

  2. Layers of Abstraction • Simple objects • Object state (i.e., fields) are basic data types • As objects become more complex… • Encapsulated object state is complex enough to require additional “layers of abstraction”. • Object state is modeled by a combination of other objects. • Recall earliest examples with Triangle and Point • Lecture4, ta.v09 and ta.v10

  3. Composition and Aggregation • Two design techniques for creating an object that encapsulates other objects. • Whole / part relationship • Any specific situation is not necessarily strictly one or the other. • In a nutshull… • Composition • The individual parts that make up the whole are “owned” solely by the whole. • They don’t otherwise have a reason for being. • Aggregation • The individual parts that make up the whole may also exist on their own outside of the whole. • Or even as a partof other objects.

  4. Example of Aggregation • lec11.ex1 • Course • Models a course at the university • Encapsulates Room, Professor, and a list of Student objects

  5. Characteristics of Aggregation • Encapsulated objects provided externally • As parameters to constructor • Getters and setters for these components often provided. • Encapsulated objects may be independently referenced outside of the aggregating object • Including possibly as part of another aggregation.

  6. Example of Composition • lec11.ex2 • Car • Two implementations: HondaOdyssey and Porche911 • Both encapsulate an implementation of Horn and an implementation of Engine • Implementations of Engine written with inheritance. • EngineImpl provides most of the implementation • Abstract class • ManualEngine and AutomaticEngineare subclasses • Override setGear()

  7. Characteristics of Composition • Encapsulated objects created internally • Usually within the constructor • No setters and often no getters • Encapsulated objects do not make sense outside of the abstraction. • Not shared with other abstractions • Functionality / state of encapsulated objects only accessible through the abstraction

  8. Delegation • Claiming an “is-a” relationship with an interface but relying on another object to actually do the work. • Can occur with either aggregation or composition. • Although more common with composition • Composition example revisited • lec11.ex3 • Both Car implementations also now claim Horn and/or AdjustableHorn interfaces as well. • Actual work of these interfaces delegated to internal horn object.

More Related