Download presentation
Presentation is loading. Please wait.
Published byIra Greer Modified over 9 years ago
1
Decorator COMP 401, Spring 2014 Lecture 15 10/14/2014
2
Multiple Observer Lists Same delegation trick can be used to support multiple lists of observers. – Useful if you want to support more than one type of observable changes/events. – Requires providing a way to specify what is being observed. One approach: separate registration methods for each observable change/event. Another approach: additional argument to registration method. – May need to provide way of distinguishing between events as part of update info if you want the same observer to be able to observe more than one observable event. lec14.ex6 2
3
A motivating example: SongLog Goal: – Create an object to represent a song “log” (i.e., record of what songs were played) Provide ability to answer queries about songs played. Functionality: – void recordInLog(Song s) – void recordInLog(Song s, Date time) – Date lastPlayed(Song s) 3
4
Date Java’s class for dealing with Date – Represents a point in time down to millisecond precision. Separate classes for formatting and calendar operations. – Calendar – DateFormat Provides a number of pre-defined formats – SimpleDateFormat Allows you to construct customized formats See Date tutorial on Oracle site for more. 4
5
SongLog version 1 lec15.v1 Strategy: – Maintain two lists One for songs One for dates 5
6
SongLog v1 critique It works, but not as elegant as it could be. – Why? 6
7
Decorator Pattern Useful when you want to add additional state or functionality to a class without formally subclassing. – When might that be? Additional state/functionality is auxiliary to object’s main purpose. Additional state/functionality is local to a collection or some other class encapsulating the object. – SongLog for example. As a way of emulating multiple inheritance – Want to build up an object with subsets of different functionality. – Decorator pattern relies on delegation. 7
8
Decorator Pattern Recipe Setting up: – Start with original interface. If decorating a class without an interface, refactor original class to have an interface. – In our example: Song(this is the interface) SongImpl 8
9
Decorator Pattern Recipe Step 1: – Extend interface, declaring additional functionality. In our example: – LoggedSong Date getDate(); 9
10
Decorate Pattern Recipe Step 2: – Create class that implements decorated interface. Constructor is given an instance of the original, undecorated type. May also need to provide any additional state information needed for decorated behavior. – Delegate original interface methods to the encapsulated original object. – Provide implementations for additional decorated behavior. In our example: – LoggedSongImpl public LoggedSongImpl(Song s, Date d) Date getDate() 10
11
SongLog v2 This version decorates the songs as logged songs and then stores them. 11
12
Decorator Avoids Subclassing Decorator applies to interfaces (not classes). – Caveat: as I am teaching it here. – It’s the interface that is being decorated. Subinterfacing is distinct from subclassing – Both are types of inheritance, but not the same This is why we encounter statements like: – Decorators provide a flexible alternative to subclassing for extending functionality. – See Decorator in Java tutorial from Abhi On Java link in readings. – This is why we needed Song to be an interface with an accompanying class (i.e., SongImpl) implementation. 12
13
Decorator Illustrated 13 interface I { void do_something(); } class C implements I { void do_something() {... } interface DI extends I { void do_more(); } class DC implements DI { private I wrapped_i; public (I i_obj) { wrapped_i = i_obj; } void do_something() { wrapped_i.do_something(); } void do_more() {... }
14
Unwrapping the Decorator When you decorate an existing object, you are creating a new object. – Original object is encapsulated inside. Might need to have the ability to “undecorate” the object. – For example, if we need to give it back to someone who expects the original. lec15.v3 14
15
Undecorating Provide method to get back original in decorated interface. 15 interface I { void do_something(); } class C implements I { void do_something() {... } interface DI extends I { void do_more(); I getWrapped(); } class DC implements DI { private I wrapped_i; public (I i_obj) { wrapped_i = i_obj; } public I getWrapped() { wrapped_i; } void do_something() { wrapped_i.do_something(); } void do_more() {... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.