Chapter 8, Design Patterns Factory

Slides:



Advertisements
Similar presentations
1 Object Oriented Programming Lecture XII The Adapter,Template, Strategy and Factory design patterns.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
IEG3080 Tutorial 7 Prepared by Ryan.
Abstract Classes An abstract class is a class with partial implementation. It implements behaviors that are common to all subclasses, but defers to the.
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Introduction to Computer Science Recursive Array Programming Recursive Sorting Algorithms Unit 16.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Software Waterfall Life Cycle Requirements Construction Design Testing Delivery and Installation Operations and Maintenance Concept Exploration Prototype.
CSSE 374: Introduction to Gang of Four Design Patterns
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Mediator Pattern and Multiuser Protection Billy Bennett June 8 th, 2009.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Design Patterns for Sorting Teaching something old in a new light Dung “Zung” Nguyen Stephen Wong Rice University.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Computing IV Singleton Pattern Xinwen Fu.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
Creational Patterns
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
Proxy.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
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.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Help Session 3: Induction and Complexity Ezgi, Shenqi, Bran.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Common Design Patterns
Design Patterns Lecture part 2.
Design Patterns for Sorting Teaching something old in a new light
Introduction to Design Patterns
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Design Patterns for Sorting something old in a new light
Programming in Java: lecture 10
Chapter 8, Design Patterns Bridge
object oriented Principles of software design
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Design Patterns for Sorting Teaching something old in a new light
Software Engineering Lecture 7 - Design Patterns
Design Patterns in Game Design
CSC215 Lecture Algorithms.
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant.
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Chapter 8, Design Patterns Singleton
CIS 644 Tues. Nov. 30, 1999 W15A … patterns.
Chapter 8, DesignPatterns Facade
Presentation transcript:

Chapter 8, Design Patterns Factory

A Pattern Taxonomy Pattern Behavioral Creational Structural Pattern Composite Decorator Adapter Bridge Façade Proxy Iterator Command Observer Template Strategy Singleton Abstract Factory Builder Factory Prototype

Animated Sorting Algorithms These programs will display multiple sorting routines running in parallel !! The primary design method will be a factory There will be a factory that produces different sorting routines Another factory will produce different sorting displays The algorithms will be developed through several iterations to illustrate these components Run Those Sorts!

Initial Version The first attempt uses the Template design pattern; the methods are The subclasses define the following methods

Creating Random Data protected void scramble() { arr = new int[getSize().height / 2]; for (int i = arr.length; --i >= 0;) { arr[i] = i; } int j = (int)(i * Math.random()); swap(arr, i, j); This swap routine will be used by the sorting methods private void swap(int a[], int i, int j) { int T; T = a[i]; a[i] = a[j]; a[j] = T;

The paintFrame Routine The integer value will control the height of the line that is drawn protected void paintFrame(Graphics g) { Dimension d = getSize(); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.black); int y = d.height - 1; double f = d.width / (double) arr.length; for (int i = arr.length; --i >= 0; y -= 2) g.drawLine(0, y, (int)(arr[i] * f), y); }

One Sorting Routine protected void MYSTERY(int a[]) { for (int i = a.length; --i >= 0; ) for (int j = 0; j < i; j++) { if (a[j] > a[j+1]) swap(a, j, j + 1); pause(); } Solve the MYSTERY; what name really belongs here for this sort? What is the complexity of this sort if there are n items The code for this sort is short, so does that mean it will run quickly?

protected void quickSort(int a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; int mid; pause(); if (hi0 > lo0) { mid = a[(lo0 + hi0) / 2 ]; while(lo <= hi) { while ((lo < hi0) && (a[lo] < mid)) ++lo; while ((hi > lo0) && (a[hi] > mid)) --hi; if(lo <= hi) { swap(a, lo, hi); pause(); ++lo; --hi; } if(lo0 < hi) quickSort(a, lo0, hi); if(lo < hi0) quickSort(a, lo, hi0); Here is Quicksort

The algorithm Method and Initialization protected void algorithm() { if ("BubbleSort".equals(algName)) bubbleSort(arr); else if ("QuickSort".equals(algName)) quickSort(arr, 0, arr.length - 1); else } protected void initAnimator() { setDelay(20); algName = "BubbleSort"; String at = getParameter("alg"); if (at != null) algName = at; scramble();

Using the Strategy Design Pattern The class relations This sort() method is abstract

The Sort Subclasses class BubbleSortAlgorithm extends SortAlgorithm { void sort(int a[]) { for (int i = a.length; --i>=0; ) for (int j = 0; j<i; j++) { if (a[j] > a[j+1]) swap(a, j, j+1); pause(); } public BubbleSortAlgorithm(AlgorithmAnimator animator) { super(animator); Carefully describe the changes that are needed for the Quicksort subclass

public class QuickSortAlgorithm extends SortAlgorithm { public QuickSortAlgorithm(AlgorithmAnimator animator) { super(animator); } protected void QSort(int a[], int lo0, int hi0) { // the partition process goes here if( lo0 < hi ) QSort( a, lo0, hi ); if( lo < hi0 ) QSort( a, lo, hi0 ); public void sort(int a[]) { QSort(a, 0, a.length - 1);

Creating the Concrete Algorithms Run Sort2

Using an Algorithm Factory

The Interface and Implementation public interface SortAlgorithmFactory { SortAlgorithm makeSortAlgorithm(String algName); } public class StaticAlgoFactory implements SortAlgorithmFactory { public SortAlgorithm makeSortAlgorithm(String algName) { if ("BubbleSort".equals(algName)) return new BubbleSortAlgorithm(animator); else if ("QuickSort".equals(algName)) return new QuickSortAlgorithm(animator); else protected AlgorithmAnimator animator; public StaticAlgoFactory(AlgorithmAnimator animator) { this.animator = animator;

Sort2 Revised public class Sort2 extends AlgorithmAnimator { public void initAnimator() { setDelay(20); algName = "BubbleSort"; String at = getParameter("alg"); if (at != null) algName = at; algorithmFactory = new StaticAlgoFactory(this); theAlgorithm =algorithmFactory.makeSortAlgorithm(algName); scramble(); } protected void algorithm() { if (theAlgorithm != null) theAlgorithm.sort(arr); protected void scramble() // same as before protected void paintFrame(Graphics g) // same as before protected int arr[]; protected String algName; protected SortAlgorithm theAlgorithm; protected SortAlgorithmFactory algorithmFactory; Sort2 Revised

The Factory Design Pattern - 1

The Factory Design Pattern - 2