CODE LIKE A NINJA
STRUCTURAL DESIGN PATTERNS
SESSION RESOURCES
• Presentation session notes including link to this session, will be
available on http://learningaboutfudge.blogspot.com
• All the source for this session is publically available at:
https://github.com/SheepWorx/Training
• RSS Feed:
http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rs
s
• Local Network: dmeyer-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
• http://www.gofpatterns.com/
Design Patterns
Creational Design Patterns
Structural Design Patterns
Behavioral Design Patterns
STRUCTURAL DESIGN PATTERNS
Definition
Structural design patterns are design patterns that ease the
design by identifying a simple way to realize relationships
between entities. Structural patterns are for tying together
existing function.
• Adapter
• Bridge
• Composite
• Decorator
• Façade
• Flyweight
• Proxy
STRUCTURAL DESIGN PATTERNS
ADAPTER PATTERN
• Acts as an intermediary between two classes
• Enables incompatible interfaces to work together
• Implements an interface known to its clients and provides
access to an instance of class not known to its clients
ADAPTER PATTERN
ADAPTER PATTERN – BENEFITS
• Allows two or more incompatible objects to communicate and
interact
• Improves reusability of older functionality
ADAPTER PATTERN – WHEN TO USE IT
• You want to use an existing class, and its interface does not match
the interface you need.
• You want to create a reusable class that cooperates with unrelated or
unforeseen classes, that is, classes that do not necessarily have
compatible interfaces.
• You want to use an object in an environment that expects an
interface that is different from the object's interface.
• Interface translation among multiple sources must occur.
ADAPTER PATTERN
See code example
COMPOSITE PATTERN
• Enables you to create hierarchical tree of varying complexity
Motivation
When dealing with Tree-structured data, programmers often
have to discriminate between a leaf-node and a branch. The
solution is an interface that allows treating complex and
primitive objects uniformly.
COMPOSITE PATTERN
COMPOSITE PATTERN – BENEFITS
• Defines class hierarchies consisting of primitive objects and
composite objects
• Makes it easier to add new kinds of components
• Provides flexibility of structure and a manageable interface
COMPOSITE PATTERN – WHEN TO USE IT
• You want to represent the whole hierarchy or part of the
hierarchy of objects.
• You want clients to be able to ignore the difference between
compositions of objects and individual objects.
• The structure can have any level of complexity, and is dynamic.
COMPOSITE PATTERN
See code example
DECORATOR PATTERN
• Enables you to add or remove functionality without changing
the external appearance or function of the object
DECORATOR PATTERN
DECORATOR PATTERN – BENEFITS
• More flexibility than static inheritance.
• Avoids feature-laden classes high up in the hierarchy
• Simplifies coding because you write a series of classes, each
targeted at a specific part of the functionality, rather than
coding all behaviour into the object
• Enhances the object's extensibility because you make changes
by coding new classes.
DECORATOR PATTERN – WHEN TO USE IT
• You want to add responsibilities to individual objects
dynamically and transparently, that is without affecting other
objects
• You want to add responsibilities to the object that you want to
change in the future
• When extension by static sub-classing is impractical.
DECORATOR PATTERN
See code example
SESSION RESOURCES
• Presentation session notes including link to this session, will be
available on http://learningaboutfudge.blogspot.com
• All the source for this session is publically available at:
https://github.com/SheepWorx/Training
• RSS Feed:
http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rs
s
• Local Network: dmeyer-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
• http://www.gofpatterns.com/

Code Like a Ninja Session 8 - Structural Design Patterns

  • 1.
    CODE LIKE ANINJA STRUCTURAL DESIGN PATTERNS
  • 2.
    SESSION RESOURCES • Presentationsession notes including link to this session, will be available on http://learningaboutfudge.blogspot.com • All the source for this session is publically available at: https://github.com/SheepWorx/Training • RSS Feed: http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rs s • Local Network: dmeyer-msharetrainingCode Like a Ninja • Source was compiled using Visual Studio 2012 • http://www.gofpatterns.com/
  • 3.
    Design Patterns Creational DesignPatterns Structural Design Patterns Behavioral Design Patterns
  • 4.
    STRUCTURAL DESIGN PATTERNS Definition Structuraldesign patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities. Structural patterns are for tying together existing function.
  • 5.
    • Adapter • Bridge •Composite • Decorator • Façade • Flyweight • Proxy STRUCTURAL DESIGN PATTERNS
  • 6.
    ADAPTER PATTERN • Actsas an intermediary between two classes • Enables incompatible interfaces to work together • Implements an interface known to its clients and provides access to an instance of class not known to its clients
  • 7.
  • 8.
    ADAPTER PATTERN –BENEFITS • Allows two or more incompatible objects to communicate and interact • Improves reusability of older functionality
  • 9.
    ADAPTER PATTERN –WHEN TO USE IT • You want to use an existing class, and its interface does not match the interface you need. • You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that do not necessarily have compatible interfaces. • You want to use an object in an environment that expects an interface that is different from the object's interface. • Interface translation among multiple sources must occur.
  • 10.
  • 11.
    COMPOSITE PATTERN • Enablesyou to create hierarchical tree of varying complexity Motivation When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. The solution is an interface that allows treating complex and primitive objects uniformly.
  • 12.
  • 13.
    COMPOSITE PATTERN –BENEFITS • Defines class hierarchies consisting of primitive objects and composite objects • Makes it easier to add new kinds of components • Provides flexibility of structure and a manageable interface
  • 14.
    COMPOSITE PATTERN –WHEN TO USE IT • You want to represent the whole hierarchy or part of the hierarchy of objects. • You want clients to be able to ignore the difference between compositions of objects and individual objects. • The structure can have any level of complexity, and is dynamic.
  • 15.
  • 16.
    DECORATOR PATTERN • Enablesyou to add or remove functionality without changing the external appearance or function of the object
  • 17.
  • 18.
    DECORATOR PATTERN –BENEFITS • More flexibility than static inheritance. • Avoids feature-laden classes high up in the hierarchy • Simplifies coding because you write a series of classes, each targeted at a specific part of the functionality, rather than coding all behaviour into the object • Enhances the object's extensibility because you make changes by coding new classes.
  • 19.
    DECORATOR PATTERN –WHEN TO USE IT • You want to add responsibilities to individual objects dynamically and transparently, that is without affecting other objects • You want to add responsibilities to the object that you want to change in the future • When extension by static sub-classing is impractical.
  • 20.
  • 21.
    SESSION RESOURCES • Presentationsession notes including link to this session, will be available on http://learningaboutfudge.blogspot.com • All the source for this session is publically available at: https://github.com/SheepWorx/Training • RSS Feed: http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rs s • Local Network: dmeyer-msharetrainingCode Like a Ninja • Source was compiled using Visual Studio 2012 • http://www.gofpatterns.com/