Download presentation
Presentation is loading. Please wait.
1
Chapter Nine The Strategy Pattern
Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
2
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
3
Overview A new case study The problem of new requirements e-commerce
A solution using the Strategy pattern The problem of new requirements Approaches to handling new variations Ku-Yaw Chang The Strategy Pattern
4
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
5
An Approach to Handling New Requirements
Disaster often comes in the long run from suboptimal decisions in the short run. Desktop filing system Software development are concerned With handling immediate, pressing needs Without future maintenance Management is under pressured to deliver, not to maintain Ku-Yaw Chang The Strategy Pattern
6
An Approach to Handling New Requirements
Design for change Program to an interface, not an implementation Favor object [aggregation] over class inheritance Consider what should be variable in your design. The opposite of focusing on the cause of redesign Encapsulate the concept that varies Ku-Yaw Chang The Strategy Pattern
7
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
8
International E-Commerce System
An order-processing system for an international e-commerce company in the U.S. To process sales orders in many different countries General architecture A controller object Handles sales requests A SalesOrder To process the order Ku-Yaw Chang The Strategy Pattern
9
International E-Commerce System
Functions of SalesOrder Allow for filling out the order with a GUI Handle tax calculations Process the order, and print a sales receipt Be implemented with the help of other objects SalesTicket object that prints the SalesOrder Ku-Yaw Chang The Strategy Pattern
10
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
11
Handling New Requirements
Handle taxes on orders from customers outside the U.S. Available approaches Copy and paste Switches or ifs on a variable Use function pointers or delegates Inheritance Delegate the entire functionality to a new object Ku-Yaw Chang The Strategy Pattern
12
Handling New Requirements
Switches Ku-Yaw Chang The Strategy Pattern
13
Handling New Requirements
Switches Ku-Yaw Chang The Strategy Pattern
14
Handling New Requirements
Switches Ku-Yaw Chang The Strategy Pattern
15
Handling New Requirements
Inheritance Handle Germany or get other things that are varying Tall inheritance hierarchies result from specialization techniques Ku-Yaw Chang The Strategy Pattern
16
Handling New Requirements
Design patterns take a different approach Consider what should be variable in your design Encapsulate the concept that varies Favor object-aggregation over class inheritance Do the following Find what varies and encapsulate it in a class of its own Contain this class in another class Ku-Yaw Chang The Strategy Pattern
17
Handling New Requirements
Step 1 Find what varies and encapsulate it A CalcTax object defines the interface Ku-Yaw Chang The Strategy Pattern
18
Handling New Requirements
Step 2 Favor aggregation One SalesOrder class contains the CalcTax class to handle the variations Ku-Yaw Chang The Strategy Pattern
19
Handling New Requirements
Ku-Yaw Chang The Strategy Pattern
20
Handling New Requirements
Advantages Better cohesion More flexible Easier to shift responsibilities Ku-Yaw Chang The Strategy Pattern
21
Handling New Requirements
Ku-Yaw Chang The Strategy Pattern
22
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
23
The Strategy Pattern Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Ku-Yaw Chang The Strategy Pattern
24
The Strategy Pattern Principles Objects have responsibilities.
Different, specific implementations of these responsibilities are manifested through the use of polymorphism. There is a need to manage several different implementations of what is, conceptually, the same algorithm. Ku-Yaw Chang The Strategy Pattern
25
Generic Structure Ku-Yaw Chang The Strategy Pattern
26
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
27
Using the Strategy Pattern
Encapsulating business rules More than algorithms Coupling between context and strategies Required information Passed to strategies Lowers cost of unit tests Ku-Yaw Chang The Strategy Pattern
28
Outline Overview An Approach to Handling New Requirements
The International E-Commerce System Case Study: Initial Requirements Handling New Requirements The Strategy Pattern Field Notes: Using the Strategy Pattern Summary Ku-Yaw Chang The Strategy Pattern
29
Summary A way to define a family of algorithms Do the same things
Have different implementations Ku-Yaw Chang The Strategy Pattern
30
The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.