Chapter Nine The Strategy Pattern Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
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
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
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
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
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
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
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
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
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
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
Handling New Requirements Switches Ku-Yaw Chang The Strategy Pattern
Handling New Requirements Switches Ku-Yaw Chang The Strategy Pattern
Handling New Requirements Switches Ku-Yaw Chang The Strategy Pattern
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
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
Handling New Requirements Step 1 Find what varies and encapsulate it A CalcTax object defines the interface Ku-Yaw Chang The Strategy Pattern
Handling New Requirements Step 2 Favor aggregation One SalesOrder class contains the CalcTax class to handle the variations Ku-Yaw Chang The Strategy Pattern
Handling New Requirements Ku-Yaw Chang The Strategy Pattern
Handling New Requirements Advantages Better cohesion More flexible Easier to shift responsibilities Ku-Yaw Chang The Strategy Pattern
Handling New Requirements Ku-Yaw Chang The Strategy Pattern
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
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
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
Generic Structure Ku-Yaw Chang The Strategy Pattern
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
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
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
Summary A way to define a family of algorithms Do the same things Have different implementations Ku-Yaw Chang The Strategy Pattern
The End