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.

Slides:



Advertisements
Similar presentations
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Advertisements

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.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
OOP Languages: Java vs C++
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Case Studies on Design Patterns Design Refinements Examples.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Y2 eProjects Session 4 – Advanced Topics. Objectives  Dynamic Models  Design Patterns (Optional)  Software testing (for S4) ACCP i7.1\Sem3_4\eProject\T4.
Object-Oriented Design Principles and Patterns. © 2005, James R. Vallino2 How Do You Design? What principles guide you when you create a design? What.
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.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Strategy Design Patterns CS 590L - Sushil Puradkar.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
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.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
Stéphane Ducasse 1 Strategy.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Written by: Dr. JJ Shepherd
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Object-Oriented Design
Strategy: A Behavioral Design Pattern
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
CS 350 – Software Design The Strategy Pattern – Chapter 9
Chapter Nine The Strategy Pattern
Creational Pattern: Prototype
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Chapter 3: Using Methods, Classes, and Objects
Design pattern Lecture 9.
Strategy Design Pattern
Introduction to Design Patterns
Software Design Lecture : 39.
GoF Patterns Ch. 26.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

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 change in mind  Change will happen  Not sure exactly what it will be  Can predict in advance where it’s likely to happen

Strategies in Response to This In anticipation of change, consider doing this:  Program to an interface not an implementation  Favor aggregation over inheritance  Encapsulate things that will vary Even if you’re not quite sure how they may vary

Often Logic Varies Read textbook pp Example: calculating sales tax  A SalesOrder object-instance must know how to calculate tax for itself  Rules vary by country  Rules change  New countries/rules may be added

Strategy Pattern The idea:  When choice of an algorithm is a dimension of variability among a family of similar things  Hide this behind an interface  The client codes to this interface  This object is usually aggregated with another object This pairing must be established before use

Is this Common? You’ve seen the Java Comparator interface Business rules  University registration system  Student Bob wants to register for CS3205  Allowed or not? selectedCourse.allowReg(aStudent)  Lots of logic here. Varies by course. Varies by student (on probation? too many credits?) Changes over time.

Strategy Pattern Description, part 1 Intent:Enables 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. If you just have a rule in place that does not change, you do not need a Strategy pattern. Solution:Separates the selection of algorithm from the implementation of the algorithm. Allows for the selection to be made based upon context.

Strategy Pattern Description, part 2

Strategy Pattern Description, part 3 Participants and collaborators:  Strategy specifies common interface for the algorithms.  ConcreteStrategies implement these different algorithms.  Context linked to a specific ConcreteStrategy with a reference of type Strategy.  Strategy and Context interact to implement the chosen algorithm. Context forwards requests from its client to Strategy. Sometimes Strategy must query Context.

Strategy Pattern Description, part 4 Consequences:  The Strategy pattern defines a family of algorithms.  Switches and/or conditionals can be eliminated. This might improve run-time efficiency!  You must invoke all algorithms in the same way. (They must all have the same interface.)  The interaction between the Concrete Strategies and the Context may require the addition of methods that get state to the Context.

Strategy Pattern Description, part 5 Implentation notes, etc.  Client responsible for linking a Context object with a particular ConcreteStrategy This means Client must “understand” what strategies are there. So some coupling but at a higher level. Note this link is dynamic -- could change If inheritance used to define a “SubContext” class, it’s fixed  Optional Strategy makes sense? Allow for Context to have null for its reference to Strategy So must check first before calling If null, Context has default behavior coded in Context

Think About and Discuss Are State and Strategy DPs similar? The same? How different? Suggestion: look up Web resources to learn more.

Strategy Examples Java comparator objects?  Collections.sort(theList, new MyComparator());  How does this match the pattern? How not?

java.util.zip Two classes need an algorithm for calculating check-sums  CheckedInputStream, CheckedOutputStream Two algorithms defined that meet CheckSum interface  Adler32 and CRC32 Constructors for first two classes get an instance of Checksum

java.util.zip and Checksum interface Interface Checksum: long getValue() : Returns the current checksum value.getValue void reset() : Resets the checksum to its initial value.reset void update(byte[] b, int off, int len) : Updates the current checksum with the specified array of bytes.update void update(int b) : Updates the current checksum with the specified byte.update

Other Examples Input field validation in GUIs  Valid type, range, values, etc.  A Validate interface hides algorithm/logic used  Each input-field linked to a Validate instance  All fields told to validate() their contents Memory allocation (a la C/C++)  Get memory chunks, return chunks  But could involve memory-pools, caches, locks, other management

Note: Again, here’s class that more like an operation than a “thing” or entity

END