To apply Decorator to a class we wrap class by creating a subclass to follow

To apply decorator to a class we wrap class by

This preview shows page 429 - 431 out of 517 pages.

To apply Decorator to a class, we “wrap” class by creating a subclass (to follow the Liskov Substitution Principle, as we’ll learn in Section 11.5 ). The subclass delegates to the original method or class for functionality that isn’t changed, and implements the extra methods that extend the functionality. We can then easily “build up” just the version of PdfFormatter we need by “stacking” decorators. 1 # reopen Mailer class and decorate its send_email method. 2 class Mailer 3 alias_method_chain :send_email, :cc 4 def send_email_with_cc(recipient,body) # this is our new method 5 send_email_without_cc(recipient,body) # will call original method 6 copy_sender(body) 7 end 8 end 9 # now we have two methods: 10 send_email(...) # calls send_email_with_cc 11 send_email_with_cc(...) # same thing 12 send_email_without_cc(...) # call (renamed) original method Figure 11.15: To decorate an existing method Mailer#send_email , we reopen its class and use alias_method_chain to decorate it. Without changing any classes that call send_email , all calls now use the decorated version that sends email and copies the sender. Python’s “decorators” are, unfortunately, completely unrelated to the Decorator design pattern. In the wild, the ActiveSupport module of Rails provides method-level decoration via alias_method_chain , which is very useful in conjunction with Ruby’s open classes, as Figure 11.15 shows. A more interesting example of Decorator in the wild is the Rack application server we’ve been using since Chapter 2 . The heart of Rack is a “middleware” module that receives an HTTP request and returns a three-element array consisting of an HTTP response code, HTTP headers, and a response body. A Rack-based application specifies a “stack” of middleware components that all requests traverse: to add a behavior to an HTTP request (for example, to intercept certain requests as OmniAuth does to initiate an authentication flow), we decorate the basic HTTP request behavior. Additional decorators add support for SSL (Secure Sockets Layer), measuring app performance, and some types of HTTP caching. Summary of Open/Closed Principle: To make a class open for extension but closed against modification, we need mechanisms that enable specific extension points at places we think extensions might be needed in the future. The Case Statement design smell is one symptom of a possible OCP violation. If the extension point takes the form of a task with varying implementations for the steps, the Strategy and Template Method patterns may apply. Both are often used in conjunction with the Abstract Factory pattern, since the variant to create may not be known until runtime. If the extension point takes the form of selecting different subsets of features that “add on” to existing class behaviors, the Decorator pattern may apply. The Rack application server is designed
Image of page 429
this way.
Image of page 430
Image of page 431

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors