Download presentation
Presentation is loading. Please wait.
Published byBarnaby Booth Modified over 9 years ago
1
Design Patterns Yonglei Tao
2
Design Patterns A design pattern describes a recurring design problem, a solution, and the context in which that solution works A common way of doing things Specialization of a design principle Tested knowledge A named description of a technique with its benefits and costs
3
The Strategy Pattern Problem how to design for varying, but related, algorithms or policies how to design for the ability to change them Solution define a family of algorithms, encapsulate each one in a separate class, and make them interchangeable
4
A Sorting Example Shellsort No extra space needed, fast but not optimal Very fast on nearly sorted data Does comparatively well on small lists Quicksort Average case is O (n log n) Relatively poor performance on short lists Require a stack of log(n) in depth Mergesort Worst case is O (n log n) Requires O (n) extra space Stable
5
Sorting (cont.) Have a sorted list container, which one gives a sort algorithm SortedList studentRecords = new SortedList( new ShellSort() ); studentRecords.add( “Sam” ); … pubic class SortedList { Object[ ] elements; SortStrategy sorter; void sort( ) { sorter.sort( elements); } }
7
More on Strategy t = getDiscount(s)
8
The Factory Pattern Problem a class cannot anticipate the class of objects it must create how to handle object creation with complex creation logic or other special considerations Solution encapsulate the knowledge of object creation and move it out of the class that uses the object define an interface for creating an object, but let subclasses decide which class to instantiate
9
Factory Method
10
The Singleton Pattern Problem objects need a global and single point of access Solution ensure a class has exactly one instance define a static method that returns the singleton Consequences facilitates communication between objects not directly coupled reduce coupling between objects in a system
11
// get a reference to the singleton object theSingleton = Singleton.getInstance(); The Singleton Pattern
12
Implementation class Singleton { private static Singleton _instance = null; private Singleton() { … } public static Singleton getInstance() { if ( _instance == null ) _instance = new Singleton(); return _instance; } public void otherOperations() { … } } class Program { public void aMethod() { X = Singleton.getInstance(); }
13
Factory
15
Navigational Model for Visual C++
16
Example: the Null Node in Binary Trees
17
public class BinaryNode extends Node { Node left, right; int key; public boolean includes( int value ) { if (key == value) return true; else if (value < key ) return left.includes( value ); else return right.includes(value); } … } public class NullNode extends Node { public boolean includes( int value ) { return false; } … }
18
The Composite Pattern Problem How to allow clients to treat individual objects and compositions of objects uniformly Solution Organize objects into a tree structure that represents an aggregation hierarchy
19
Structure
20
Use of Composite
21
More on Composite
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.