1 / 8

The Decorator Pattern Decorators in Java I/O classes

The Decorator Pattern Decorators in Java I/O classes. Decorator Pattern context. You want to attach additional functionality to an (existing) class dynamically… …without having to resort to sub-classing the existing class We don’t want a class explosion

aleta
Télécharger la présentation

The Decorator Pattern Decorators in Java I/O classes

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. The Decorator PatternDecorators in Java I/O classes SE-2811 Dr. Mark L. Hornick

  2. Decorator Pattern context • You want to attach additional functionality to an (existing) class dynamically… • …without having to resort to sub-classing the existing class • We don’t want a class explosion • We want to allow classes to be easily “extended” to incorporate new behavior without modifying existing code. • Existing classes are closed to modification

  3. SE-2811 Dr. Mark L. Hornick

  4. Summary • Decorators have the same super-type as the objects they decorate. • One or more decorators can be used to wrap an object. • Given that the decorator has the same super-type as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object. • The decorator adds its own behavior either before and/or afterdelegating to the object it decorates to do the rest of the job. • Objects can be decorated at any time, so we can decorate objects at runtime with as many decorators as we like. The Decorator pattern favors Encapsulation in place of Extensionaka the Open-Closed Principle

  5. The java.io package contains dozens of classes for I/O OutputStream, FileOutputStream, PipedOutputStream, DataOutputStream, ObjectOutputStream, PrintStream, PrintWriter, … Understanding the associations between them just by reading the Javadoc API is difficult SE-2811 Dr. Mark L. Hornick

  6. Knowing that the input stream classes are based on the Decorator pattern can make things easier: SE-2811 Dr. Mark L. Hornick

  7. The Decorator pattern applied to input streams SE-2811 Dr. Mark L. Hornick

  8. You can create custom stream decorators by extending FilterOutputStream and FilterInputStream • Demonstration/Exercise SE-2811 Dr. Mark L. Hornick

More Related