Introduction to Design Patterns as a Best Practice
-
Upload
kevpthomas -
Category
Documents
-
view
217 -
download
0
Transcript of Introduction to Design Patterns as a Best Practice
-
8/14/2019 Introduction to Design Patterns as a Best Practice
1/33
Mark Godfrey
Introduction to Design Patterns as a Best Practice
Kevin Thomas - 8/11/2009
-
8/14/2019 Introduction to Design Patterns as a Best Practice
2/33
Objectives
Introduce the concept of design patterns as
originally defined by the Gang of Four
Define and explain several commonly-used GoFpatterns
Provide a working .NET solution (code samples)
demonstrating these patterns in a somewhat
real-world situation Lay the groundwork for exploring Test-Driven
Development, which requires the use of design
patterns to be successful
-
8/14/2019 Introduction to Design Patterns as a Best Practice
3/33
Agenda
Provide a somewhat real-world set of user requirements
Define terms Best Practice
Design Pattern Briefly discuss a possible ugly code solution
Explore several design patterns Pattern definition
Example using metaphor
UML Explain how the design patterns can be used in a
solution
Code samples from a working Visual Studio 2008 .NETsolution
-
8/14/2019 Introduction to Design Patterns as a Best Practice
4/33
User Requirements
Assignment:
2 users. Both want:
Extract files from an archive
Validate the archive before extraction
Report the results back to the UI
User #1 wants:
Windows (console)
application Only Zip files for now
1 MB archive file size limit
User #2 wants:
Web application
Zip or Tar files, maybeothers
20 KB archive file size limit
Archive must contain a
Readme.txt file
-
8/14/2019 Introduction to Design Patterns as a Best Practice
5/33
Solution Overview
This talk will demonstrate the best practice of
using design patterns to solve this problem and
meet all of the user requirements for both users.
The result will be easily maintained, flexible,
reusable, and testable code.
Sample code: C#.NET using Visual Studio 2008
-
8/14/2019 Introduction to Design Patterns as a Best Practice
6/33
Definition: Best Practice
A Best Practice is a solution
that when compared to other
solutions for the same
problem offers improved
ease-of-implementation,
maintainability, performance,
and/or security
-
8/14/2019 Introduction to Design Patterns as a Best Practice
7/33
Definition: Design Pattern
A Design Pattern describes a general solution to
a design problem that recurs repeatedly.
1. Pattern Name labels a
common solution2. Problem
What the pattern solves
When to apply the pattern
3. Solution
Elements that comprise thedesign
A template, not copy and paste
code
4. Consequences the results
and trade-offs of applying the
pattern
4 fundamental components to any Design Pattern:
-
8/14/2019 Introduction to Design Patterns as a Best Practice
8/33
-
8/14/2019 Introduction to Design Patterns as a Best Practice
9/33
Possible Solution - Ugly Code
Windows App:
1. Get archive file
2. Validation
1. Check archive file extension(code block)
2. Check archive file size (codeblock)
3. Extraction
1. Unzip the archive file
4. Display the results
Web App:
1. Get archive file
2. Validation
1. Check archive file extension(copy/paste from Windows App)
2. Check archive file size(copy/paste from Windows App)
3. Check archive contents
3. Extraction
1. If .zip
(copy/paste from Windows App)2. Else
Extract from the Tar file
4. Display the results
You might be tempted to do something like this:
-
8/14/2019 Introduction to Design Patterns as a Best Practice
10/33
Issues - Ugly Code
This ugly code solution is hard to maintain, noteasily reused, and difficult to test
Lots of copying and pasting code from one
solution to the other Difficult to modify if add a new validation, drop a
validation, or need to change the order Add new if/then/else blocks
Reorder chunks of code Not easily tested
Big chunks of code
Difficult to run code in test environment
-
8/14/2019 Introduction to Design Patterns as a Best Practice
11/33
Design Patterns Explained
Following slides will define several Gang of Four
design patterns
For each DesignPattern, will provide:
Definition
Metaphoricalexample
UML
-
8/14/2019 Introduction to Design Patterns as a Best Practice
12/33
Adapter Pattern - Definition
Adapter Pattern
Convert the interface of a class into
another interface clients expect.Adapter lets classes work together
that couldn't otherwise because of
incompatible interfaces.
-
8/14/2019 Introduction to Design Patterns as a Best Practice
13/33
Adapter Pattern - Example (appliance)
USA Appliance
Intended
consumer code
framework(120 V)
Class I want to
use
Class is designed to work with a particular framework
My framework
(240 V)
Make the assembly work with my code using an adapter
USA Appliance
Add a custom
Adapter
-
8/14/2019 Introduction to Design Patterns as a Best Practice
14/33
Adapter Pattern - UML
-
8/14/2019 Introduction to Design Patterns as a Best Practice
15/33
-
8/14/2019 Introduction to Design Patterns as a Best Practice
16/33
Strategy Pattern - Example (game)
Strategies can be plugged in to existing code - the
existing code doesnt care which strategy is used
Go to building
Open
Door
Enter building
Turn knob
Kick down door
Battering ram
Grenade
Door-opening Strategies
Strategy
interface
-
8/14/2019 Introduction to Design Patterns as a Best Practice
17/33
Strategy Pattern - UML
-
8/14/2019 Introduction to Design Patterns as a Best Practice
18/33
Decorator Pattern - Definition
Decorator Pattern
Attach additional responsibilities to an
object dynamically. Decoratorsprovide a flexible alternative to
subclassing for extending
functionality.
-
8/14/2019 Introduction to Design Patterns as a Best Practice
19/33
Strategy Pattern - Example (budget)
Decorators are added for each employee depending on the
employees title, job description, etc. Call code doesnt care which
decorator(s), if any, have been applied
CalculateRent
Calculate
Salary
Calculate
Travel
total += rent
for each employee e in employees
total += e.GetPay()
total += travel
StaffOvertime (decorator)
GetPay():
Return component.GetPay() + overtime
StaffPension (decorator)
GetPay():
Return component.GetPay() - pension
Employee
GetPay(): Return hours * rate
-
8/14/2019 Introduction to Design Patterns as a Best Practice
20/33
Decorator Pattern - UML
-
8/14/2019 Introduction to Design Patterns as a Best Practice
21/33
Faade Pattern - Definition
Faade Pattern
Provide a unified interface to a set of
interfaces in a subsystem withouthiding the subsystem. Faade defines
a higher-level interface that makes
the subsystem easier to use.
-
8/14/2019 Introduction to Design Patterns as a Best Practice
22/33
Faade Pattern - Example (hardware store)
Faade provides an easy-to-use interface to a library and
provides the most common features
Get dog
Put dog outside
Hardware Store
Get dog house
Preassembled dog house
easy to use
most common features
Custom dog house
harder to use - must determine
correct tools, sequence, etc
can customize features
-
8/14/2019 Introduction to Design Patterns as a Best Practice
23/33
Faade Pattern - UML
-
8/14/2019 Introduction to Design Patterns as a Best Practice
24/33
Other Patterns
Template pattern
Define the skeleton of an algorithm in an operation,deferring some steps to subclasses. Template Methodlets subclasses redefine certain steps of an algorithmwithout changing the algorithm's structure.
Singleton pattern
Ensure a class has only one instance and provide a
global point of access to it.
http://www.dofactory.com/Patterns/Patterns.aspx for moredetails
-
8/14/2019 Introduction to Design Patterns as a Best Practice
25/33
Revisit the User Requirements
Extraction
Extract from ZIP
Extract from TAR
Extract from other
Suggests Strategy pattern
User Interface
Need Console and Web
UI
Want to reuse code Want to make it easy for
client to use
Suggests Faade pattern to
minimize code in UI andmake easy to use
Also suggests Adapter
pattern because different
UIs will supply methods
Validation
Validate size and/orcontent and/or extension
and/or other
Suggests Decoratorpattern
-
8/14/2019 Introduction to Design Patterns as a Best Practice
26/33
Citations
Data & Object Factory, LLC . Design Patterns.
Dofactory.com. 2009.
Fowler, Martin. Patterns of Enterprise Application
Architecture. Massachusetts: Addison-Wesley,
2002.
Gamma, Helm, Johnson, and Vlissides. Elements of
Reusable Object-Oriented Software. Massachusetts:
Addison-Wesley, 1995. (Gang of Four)
-
8/14/2019 Introduction to Design Patterns as a Best Practice
27/33
UI CODE
COMPARISON
-
8/14/2019 Introduction to Design Patterns as a Best Practice
28/33
UI Code Comparison
Web Application Console Application
-
8/14/2019 Introduction to Design Patterns as a Best Practice
29/33
UI Code Comparison
Web Application Console Application
Setup is similar:
name upload file
save/copy upload file
instantiate a directory manager
-
8/14/2019 Introduction to Design Patterns as a Best Practice
30/33
UI Code Comparison
Web Application Console Application
Different extract strategies:
Web = zip, tar
Console = zip
-
8/14/2019 Introduction to Design Patterns as a Best Practice
31/33
UI Code Comparison
Web Application Console Application
Different validation decorators: Web = extension + size + content
Console = extension + size
-
8/14/2019 Introduction to Design Patterns as a Best Practice
32/33
UI Code Comparison
Web Application Console Application
Both use the faade to get
a status message for display:
-
8/14/2019 Introduction to Design Patterns as a Best Practice
33/33
CODE SAMPLES
VS 2008