The document discusses the Decorator design pattern, which allows additional responsibilities to be attached to an object dynamically at runtime. It provides examples of how the Decorator pattern can be used to add features like borders and scrolling to views. The Decorator pattern provides a more flexible alternative to inheritance for extending object functionality compared to traditional inheritance which can result in explosion of classes.
This document introduces GUI development in Java using the Abstract Window Toolkit (AWT) and Java Foundation Classes (JFC). It discusses containers, layout managers, events, and event handling. The key GUI components introduced are JFrame for top-level windows, JPanel for grouping components, and using layout managers like BorderLayout and GridLayout to position components. It also covers adding event listeners to components to handle user interactions.
This lecture introduces Java programming and covers: the Java Virtual Machine; object oriented concepts like classes and instances; the structure of a Java program; and using the Scanner class for input/output. Key topics are compiling Java code, executing bytecode on the JVM, and the basics of classes, methods, and objects.
The document discusses exception handling in Java. It defines an exception as an event that disrupts normal program flow, such as dividing by zero. Exceptions are handled using try, catch, and finally blocks. Catch blocks handle specific exception types. Checked exceptions must be handled, while unchecked exceptions do not require handling but can cause program termination. The document provides examples of exception handling for input/output errors and file processing.
This document discusses aggregation and collection classes in Java. It introduces the concept of the "has-a" relationship between classes, where one class contains instances of other classes. It provides examples of classes like Employee that have attributes represented by other classes like PersonalDetails and JobDescription. It discusses encapsulation and exposing functionality through wrapper methods. It also introduces the concepts of ownership and aggregation vs composition. Finally, it discusses using arrays and collections to store and access groups of objects in a 1-to-many relationship.
This chapter introduces state diagrams and their components. It discusses how state diagrams describe the states of an object and transitions between states triggered by events. It covers initial and final states, actions, activities, and different types of events. The chapter also discusses transitions between states and the use of guard conditions. Finally, it introduces concepts of nested and concurrent states.
Class diagrams using UML can model the static view of a system by showing how classes are related through relationships like association and generalization. The chapter introduces class diagram notation and discusses how to identify problem domain classes and the different relationships that can exist between classes like association, aggregation, composition, and generalization. It also covers how to model classes, attributes, operations, and relationships in UML class diagrams.
This chapter discusses software design patterns, including why they evolved to capture recurring design solutions, defines a design pattern as describing communicating objects and classes that solve general problems, and describes fundamental GRASP patterns that assign responsibilities to experts, creators, or controllers to achieve high cohesion and low coupling.
This document introduces a module on programming concepts in C++. The module will cover object-oriented programming principles, C++ basics, classes and objects, inheritance, pointers, and more. Students will learn to apply OOP to model real-world systems. Their knowledge will be assessed through a final exam and group coursework assignments focusing on programming and class design.
This document introduces GUI development in Java using the Abstract Window Toolkit (AWT) and Java Foundation Classes (JFC). It discusses containers, layout managers, events, and event handling. The key GUI components introduced are JFrame for top-level windows, JPanel for grouping components, and using layout managers like BorderLayout and GridLayout to position components. It also covers adding event listeners to components to handle user interactions.
This lecture introduces Java programming and covers: the Java Virtual Machine; object oriented concepts like classes and instances; the structure of a Java program; and using the Scanner class for input/output. Key topics are compiling Java code, executing bytecode on the JVM, and the basics of classes, methods, and objects.
The document discusses exception handling in Java. It defines an exception as an event that disrupts normal program flow, such as dividing by zero. Exceptions are handled using try, catch, and finally blocks. Catch blocks handle specific exception types. Checked exceptions must be handled, while unchecked exceptions do not require handling but can cause program termination. The document provides examples of exception handling for input/output errors and file processing.
This document discusses aggregation and collection classes in Java. It introduces the concept of the "has-a" relationship between classes, where one class contains instances of other classes. It provides examples of classes like Employee that have attributes represented by other classes like PersonalDetails and JobDescription. It discusses encapsulation and exposing functionality through wrapper methods. It also introduces the concepts of ownership and aggregation vs composition. Finally, it discusses using arrays and collections to store and access groups of objects in a 1-to-many relationship.
This chapter introduces state diagrams and their components. It discusses how state diagrams describe the states of an object and transitions between states triggered by events. It covers initial and final states, actions, activities, and different types of events. The chapter also discusses transitions between states and the use of guard conditions. Finally, it introduces concepts of nested and concurrent states.
Class diagrams using UML can model the static view of a system by showing how classes are related through relationships like association and generalization. The chapter introduces class diagram notation and discusses how to identify problem domain classes and the different relationships that can exist between classes like association, aggregation, composition, and generalization. It also covers how to model classes, attributes, operations, and relationships in UML class diagrams.
This chapter discusses software design patterns, including why they evolved to capture recurring design solutions, defines a design pattern as describing communicating objects and classes that solve general problems, and describes fundamental GRASP patterns that assign responsibilities to experts, creators, or controllers to achieve high cohesion and low coupling.
This document introduces a module on programming concepts in C++. The module will cover object-oriented programming principles, C++ basics, classes and objects, inheritance, pointers, and more. Students will learn to apply OOP to model real-world systems. Their knowledge will be assessed through a final exam and group coursework assignments focusing on programming and class design.
The document summarizes and compares several major object-oriented programming languages: Smalltalk, C++, Eiffel, and Java. Smalltalk was designed for interactive use and has a fully integrated development environment, while C++ was designed for production use and has less integration. Eiffel focuses on ensuring code correctness but had poor tooling. Java became popular for web development due to its portability and is expected to further object-oriented adoption.
This document introduces multi-threading in Java. It discusses that threads allow a program to have multiple paths of execution. Threads can be created by extending the Thread class or implementing the Runnable interface. The key method for threads is run, which contains the logic to be executed. The document covers starting threads, potential problems with threads accessing shared resources, and techniques for thread synchronization like synchronized methods, waits and notifies.
This chapter introduces activity diagrams and discusses where they are used. Activity diagrams can model workflows, operations, and dynamic aspects of a system. They contain activities, transitions, synchronization bars, and decision activities. Activity diagrams are suitable for analyzing use cases, understanding workflow, and modeling multithreaded applications. They are not suitable for showing object collaboration or lifetime behavior.
This document introduces a module on programming concepts in C++. The module will cover object-oriented programming principles, C++ basics, classes, inheritance, pointers, and more. Students will learn to apply OOP to model real-world systems. Their knowledge will be assessed through a final exam and group project, both testing their ability to explain and apply core OOP concepts and develop class structures.
This document discusses abstract classes and interfaces in Java. It introduces abstract classes using a drawing package example, showing how abstract classes like Shape can be extended by classes like Rectangle and Circle. Interfaces are also introduced, explaining that they specify what an object can do without defining how. Classes can implement multiple interfaces to take on those required behaviors.
Class diagrams using UML model the static relationships between classes in a system. They show classes, attributes, operations, and relationships like associations and generalizations. Associations describe links between classes, like a customer ordering products. Generalizations define inheritance between superclasses and subclasses, where the subclass inherits structure and behavior from the parent class.
This chapter discusses dynamic modeling techniques used to describe the behavior of object-oriented systems. It introduces sequence diagrams and collaboration diagrams, which show how objects interact and communicate through messages. Sequence diagrams focus on the time-ordering of messages while collaboration diagrams emphasize the relationships between objects. The chapter also covers different types of messages and events that can be depicted in dynamic models.
The document summarizes a chapter about use case modeling. It describes use cases and actors, relationships between use cases, and use case descriptions. Use case modeling involves identifying functional requirements, drawing diagrams showing actors and use cases, and writing use case descriptions in a conversational style to document interactions between actors and the system. Relationships like include, extend, and generalization are used to show how use cases relate to each other.
This chapter introduces state diagrams and their components. It discusses how state diagrams describe the states of an object and transitions between states triggered by events. It covers initial and final states, actions, activities, and different types of events. The chapter also discusses transitions between states and the use of guard conditions. Finally, it introduces concepts like substates, concurrent state diagrams, and ways for orthogonal components to communicate in concurrent state models.
This document discusses reading from and writing to files in Java programs. It explains how to open a file using a Scanner or PrintWriter object, read/write data using methods like next() and println(), and close the file when finished. It recommends storing file data in memory structures, processing it, then writing the results back to improve efficiency over sequential file access. Proper file handling and exception handling are also emphasized.
This document provides an overview and introduction to design patterns. It discusses creational, structural, and behavioral patterns. For each category it briefly introduces and describes the intent and use of some common patterns, including Factory Method, Singleton, Decorator, Facade, Command, Iterator, and Observer. It also includes diagrams to illustrate the relationships between objects in sample implementations.
The document discusses the decorator design pattern. The decorator pattern allows adding new behaviors to existing objects at runtime by placing them inside decorator objects that contain the original object. This allows functionality to be added without changing the object's class. Some key points made are: the decorator pattern adds functionality at runtime through composition; decorations are independent and can be mixed; and it is used to attach additional responsibilities to objects without subclassing. An example using shapes and color decorators is provided to demonstrate implementation.
The document discusses design patterns, which are reusable solutions to common software design problems. It provides examples of different types of patterns, including creational (Factory Method, Abstract Factory, Builder, Prototype, Singleton), structural (Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy), and behavioral (Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor) patterns. Each pattern is defined and an example problem and solution using the pattern is described.
The document introduces design patterns and their key elements. It discusses how design patterns help address common object-oriented design problems like determining appropriate objects, object granularity, specifying interfaces, and implementations. Key points made include: design patterns make reuse easier, express proven techniques, help choose reusable designs, and get a design "right" faster. Each pattern has a name, problem description, solution, and consequences. Patterns are organized by purpose (creational, structural, behavioral) and scope (class, object).
This document introduces design patterns and their key elements. Design patterns help designers solve common problems in object-oriented software design and make reusable designs more accessible. A pattern has four elements - a name, the problem it addresses, the solution, and consequences of applying the pattern. Patterns are organized based on their purpose (creational, structural, behavioral) and scope (object, class, etc.). The document outlines 23 classic design patterns organized in a catalog.
The document discusses the adapter design pattern, which allows classes with incompatible interfaces to work together. The adapter pattern involves a target interface that clients expect, an adaptee with a different interface that needs adapting, and an adapter class that converts the adaptee's interface to the target interface. The adapter acts as a bridge between the adaptee and target, allowing the adaptee's functionality to be accessed by clients through the target interface.
Design pattern is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.
Starting with that principles of design patterns and ending by giving you the chance to design you own pattern. - Don't lose your pattern.
The document discusses design patterns used in object-oriented programming. It describes common design patterns like factory method, singleton, observer, and adapter patterns. It also discusses principles for assigning responsibilities to objects like high cohesion and low coupling. The document provides examples of applying these patterns and principles to design software objects and systems.
Designing object-oriented software is hard, and designing reusable object oriented software is even harder.
A reusable design should be specific to the problem at hand but also general enough to address future problems and requirements.
When a good enough design is found it is reused again and again. At each use the solution becomes more flexible.
This gives the ability to use it next time. By time these design solutions are used to solve specific design problems and make the object oriented design more flexible, elegant, and ultimately reusable
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The Unified Process includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The Unified Process includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases.
The document summarizes and compares several major object-oriented programming languages: Smalltalk, C++, Eiffel, and Java. Smalltalk was designed for interactive use and has a fully integrated development environment, while C++ was designed for production use and has less integration. Eiffel focuses on ensuring code correctness but had poor tooling. Java became popular for web development due to its portability and is expected to further object-oriented adoption.
This document introduces multi-threading in Java. It discusses that threads allow a program to have multiple paths of execution. Threads can be created by extending the Thread class or implementing the Runnable interface. The key method for threads is run, which contains the logic to be executed. The document covers starting threads, potential problems with threads accessing shared resources, and techniques for thread synchronization like synchronized methods, waits and notifies.
This chapter introduces activity diagrams and discusses where they are used. Activity diagrams can model workflows, operations, and dynamic aspects of a system. They contain activities, transitions, synchronization bars, and decision activities. Activity diagrams are suitable for analyzing use cases, understanding workflow, and modeling multithreaded applications. They are not suitable for showing object collaboration or lifetime behavior.
This document introduces a module on programming concepts in C++. The module will cover object-oriented programming principles, C++ basics, classes, inheritance, pointers, and more. Students will learn to apply OOP to model real-world systems. Their knowledge will be assessed through a final exam and group project, both testing their ability to explain and apply core OOP concepts and develop class structures.
This document discusses abstract classes and interfaces in Java. It introduces abstract classes using a drawing package example, showing how abstract classes like Shape can be extended by classes like Rectangle and Circle. Interfaces are also introduced, explaining that they specify what an object can do without defining how. Classes can implement multiple interfaces to take on those required behaviors.
Class diagrams using UML model the static relationships between classes in a system. They show classes, attributes, operations, and relationships like associations and generalizations. Associations describe links between classes, like a customer ordering products. Generalizations define inheritance between superclasses and subclasses, where the subclass inherits structure and behavior from the parent class.
This chapter discusses dynamic modeling techniques used to describe the behavior of object-oriented systems. It introduces sequence diagrams and collaboration diagrams, which show how objects interact and communicate through messages. Sequence diagrams focus on the time-ordering of messages while collaboration diagrams emphasize the relationships between objects. The chapter also covers different types of messages and events that can be depicted in dynamic models.
The document summarizes a chapter about use case modeling. It describes use cases and actors, relationships between use cases, and use case descriptions. Use case modeling involves identifying functional requirements, drawing diagrams showing actors and use cases, and writing use case descriptions in a conversational style to document interactions between actors and the system. Relationships like include, extend, and generalization are used to show how use cases relate to each other.
This chapter introduces state diagrams and their components. It discusses how state diagrams describe the states of an object and transitions between states triggered by events. It covers initial and final states, actions, activities, and different types of events. The chapter also discusses transitions between states and the use of guard conditions. Finally, it introduces concepts like substates, concurrent state diagrams, and ways for orthogonal components to communicate in concurrent state models.
This document discusses reading from and writing to files in Java programs. It explains how to open a file using a Scanner or PrintWriter object, read/write data using methods like next() and println(), and close the file when finished. It recommends storing file data in memory structures, processing it, then writing the results back to improve efficiency over sequential file access. Proper file handling and exception handling are also emphasized.
This document provides an overview and introduction to design patterns. It discusses creational, structural, and behavioral patterns. For each category it briefly introduces and describes the intent and use of some common patterns, including Factory Method, Singleton, Decorator, Facade, Command, Iterator, and Observer. It also includes diagrams to illustrate the relationships between objects in sample implementations.
The document discusses the decorator design pattern. The decorator pattern allows adding new behaviors to existing objects at runtime by placing them inside decorator objects that contain the original object. This allows functionality to be added without changing the object's class. Some key points made are: the decorator pattern adds functionality at runtime through composition; decorations are independent and can be mixed; and it is used to attach additional responsibilities to objects without subclassing. An example using shapes and color decorators is provided to demonstrate implementation.
The document discusses design patterns, which are reusable solutions to common software design problems. It provides examples of different types of patterns, including creational (Factory Method, Abstract Factory, Builder, Prototype, Singleton), structural (Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy), and behavioral (Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor) patterns. Each pattern is defined and an example problem and solution using the pattern is described.
The document introduces design patterns and their key elements. It discusses how design patterns help address common object-oriented design problems like determining appropriate objects, object granularity, specifying interfaces, and implementations. Key points made include: design patterns make reuse easier, express proven techniques, help choose reusable designs, and get a design "right" faster. Each pattern has a name, problem description, solution, and consequences. Patterns are organized by purpose (creational, structural, behavioral) and scope (class, object).
This document introduces design patterns and their key elements. Design patterns help designers solve common problems in object-oriented software design and make reusable designs more accessible. A pattern has four elements - a name, the problem it addresses, the solution, and consequences of applying the pattern. Patterns are organized based on their purpose (creational, structural, behavioral) and scope (object, class, etc.). The document outlines 23 classic design patterns organized in a catalog.
The document discusses the adapter design pattern, which allows classes with incompatible interfaces to work together. The adapter pattern involves a target interface that clients expect, an adaptee with a different interface that needs adapting, and an adapter class that converts the adaptee's interface to the target interface. The adapter acts as a bridge between the adaptee and target, allowing the adaptee's functionality to be accessed by clients through the target interface.
Design pattern is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.
Starting with that principles of design patterns and ending by giving you the chance to design you own pattern. - Don't lose your pattern.
The document discusses design patterns used in object-oriented programming. It describes common design patterns like factory method, singleton, observer, and adapter patterns. It also discusses principles for assigning responsibilities to objects like high cohesion and low coupling. The document provides examples of applying these patterns and principles to design software objects and systems.
Designing object-oriented software is hard, and designing reusable object oriented software is even harder.
A reusable design should be specific to the problem at hand but also general enough to address future problems and requirements.
When a good enough design is found it is reused again and again. At each use the solution becomes more flexible.
This gives the ability to use it next time. By time these design solutions are used to solve specific design problems and make the object oriented design more flexible, elegant, and ultimately reusable
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The Unified Process includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The Unified Process includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The document then discusses OO concepts like encapsulation, abstraction, inheritance, and polymorphism and how classes and objects are used in object-oriented programming. It provides an overview of the course structure and evaluation criteria.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The Unified Process includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The unified process model includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases. Main OO concepts are encapsulation, abstraction, inheritance, and polymorphism.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The Unified Process includes inception, elaboration, construction, and transition phases with iterations and milestones leading to final product releases.
Object-oriented analysis and design (OOAD) emphasizes investigating requirements rather than solutions, and conceptual solutions that fulfill requirements rather than implementations. OOAD focuses on identifying domain concepts and defining software objects and how they collaborate. The document then discusses OO concepts like encapsulation, abstraction, inheritance, and polymorphism and how classes and objects are used in object-oriented programming. It provides an overview of the course structure and evaluation criteria.
Full description of design pattern. Mostly focused on structural design patterns and there types. In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code.
The document discusses the adapter pattern, which allows classes with incompatible interfaces to work together by providing a standard interface. An example is provided of using an adapter class to allow an icon class to be used as a GUI component, even though the icon class does not implement the required component interface. The key aspects of the adapter pattern are explained, including that it converts one interface into another so incompatible classes can work together through the adapter.
The document discusses key aspects of the software design process including that design is iterative and represented at a high level of abstraction, guidelines for quality design include modularity, information hiding, and functional independence, and architectural design defines system context and archetypes that compose the system.
The document summarizes and compares several major object-oriented programming languages: Smalltalk, C++, Eiffel, and Java. Smalltalk was designed for interactive use and has a fully integrated development environment, while C++ was designed for production use and has less integration. Eiffel focuses on ensuring code correctness but has not been widely adopted. Java became very popular and may become the main language for web programming due to its portability.
1. Structural Patterns
The Decorator Pattern
Design Patterns
Chapter 8C
Dec 21, 2012 Design Patterns Chapter 8C 1
2. Objectives
In this lecture, we will
• Introduce the Decorator pattern
• Discuss examples that take advantage of the Decorator
pattern
• Compare the decorator pattern with some other structural
patterns
Dec 21, 2012 Design Patterns Chapter 8C 2
3. Structural Patterns
• Structural Patterns are used when it is necessary to build
larger structures that are composed of other existing
classes and objects
– Re-use of existing implementations
• Structural class patterns use inheritance
• Structural object patterns use aggregation
• This lecture introduces the decorator pattern
– Classified as object structural
Dec 21, 2012 Design Patterns Chapter 8C 3
4. Introduction to the Decorator Pattern
• The decorator pattern allows additional functionality to be
attached to an object dynamically
– A collection of books exist in a library
– How do you make some of the books available for loan?
• A graphical user interface toolkit is the most common
example of a motivation for using the decorator pattern
• Consider a user interface component such as view of a
document
– A page of text
• The view can be enhanced by adding scrolling or borders
– How can these be added?
Dec 21, 2012 Design Patterns Chapter 8C 4
5. Adding to a View
• Decorating a view
This is some text
that goes on and
on and on and on
and on and on and
on and on and on
and on and on and
Dec 21, 2012 Design Patterns Chapter 8C 5
6. Inheritance
• One way to add to a class is through inheritance
• A view with a border IS A view
• A view with a vertical scrollbar IS A view
• What does the class hierarchy for view look like?
Dec 21, 2012 Design Patterns Chapter 8C 6
7. Inheritance -Disadvantages
• Consider the classes on the previous slide
– There is a little bit of a class explosion
• Suppose another new type of enhancement to a view,
such as a 3D border, is required
– How many new classes are added to the hierarchy?
• A view is just one example of a GUI component
• There is a good chance that these enhancements may be
needed for other components
– How many more classes are needed to add borders to other
components?
Dec 21, 2012 Design Patterns Chapter 8C 7
8. Inheritance -Disadvantages
• When is it necessary to decide that a component has a
border?
• The choice has to be made statically
– A specific class has to be instantiated to achieve the desired
behaviour
– For example it is not possible to start with a view and then
add scrolling and then add a border
• A client cannot control how and when these decorations
are added to the component
• In this scenario the decorator pattern provides a more
flexible approach than inheritance
Dec 21, 2012 Design Patterns Chapter 8C 8
10. The Decorator Pattern
• Intent
– Attach additional responsibilities to an object dynamically
– The decorator pattern provides a flexible alternative to
inheritance for extending functionality
• Applicability
– The decorator pattern is used:
• To add responsibilities to individual objects dynamically and
transparently without affecting other objects
• For responsibilities that can be withdrawn
• When sub classing results in a class explosion
• When a class definition is hidden or otherwise unavailable for
subclassing
Dec 21, 2012 Design Patterns Chapter 8C 10
11. The Decorator Pattern
• Participants
• Component
– Defines the interface for objects that need to have
responsibilities added to them dynamically
• ConcreteComponent
– Defines an object that implements the component interface
and possibly adds additional responsibilities
• Decorator
– Defines an interface that conforms to the component
interface
– Maintains a reference to a component object
• ConcreteDecorator
– Adds respnsibilities to the component
Dec 21, 2012 Design Patterns Chapter 8C 11
13. Consequences
• The decorator pattern has at least two key advantages and two
liabilities:
• More flexible than static inheritance
– Responsibilities can be added and removed at run time
– The decorator pattern allows a property to be added more than
once; for example adding two borders (difficult to achieve through
inheritance)
• Avoids classes with excessive numbers of features high up in
the class hierarchy
– A pay as you go approach to adding responsibilities
• A decorator and its component are not identical
– Transparent enclosure but a decorated component is not identical
to the component itself
• Decorator pattern often results in lots of little objects that look
alike
Dec 21, 2012 Design Patterns Chapter 8C 13
14. The Library Example
• Consider building a library application
• Initially it is decided that all the library items are for
reference only
– That is they cannot be borrowed
• Once the system is up and running it is decided that some
of the library items can be borrowed
• How would you apply the decorator pattern in this
scenario?
Dec 21, 2012 Design Patterns Chapter 8C 14
15. Related Patterns
• A decorator is different from an adapter in that a decorator
only changes the responsibilities of an object not the
interface
– An adapter gives an object a new interface
• A decorator can be viewed as a degenerate composite
with only one component
– BUT a decorator adds responsibilities; it is not intended for
object aggregation
Dec 21, 2012 Design Patterns Chapter 8C 15
16. Summary
In this lecture we have:
• Introduced the Decorator pattern
• Discussed examples that take advantage of the Decorator
pattern
• Compared the decorator pattern with some other
structural patterns
Dec 21, 2012 Design Patterns Chapter 8C 16
Editor's Notes
Part of this lecture will be reserved for working through solutions to selected exercises from last week. Notes relating to this do not appear in the slides.