decorator pattern flyweight pattern
DESCRIPTION
Matt Klein. Decorator Pattern flyweight Pattern. Decorator Pattern. Intent Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. AKA Wrapper. Decorator: Motivation. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/1.jpg)
DECORATOR PATTERNFLYWEIGHT PATTERN
Matt Klein
![Page 2: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/2.jpg)
Decorator Pattern
Intent Attach Additional responsibilities to an
object by dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
AKA Wrapper
![Page 3: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/3.jpg)
Decorator: Motivation
Desire to add responsibilities to individual objects
Two Approaches Inheritance
Choices are made statically Inflexible
Decorator Dynamic Unlimited in number of added
responsibilities
![Page 4: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/4.jpg)
Decorator: Applicability
Use the Decorator Pattern… To add responsibilities to individual
objects dynamically and transparently For responsibilities that can be
withdrawn When extension by subclassing is
impractical
![Page 5: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/5.jpg)
Decorator: Participants
Component Defines the interface for objects that can
have responsibilities added to them dynamically
ConcreteComponent Defines an object to which additional
responsibilities can be attached
![Page 6: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/6.jpg)
Decorator: Participants
Decorator Maintains a reference to a Component
object and defines an interface that conforms to Component’s interface
ConcreteDecorator Adds responsibilities to the component
![Page 7: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/7.jpg)
Decorator: Structure
![Page 8: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/8.jpg)
Decorator: Consequences
Good More Flexibility than static inheritance
Much easier to use than multiple inheritance
Can be used to mix and match features Can add the same property twice
Avoids feature laden classes high up in hierarchy Allows to easily add new features
incrementally
![Page 9: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/9.jpg)
Decorator: Consequences
Bad A decorator and its component aren’t
identical From an object identity point of view, a
decorated component is not identical to the component itself
Don’t rely on object identity when using decorators
Lots of little objects Often end up with systems composed of
lots of little objects Can be hard to learn and debug
![Page 10: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/10.jpg)
Decorator: Implementation
Interface conformance Decorator object’s interface must
conform to the interface of the component it decorates
Omitting the abstract Decorator class If you’re adding just one responsibility,
there’s no need to have the abstract Decorator class
![Page 11: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/11.jpg)
Decorator: Implementation
Keeping Component classes lightweight Component should stick to defining an
interface Changing the skin of an object vs.
changing the guts Strategy vs. Decorator
![Page 12: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/12.jpg)
Decorator: Related Patterns Adapter
Provides new interface vs. changing responsibilities
Composite Can be viewed as a degenerate
composite with only one component Strategy
Skin vs. Guts
![Page 13: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/13.jpg)
Flyweight Pattern
Intent Use sharing to support large numbers of
fine-grained objects efficiently
![Page 14: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/14.jpg)
Flyweight: Motivation
Can be used when an application could benefit from using objects throughout their design, but a naïve implementation would be prohibitively expensive Objects for each character in a
document editor Cost is too great!
Can use flyweight to share characters
![Page 15: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/15.jpg)
Intrinsic vs. Extrinsic
Intrinsic state is stored in the flyweight Information that’s independent of the
flyweight’s context Shared
Extrinsic State depends on and varies with the
flyweight’s context Cannot be shared
![Page 16: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/16.jpg)
Flyweight: Applicability
Use the Flyweight pattern when ALL of the following are true An application uses a large number of
objects Storage costs are high because of the
sheer quantity of objects Most object state can be made extrinsic Many Groups of objects may be replaced
by relatively few shared objects once extrinsic state is removed
The application doesn’t depend on object identity
![Page 17: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/17.jpg)
Flyweight: Participants
Flyweight Declares an interface through which
flyweights can receive and act on extrinsic state
ConcreteFlyweight Implements the Flyweight interface and
adds storage for intrinsic state, if any Must be shareable
![Page 18: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/18.jpg)
Flyweight: Participants
FlyweightFactory Creates and manages flyweight objects Ensures that flyweights are shared
properly Client
Maintains reference to flyweights Computes or stores the extrinsic state of
flyweights
![Page 19: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/19.jpg)
Flyweight: Structure
![Page 20: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/20.jpg)
Flyweight: Consequences
May introduce run-time costs associated with transferring, finding, and/or computing extrinsic state
Costs are offset by space savings
![Page 21: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/21.jpg)
Flyweight: Consequences
Storage savings are a function of the following factors: The reduction in the total number of
instances that comes from sharing The amount of intrinsic state per object Whether extrinsic state is computed or
stored
![Page 22: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/22.jpg)
Flyweight: Consequences
Ideal situation High number of shared flyweights Objects use substantial quantities of
both intrinsic and extrinsic state Extrinsic state is computed
![Page 23: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/23.jpg)
Decorator: Implementation
Removing extrinsic state Success of pattern depends on ability to
remove extrinsic state from shared objects
No help if there are many different kinds of extrinsic state
Ideally, state is computed separately
![Page 24: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/24.jpg)
Flyweight: Implementation
Managing shared objects Objects are shared so clients should not
instantiate FlyweightFactory is used to create and
share objects Garbage collection may not be
necessary
![Page 25: Decorator Pattern flyweight Pattern](https://reader034.vdocuments.mx/reader034/viewer/2022050800/568166ed550346895ddb3f68/html5/thumbnails/25.jpg)
Flyweight: Related Patterns Composite
Often combined with flyweight Provides a logically hierarchical structure
in terms of a directed-acyclic graph with shared leaf nodes
State and Strategy Best implemented as flyweights