Decorator(COMP(401,(Spring(2012(Lecture(18(3/21/2013(A(mo>va>ng(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.(• Design:(– void(recordInLog(SongInterface(s)(– void(recordInLog(SongInterface(s,(Date(>me)(– Date(lastPlayed(SongInterface(s)(COMP(401(::(Spring(2012( 2(Date(• Java’s(class(for(dealing(with(Date(– Represents(a(point(in(>me(down(to(millisecond(precision.(• Separate(classes(for(formaVng(and(calendar(opera>ons.(– Calendar(– DateFormat(• Provides(a(number(of(preXdefined(formats(– SimpleDateFormat(• Allows(you(to(construct(customized(formats(• See(Date(tutorial(on(Oracle(site(for(more.(COMP(401(::(Spring(2012( 3(SongLog(version(1(• lec18.v1(• Strategy:(– Maintain(two(array(lists(• One(for(songs(• One(for(dates(COMP(401(::(Spring(2012( 4(SongLog(v1(cri>que(• It(works,(but(not(as(clean(as(it(could(be.(– Why?(COMP(401(::(Spring(2012( 5(Decorator(Pa_ern(• Useful(when(you(want(to(add(addi>onal(state(or(func>onality(to(a(class(without(formally(subclassing.(– When(might(that(be?(• Addi>onal(state/func>onality(is(auxiliary(to(object’s(main(purpose.(• Addi>onal(state/func>onality(is(local(to(a(collec>on(or(some(other(class(encapsula>ng(the(object.(– SongLog(for(example.(• As(a(workaround(for(mul>ple(inheritance(– Want(to(build(up(an(object(with(subsets(of(different(func>onality.(– Decorator(pa_ern(is(a(form(of(delega>on.(COMP(401(::(Spring(2012( 6(Decorator(Pa_ern(Recipe(• SeVng(up:(– Start(with(original(interface.(• If(decora>ng(a(class(without(an(interface,(refactor(original(class(to(have(an(interface.(– In(our(example:(• Song((this(is(the(interface)(• SongImpl(COMP(401(::(Spring(2012( 7(Decorator(Pa_ern(Recipe(• Step(1:(– Extend(interface,(declaring(addi>onal(func>onality.(• In(our(example:(– LoggedSong(• Date(getDate();(COMP(401(::(Spring(2012( 8(Decorate(Pa_ern(Recipe(• Step(2:(– Create(class(that(implements(decorated(interface.(• Provide(constructor(that(takes(an(object(of(the(original((i.e.,(undecorated)(interface(type(and(possibly(any(addi>onal(state(informa>on(needed(for(decorated(behavior.(– Delegate(original(interface(methods(to(encapsulated(object.(– Provide(implementa>ons(for(decorated(behavior.(• In(our(example:(– LoggedSongImpl(• public(LoggedSongImpl(Song(s,(Date(d)(• Date(getDate()(COMP(401(::(Spring(2012( 9(SongLog(v2(• This(version(decorates(the(songs(as(logged(songs(and(then(stores(them.(COMP(401(::(Spring(2012( 10(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(dis>nct(from(subclassing(– Both(are(types(of(inheritance,(but(not(the(same(• This(is(why(we(encounter(statements(like:(– Decorators(provide(a(flexible(alterna>ve(to(subclassing(for(extending(func>onality.(– 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)(implementa>on.(COMP(401(::(Spring(2012( 11(Decorator(Illustrated(COMP(401(::(Spring(2012( 12(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()({(((((((((((...(((((((}(}(Unwrapping(the(Decorator(• When(you(decorate(an(exis>ng(object,(you(are(crea>ng(a(new(object.(– Exis>ng(object(is(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.(• lec18.v3(COMP(401(::(Spring(2012( 13(Undecora>ng(• Provide(method(to(get(back(original(in(decorated(interface.(COMP(401(::(Spring(2012( 14(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()({(((((((((((...(((((((}(}(Assignment(5(• You(can(add(new(state(to(piece(subclasses(if(you(need(/(want(to.(– But(you(shouldn’t(have(to.(• You(can(create(new(methods(in(the(parent(class(as(helpers(for(your(subclasses.(– Be(sure(to(mark(as(“protected”(to(provide(access.(–
View Full Document