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.

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.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
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.
Software Lifecycle A series of steps through which a software product progresses Lifetimes vary from days to months to years Consists of –people –overall.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Chapter 22 Object-Oriented Design
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
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,
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object-oriented programming and software development Lecture 1.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
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.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
Strategy Design Patterns CS 590L - Sushil Puradkar.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Chapter 7 Software Engineering Introduction to CS 1 st Semester, 2015 Sanghyun Park.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
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.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
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.
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
CS 210 Introduction to Design Patterns August 29, 2006.
CS451 Software Maintenance Yugi Lee STB #555 (816) Note: This lecture was designed based on Stephen Schach’s.
Introduction to OOP CPS235: Introduction.
Deriving State…...and an example of combining behaviour.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
In class exercise (I) Problem specification: An e-commerce company based in Maryland, U.S. called Dogwood, needs an online sale system. You are asked to.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Multi Dimensional Variance: How to make ultra flexible software!
Chapter Ten The Bridge Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
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.
Programming Logic and Design Seventh Edition
CompSci 280 S Introduction to Software Development
Strategy Design Pattern
Strategy Pattern.
Low Budget Productions, LLC
Conception OBJET GRASP Patterns
Introduction to Design Patterns
CS 350 – Software Design The Strategy Pattern – Chapter 9
Chapter Nine The Strategy Pattern
Behavioral Design Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
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
DESIGN PATTERNS : State Pattern
Design pattern Lecture 9.
Strategy Design Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
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 Overanalyze and overdesign: Analysis paralysis Jump right in 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 Allow for filling out the order with a GUI Handle tax calculations Handle tax calculations Process the order and print a sales receipt Process the order and print a sales receipt Some object will be implemented with the help of other objects SalesOrder will not print itself 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 Copy and paste Switches/Ifs on variable specifying the case we have Switches/Ifs on variable specifying the case we have Use function pointers (a different one representing each case) Use function pointers (a different one representing each case) Inheritance Inheritance Delegate the entire functionality to a new object Delegate the entire functionality to a new object

CS 350 – Software Design The Strategy Pattern – Chapter 9 Copy / Paste Traditional method Traditional method Copy something that works, paste it somewhere else, make a changes. Copy something that works, paste it somewhere else, make a changes. Maintenance headaches Maintenance headachesSwitches Issues with coupling, testing Issues with coupling, testing More importantly, what happens when you have multiple variations? 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: case US: //US Tax Rules here //US Tax Rules here break; break; case Canada: case Canada: //Canadian Tax Rules here //Canadian Tax Rules here break; break;} //Handle Currency switch (myNation) { case US: case US: //US Currency Rules here //US Currency Rules here break; break; case Canada: case Canada: //Canadian Currency Rules here //Canadian Currency Rules here break; break;} //Handle Date Format switch (myNation) { case US: case US: //US Date Format Rules here //US Date Format Rules here break; break; case Canada: case Canada: //Canadian Date Format Rules here //Canadian Date Format Rules here break; break;}

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

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 US: case Canada: case Canada: //use English //use English break; break; case Germany: case Germany: //use German //use German break; break;}

CS 350 – Software Design The Strategy Pattern – Chapter 9 Variations “dirty” the implementation // Handle Language switch (myNation) { case Canada: case Canada: if (inQuebec) { if (inQuebec) { //use French //use French break;} break;} else else //use English //use English break; break; case Germany: case Germany: //use German //use German break; break;} 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 Nice Compact Compact Can’t retain state on a per-object basis Can’t retain state on a per-object basisInheritance Nothing really wrong with it per say Nothing really wrong with it per say Certainly allows reuse Certainly allows reuse Could create new SalesOrder objects from existing objects 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 1. Find out what varies and encapsulate it in a class of its own. 2. 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); } abstract public double taxAmount(long itemSold, double price); } Public class CanTax extends CalcTax { public double TaxAmount (long itemSold, double price){ public double TaxAmount (long itemSold, double price){ //in real life, figure out tax according to the rules in Canada and return it //in real life, figure out tax according to the rules in Canada and return it //here return 0 so this will compile //here return 0 so this will compile return 0.0; return 0.0; }} Public class USTax extends CalcTax { public double TaxAmount (long itemSold, double price){ public double TaxAmount (long itemSold, double price){ //in real life, figure out tax according to the rules in the US and return it //in real life, figure out tax according to the rules in the US and return it //here return 0 so this will compile //here return 0 so this will compile return 0.0; return 0.0; }}

CS 350 – Software Design The Strategy Pattern – Chapter 9 public class SalesOrder { public void process (CalcTax taxToUse) { public void process (CalcTax taxToUse) { long itemNumber=0; long itemNumber=0; double price=0; double price=0; //given the tax object to use, calculate the tax //given the tax object to use, calculate the tax double tax = taxToUse.taxAmount(itemNumber, price); double tax = taxToUse.taxAmount(itemNumber, price); }} public class TaskController { public void process() { public void process() { // this code is an emulation of a processing task controller // this code is an emulation of a processing task controller // figure out what country you are in // figure out what country you are in CalcTax myTax; CalcTax myTax; myTax = getTaxRulesForCountry(); myTax = getTaxRulesForCountry(); SalesOrder mySO = new SalesOrder(); SalesOrder mySO = new SalesOrder(); mySO.process(myTax); 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 Strategy Pattern 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 Objects have responsibilities Different specific implementations of these responsibilities are manifested through the use of polymorphism 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. 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 Strategy Pattern 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 Strategy Pattern 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.