1 / 17

Lecture 10: Decorator Pattern

CSC 313 – Advanced Programming Topics. Lecture 10: Decorator Pattern. Open-Closed Principle. Classes should be open for extension, but closed to modification So, what does this mean?. Reality Check. Classes should be open for extension… Want to update other’s code

carlo
Download Presentation

Lecture 10: Decorator Pattern

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. CSC 313 – Advanced Programming Topics Lecture 10:Decorator Pattern

  2. Open-Closed Principle Classes should be open for extension, but closed to modification • So, what does this mean?

  3. Reality Check Classes should be open for extension… • Want to update other’s code • Other is important emphasis • Everyone is other to someone • Easily create subclasses to: • Add functionality • Includeadditional fields • Specialize behavior via overriding

  4. More Reality Check …But closed to modification

  5. More Reality Check …But closed to modification • Nobody better mess with your code • YOUR code being beauty incarnate usually • Only add errors & will soil your perfection • Never let anyone: • Make changes which violate basic assumptions • Alter tasks it performs perfectly already • Anything that might introduce bugs

  6. Strategy Pattern & OCP • Pattern stays true to open-closed principle • Using pattern, context open for extension • New functionality created by changing strategy • If more data is needed, subclasses can add fields • Context closed to modification despite all this • Actions limited by data provided to strategies • Strategies focused on task & cannot do other acts

  7. Observer Pattern & OCP • Observer pattern devotee of principle, too • Using this pattern, subject open for extension • Responding to events open to each Observer • In pull model, extend Subject to make more info • Cannot modify Subject using this pattern • Limits of pattern means Observers only re-act • Subject chooses events to expose to Observers

  8. Warning Remainder of lecture unsafe for: Beginning programmers Unwilling to consider new viewpoints Stubbornly dogmatic Stupid Liberal arts majors

  9. Inspiration For Pattern

  10. Making a Cultured Pearl • Pearl made when piece of shell enters oyster • Irritant required to start making of pearl • To avoid irritation, shell grown around it • Layer after layer of material added by the oyster • As it grows, each layer adds new affect • Color, shine, shape modified by every layer added • So each layer adds something, but always a pearl

  11. Problem At Hand • Have single key main concept • Coffee • Paycheck • Pizza • Characters • But many ways to adjust and extend concept • Cream, sugar, soy, mocha, caffeine, whip, chains • Federal & State income tax, FICA, health insurance • Bacon, pineapple, ham, anchovies, mushrooms • Bold, italic, underline, color, small caps, error

  12. Add Fields for properties • Add field for each possible property • Is double whip, decaf, soy water possible? • Multiple states need taxes; how to handle this? • Using fields is both hard & inefficient • Must modify class for each new property we need • Creates huge number of rarely used fields

  13. Add Fields for properties • Add field for each possible property • Is double whip, decaf, soy water possible? • Multiple states need taxes; how to handle this? • Using fields is both hard & inefficient • Must modify class for each new property we need • Creates huge number of rarely used fields • Class open to everyone to play with & change • This very clear violation of open close principle

  14. Create Subclasses • Define subclass for each possible situation • Need both MochaWhipCoffee & WhipMochaCoffee? • Every pizza topping combination must be written • Good news: this follows open-close principle • But code duplicated in many places • Who’ll modify classes to update New York tax rate? • Maintaining this code will be pain for someone

  15. Decorator Pattern Intent • Add, but not destroy, existing class instances • Double whip, soy, decaf coffee is just coffee • Theseare still letters ‘t’, ‘h’, ‘e’, ‘s’, & ‘e’ • After taxes, my paycheck still (barely) paycheck • Add functionality using these additions • Wrapping around character changes its display • Add $5.00 to price for calling coffee “tall” • This pattern can also add information • Many attachments included with my e-mail

  16. Decorator Pattern Usage • Flexibly add adjectives to main class’s noun • These decorations provide additional description • Not required to use each decoration in each case • Wrappings can be stacked in any order • Enhance class’s actions by adding adverbs • Use additional information to improve result • Include additional tasks that must be performed • Lets main instance be invisiblyaugmented

  17. For Next Lecture • Lab #2 due before next lab • Asks you to implement the Strategy Pattern • Read rubric • Read pages 95 – 105 in the book • How do we implement the design pattern? • Can anyone actually use this? • What is the main point of this pattern?

More Related