CS 350 – Software Design The Strategy Pattern – Chapter 9

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Strategy AKA Policy Dale Willey Ian Price. Overview Definitions Difference between Strategy pattern and strategy Where would you use this? Challenges.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Chapter 22 Object-Oriented Design
Chapter 25 More Design Patterns.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 18 Slide 1 Software Reuse.
1 Pattern-Oriented Design by Rick Mercer based on the GoF book and Design Patterns Explained A New Perspective on Object-Oriented Design Alan Shalloway,
Engineering 1020 Introduction to Programming Peter King Winter 2010.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Creational Patterns (1) CS350, SE310, Fall, 2010.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
CS 4240: More Design Patterns Readings: Chap. 9. Let’s Recap Some Ideas and Strategies We’ll assume some design-planning is useful up-front Design with.
Design Patterns. Patterns “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
Strategy Design Patterns CS 590L - Sushil Puradkar.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
CS451 Software Maintenance Yugi Lee STB #555 (816) Note: This lecture was designed based on Stephen Schach’s.
Deriving State…...and an example of combining behaviour.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Multi Dimensional Variance: How to make ultra flexible software!
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Module 9. Dealing with Generalization Course: Refactoring.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Strategy in 15 minutes... derived from the principles.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Programming Logic and Design Seventh Edition
CompSci 280 S Introduction to Software Development
Visit for more Learning Resources
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Strategy Pattern.
GoF Patterns (GoF) popo.
Low Budget Productions, LLC
Conception OBJET GRASP Patterns
Introduction to Design Patterns
Chapter Nine The Strategy Pattern
Behavioral Design Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
The Object-Oriented Thought Process Chapter 08
Algorithm and Ambiguity
Strategy Design Pattern
Object Oriented Practices
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
CS 350 – Software Design A Standard Object-Oriented Solution – Chapter 4 Before studying design patterns, many programmers solve a problem by starting.
Pattern-Oriented Design
Object Oriented Practices
Algorithm and Ambiguity
DESIGN PATTERNS : Strategy Pattern
DESIGN PATTERNS : State Pattern
CS 350 – Software Design Principles and Strategies – Chapter 14
Design pattern Lecture 9.
Algorithms and Problem Solving
CS 350 – Software Design Singleton – Chapter 21
Strategy Design Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Workshop for Programming And Systems Management Teachers
C++ Object Oriented 1.
Software Engineering and Architecture
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore the longer term. Two common approaches: Overanalyze and overdesign: Analysis paralysis Jump right in Somewhere in between is the ideal. This is why I feel the argument in Software Engineering about Agile methodologies vs. traditional waterfall development is silly. It should always be a combination. In addition, a key element is to be wise enough to DESIGN FOR CHANGE. Simple to say, no so intuitive (at first) to do. With practice, it’s not that difficult.

CS 350 – Software Design The Strategy Pattern – Chapter 9 We do not anticipate the exact change, instead we anticipate what may change. New Case Study: E-Commerce System The company is located in the US, but its business has an international scope A Task controller object handles sales request and passes it to the SalesOrder object to process the order.

CS 350 – Software Design The Strategy Pattern – Chapter 9 The functions for SalesOrder include the following: Allow for filling out the order with a GUI Handle tax calculations Process the order and print a sales receipt Some object will be implemented with the help of other objects SalesOrder will not print itself

CS 350 – Software Design The Strategy Pattern – Chapter 9 What happens when requirements change? Perhaps a new requirement for taxation rules is added. Handle taxation for countries outside the United States. Thus, we need to add new rules for computing taxes.

CS 350 – Software Design The Strategy Pattern – Chapter 9 How do we handle different implementations of tasks that are pretty much conceptually the same? Copy and paste Switches/Ifs on variable specifying the case we have Use function pointers (a different one representing each case) Inheritance Delegate the entire functionality to a new object

CS 350 – Software Design The Strategy Pattern – Chapter 9 Copy / Paste Traditional method Copy something that works, paste it somewhere else, make a changes. Maintenance headaches Switches Issues with coupling, testing More importantly, what happens when you have multiple variations?

CS 350 – Software Design The Strategy Pattern – Chapter 9 Simple Cases – Two Countries // Handle Tax switch (myNation) { case US: //US Tax Rules here break; case Canada: //Canadian Tax Rules here } //Handle Currency //US Currency Rules here //Canadian Currency Rules here //Handle Date Format //US Date Format Rules here //Canadian Date Format Rules here

CS 350 – Software Design The Strategy Pattern – Chapter 9 Add a Third Country – Still a “Clean” implementation // Handle Tax switch (myNation) { case US: //US Tax Rules here break; case Canada: //Canadian Tax Rules here case Germany: //Germany Tax Rules here } //Handle Currency //US Currency Rules here //Canadian Currency Rules here //Germany Currency Rules here

CS 350 – Software Design The Strategy Pattern – Chapter 9 Add a Third Country – Still a “Clean” implementation You may need to add another switch // Handle Language switch (myNation) { case US: case Canada: //use English break; case Germany: //use German }

CS 350 – Software Design The Strategy Pattern – Chapter 9 Variations “dirty” the implementation // Handle Language switch (myNation) { case Canada: if (inQuebec) { //use French break;} else //use English break; case Germany: //use German } Flow of the switches is hard to read When new cases come in, you must find every place it can be involved Called Switch Creep

CS 350 – Software Design The Strategy Pattern – Chapter 9 Function Pointers Nice Compact Can’t retain state on a per-object basis Inheritance Nothing really wrong with it per say Certainly allows reuse Could create new SalesOrder objects from existing objects

CS 350 – Software Design The Strategy Pattern – Chapter 9 Inheritance What happens when things vary independently? Too many classes! Thing of the German example where we may vary data format, language, and freight rules

CS 350 – Software Design The Strategy Pattern – Chapter 9 A Better Approach Find out what varies and encapsulate it in a class of its own. Contain this class in another class. What varies in this example? Tax rules, therefore create an abstract class that defines how to accomplish taxation conceptually.

CS 350 – Software Design The Strategy Pattern – Chapter 9 A Better Approach Now use aggregation to give the SalesOrder object the ability to handle Tax.

CS 350 – Software Design The Strategy Pattern – Chapter 9 public abstract class CalcTax { abstract public double taxAmount(long itemSold, double price); } Public class CanTax extends CalcTax { public double TaxAmount (long itemSold, double price){ //in real life, figure out tax according to the rules in Canada and return it //here return 0 so this will compile return 0.0; } Public class USTax extends CalcTax { //in real life, figure out tax according to the rules in the US and return it

CS 350 – Software Design The Strategy Pattern – Chapter 9 public class SalesOrder { public void process (CalcTax taxToUse) { long itemNumber=0; double price=0; //given the tax object to use, calculate the tax double tax = taxToUse.taxAmount(itemNumber, price); } public class TaskController { public void process() { // this code is an emulation of a processing task controller // figure out what country you are in CalcTax myTax; myTax = getTaxRulesForCountry(); SalesOrder mySO = new SalesOrder(); mySO.process(myTax);

CS 350 – Software Design The Strategy Pattern – Chapter 9 A Better Approach

CS 350 – Software Design The Strategy Pattern – Chapter 9 So What’s the Difference? Looking at it quickly, it appears we just pushed the problem down the chain. The key difference is that in the original solution, there was one large hierarchy containing all the variation. The improved solution has a few, smaller, independent hierarchies. Also other pieces of the system can use the smaller object hierarchies we created.

CS 350 – Software Design The Strategy Pattern – Chapter 9 Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. The Strategy Pattern is based on a few 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 the same basic algorithm. It is good design practice to separate behaviors that occur in the same problem domain from each other.

CS 350 – Software Design The Strategy Pattern – Chapter 9 Intent: Enable you to use different business rules or algorithms depending on the context in which they occur. Problem: The selection of an algorithm that needs to be applied depends on the client making the request or the data being acted on. Solution: Separate the selection of the algorithm from the implementation of the algorithm.

CS 350 – Software Design The Strategy Pattern – Chapter 9 Implementation: Have the class that uses the algorithm (Context) contain an abstract class (Strategy) that has an abstract method specifying how to call the algorithm. Each derived class implements the algorithm needed.