Strategy in 15 minutes... derived from the principles.

Slides:



Advertisements
Similar presentations
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
1 Postdelivery Maintenance Xiaojun Qi. 2 Why Postdelivery Maintenance Is Necessary Corrective maintenance: To correct residual faults –Analysis, design,
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 18 Slide 1 Software Reuse.
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.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
GoF Sections Design Problems and Design Patterns.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Object Oriented Software Development
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
Test Stubs... getting the world under control. TDD of State Pattern To implement GammaTown requirements I CS, AUHenrik Bærbak Christensen2.
Object Oriented Programming
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Deriving State…...and an example of combining behaviour.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Multi Dimensional Variance: How to make ultra flexible software!
Applying the Principles Two Examples. Example 1 New Requirement It would be nice with a simple GUI “to see something” instead of just xUnit tests...
Refactoring and Integration Testing or Strategy, introduced reliably by TDD The power of automated tests.
MiniDraw Introducing a Framework... and a few patterns.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Patterns are Roles What patterns are and what not…
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
CS, AUHenrik Bærbak Christensen1 Compositional Design Principles The “GoF” principles Or Principles of Flexible Design.
CSHenrik Bærbak Christensen1 Flexibility and Maintainability And their metrics: coupling and cohesion.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Design Patterns: MORE Examples
Abstract Factory Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Factory Patterns 1.
Applying the Principles
Inheritance and Polymorphism
CS 350 – Software Design The Strategy Pattern – Chapter 9
Chapter Nine The Strategy Pattern
Behavioral Design Patterns
Instructor: Dr. Hany H. Ammar
Compositional Design Principles
Lecture 9-2: Interacting with the Superclass (super);
Section 11.1 Class Variables and Methods
Intent (Thanks to Jim Fawcett for the slides)
CS 325: Software Engineering
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Software Engineering and Architecture
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Strategy in 15 minutes... derived from the principles

New requirement Betatown: Maybe we will see future changes in pricing models ??? How can we handle these two products? AUHenrik Bærbak Christensen2 “New progressive price model”

The present code AUHenrik Bærbak Christensen3 This is the spot where things may change: variability point

Analysis: The 4 variability techniques Model 1: –Source code copy: make a copy, modify it Model 2: –Parameterization: Throw in some ‘if’-statements Model 3: –Polymorphic proposal: Variation through inheritance Model 4: –Compositional proposal: Factor out rate model responsibility AUHenrik Bærbak Christensen4

Model 1: Source tree copying AUHenrik Bærbak Christensen5 Idea: Deep copy production code source tree Code the new variant by replacing the code at the variability point.

Technique Applied AUHenrik Bærbak Christensen6 And begin to code BetaTown rate calculations in the copy!

Benefits Benefits: –It is simple! no special skill set required in developer team easy to explain idea to new developers –It is fast! < 5 minutes? –It provides perfect variant decoupling defects introduced in variant 2 does not reduce reliability of variant 1 easy to distinguish variants (consult folder hierarchy) AUHenrik Bærbak Christensen7

Liabilities Liabilities: Multiple maintenance problem  –Changes in common code must be propagated to all copies –Usually manual process (or tedious SCM operation) Example: –4 pay station variants (different rate policies) –request: pay station keeps track of earning  Experience: Variants drift apart, becoming different products instead of variants... AUHenrik Bærbak Christensen8

Model 2: Parametric Idea: –It is only a single “behavioural unit” in the addPayment method that varies –I can simply make a conditional statement there A) Introduce a parameter (Which town) B) Switch on the parameter each time town specific behaviour is needed. AUHenrik Bærbak Christensen9

Technique Applied AUHenrik Bærbak Christensen10

Benefits Benefits: –Simple A conditional statement is one of the first aspects learned by programmers, used widely, and thus easy to understand for any skill level developer team –Avoid multiple maintenance problem Yeah!!! Common defects/requirements are handled once and for all. AUHenrik Bærbak Christensen11

Liabilities Liabilities: –Reliability concerns All variants in same code file => constant modifications => probability of errors introduced –Readability concerns The core algorithms are lost in large numbers of if-else switches –Responsibility erosion PayStationImpl now also must handle variability management AUHenrik Bærbak Christensen12

Model 3: Polymorphic proposal Subclass and override! AUHenrik Bærbak Christensen13

Proposal 3: Instantiation AUHenrik Bærbak Christensen14 Instantiation: PayStation ps = new PayStationProgressiveRate();

Analysis Benefits –Avoid multiple maintenance –Reliability concern –Code readability Liabilities –Increased number of classes –Inheritance relation spent on single variation type –Reuse across variants difficult –Compile-time binding AUHenrik Bærbak Christensen15

Benefits Reliability concern –The first time I add a new rate policy I change by modification! I have to refactor the code to introduce the new private method calculateTime But –All following new requirements regarding rate policies can be handled by adding new subclasses, not by modifying existing classes. –Thus, no fear of introducing defects in existing software; no regression testing, no reviews. Change by addition, not by modification AUHenrik Bærbak Christensen16

Benefits Readability –There is no code bloating from introduction conditional statements –I simply add new classes instead AUHenrik Bærbak Christensen17

Liabilities  Increased number of classes –I have to add one new class for each rate policy variant –thus instead of 43 if statements in one class I get 43 subclasses to overview AUHenrik Bærbak Christensen18

Liabilities  Spent inheritance on single variation type –You have “wasted” your single implementation- inheritance capability on one type of variation! The name is odd – isn’t it? The parameter is part of the name “PayStationProgressiveRate” What is next: “PayStationProgressiveRateButLiniarInWeekendsWithOracle DataBaseAccessDebuggingVersionAndBothCoinAndMobileP honePaymentOptions” ??? –We will discuss this problem in detail later... AUHenrik Bærbak Christensen19

Liabilities  Inheritance is a compile time binding –Inheritance is a compile time binding !!! you literally write “extends / :” in your editor !!! –Thus you cannot change rate model except by rewriting code! Sorts of similar to “change by modification ” –And it is completely impossible to dynamically change rate policy at run-time or at start-up time. AUHenrik Bærbak Christensen20

Proposal 4: Composition AUHenrik Bærbak Christensen21 Golden rule: No abstraction should have too many responsibilities. Max 3 is a good rule of thumb… (Facade objects are an exception)

Serving too many responsibilities The reason that we have to modify code to handle the new requirement instead of adding code is because The change revolves around a responsibility (calculate parking time) that is buried within an abstraction and mixed up with many other responsibilities (print receipt, handle buy, etc.) !!! So: What do we do??? AUHenrik Bærbak Christensen22

Divide responsibilities - compose them A proposal is simply to Put the responsibility in its own abstraction / object AUHenrik Bærbak Christensen23

Delegation AUHenrik Bærbak Christensen24 The basic principle is simple but powerful: –Instead of one object doing it all by itself, it asks another object to help out. Some of the job is handled by another “actor” – the delegate This principle has a name:

Concrete behaviours Responsibilities must be served by concrete behaviour in objects... AUHenrik Bærbak Christensen25

Code View AUHenrik Bærbak Christensen26

Analysis Benefits –Readability –Run-time binding –Separation of responsibilities –Variant selection is localized –Combinatorial Liabilities –Increased number of interfaces, objects –Clients must be aware of strategies AUHenrik Bærbak Christensen27

Analysis Readability –no code bloat of conditional statements Run-time binding –I can actually change the rate policy while the system is running. Leads to lower maintenance costs as no shut down required AUHenrik Bærbak Christensen28

Benefits Responsibilities clearly stated in interfaces –Leads to No Odd Naming: –PayStation and RateStrategy: The responsibilities –LinearRateStrategy ect: Concrete behaviour fulfilling responsibilities –The pay station has “lost some fat” by separating responsibilities the cohesion of the code within each abstraction is higher Note though that from the GUI/hardware’s perspective, the pay station still has the ‘formal’ responsibility to calculate rates! AUHenrik Bærbak Christensen29

Benefits Variant selection localized –There is only one place in the code where I decide which rate policy to take namely in the configuration/main code where I instantiate the pay station! –contrast to the parametric solution where selection and decision code is smeared all over the place –No variant handling code at all in the pay station code ! AUHenrik Bærbak Christensen30

Benefits Combinatorial –I have not used inheritance – we can still subclass it to provide new behavior on other aspects – without interfering with the rate calculation! –But – much more on that later... AUHenrik Bærbak Christensen31

The process

So – Pizza from the ingredients  We have identified some behaviour that is likely to change… –rate policies  We have clearly stated a responsibility that covers this behaviour and expressed it in an interface:  The parking machine now perform rate calculations by letting a delegate object do it: the RateStrategy object. –time = rateStrategy.calculateTime(amount); AUHenrik Bærbak Christensen33 > RateStrategy -- Calculate Parkingtime

The process I call this “mini-process” of handling variability for the process The reason for the odd numbering is its relations to the compositional design principles that were first put forward in the Design Pattern book (GoF) by Gamma et al. / Chapter 1.6 The number refer to the sequence in the GoF book that the principle is mentioned. AUHenrik Bærbak Christensen34

Transferring responsibilities Actually this is a common thing in everyday life Many years ago I transferred the responsibility to empty the garbage can to my eldest son –(not without some heated arguments though ) Project leaders’ main responsibility is – to delegate responsibility to other people And why? Because A) we cannot do everything ourselves and B) too many responsibilities leads to stress and errors! AUHenrik Bærbak Christensen35

Key technique: Delegation In software this simple technique ”let someone else do the dirty job” is called delegation. Instead of an object doing it itself: –time = this.calculateTime(amount); –this.takeGarbageToGarbageCan(); we let some specialist object do it for us: –time = rateStrategy.calculateTime(amount); –son.takeGarbageToGarbageCan(); AUHenrik Bærbak Christensen36

Conclusion We have derived the strategy pattern by analysing our problem in a certain way! AUHenrik Bærbak Christensen37

Summary From the ingredients –  identified behaviour likely to change –  express responsibility for behaviour as interfaces –  use delegation to support behaviour we have derived a pattern automagically This is the nuts and bolts for most (behavioural) patterns ! AUHenrik Bærbak Christensen38