21
CODE LIKE A NINJA STRUCTURAL DESIGN PATTERNS

Code Like a Ninja Session 8 - Structural Design Patterns

Embed Size (px)

DESCRIPTION

presentation

Citation preview

Page 1: Code Like a Ninja Session 8 - Structural Design Patterns

CODE LIKE A NINJASTRUCTURAL DESIGN PATTERNS

Page 2: Code Like a Ninja Session 8 - 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=rss

• Local Network: \\dmeyer-m\share\training\Code Like a Ninja

• Source was compiled using Visual Studio 2012

• http://www.gofpatterns.com/

Page 3: Code Like a Ninja Session 8 - Structural Design Patterns

Design Patterns

Creational Design PatternsStructural Design PatternsBehavioral Design Patterns

Page 4: Code Like a Ninja Session 8 - Structural 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.

Page 5: Code Like a Ninja Session 8 - Structural Design Patterns

• Adapter

• Bridge

• Composite

• Decorator

• Façade

• Flyweight

• Proxy

STRUCTURAL DESIGN PATTERNS

Page 6: Code Like a Ninja Session 8 - 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

Page 7: Code Like a Ninja Session 8 - Structural Design Patterns

ADAPTER PATTERN

Page 8: Code Like a Ninja Session 8 - Structural Design Patterns

ADAPTER PATTERN – BENEFITS

• Allows two or more incompatible objects to communicate and interact

• Improves reusability of older functionality

Page 9: Code Like a Ninja Session 8 - Structural Design Patterns

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.

Page 10: Code Like a Ninja Session 8 - Structural Design Patterns

ADAPTER PATTERN

See code example

Page 11: Code Like a Ninja Session 8 - Structural Design Patterns

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.

Page 12: Code Like a Ninja Session 8 - Structural Design Patterns

COMPOSITE PATTERN

Page 13: Code Like a Ninja Session 8 - Structural Design Patterns

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

Page 14: Code Like a Ninja Session 8 - Structural Design Patterns

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.

Page 15: Code Like a Ninja Session 8 - Structural Design Patterns

COMPOSITE PATTERN

See code example

Page 16: Code Like a Ninja Session 8 - Structural Design Patterns

DECORATOR PATTERN

• Enables you to add or remove functionality without changing the external appearance or function of the object

Page 17: Code Like a Ninja Session 8 - Structural Design Patterns

DECORATOR PATTERN

Page 18: Code Like a Ninja Session 8 - Structural Design Patterns

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.

Page 19: Code Like a Ninja Session 8 - Structural Design Patterns

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.

Page 20: Code Like a Ninja Session 8 - Structural Design Patterns

DECORATOR PATTERN

See code example

Page 21: Code Like a Ninja Session 8 - 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=rss

• Local Network: \\dmeyer-m\share\training\Code Like a Ninja

• Source was compiled using Visual Studio 2012

• http://www.gofpatterns.com/