CSC 313 – Advanced Programming Topics. Strategy Pattern Usage public class RubberDuck extends Duck { FlightBehavior flyBehavior; QuackBehavior quackBehavior;

Slides:



Advertisements
Similar presentations
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Advertisements

Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related.
CS 210 Introduction to Design Patterns September 19 th, 2006.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Computer Science 313 – Advanced Programming Topics.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
Beware of bugs in the above code; I have only proved it correct, not tried it.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
CSC 313 – Advanced Programming Topics. Open-Closed Principle Classes should be open for extension, but closed to modification  So, what does this mean?
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
CSC 313 – Advanced Programming Topics. Decorator Pattern Intent.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Design Patterns Introduction
Religious Studies 313 – Advanced Programming Topics.
CS 210 Introduction to Design Patterns August 29, 2006.
Interfaces and Inner Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSC 480 Software Engineering Design With Patterns.
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Fred Brooks Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Bjarne Stroustrup I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my.
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.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Design Patterns Spring 2017.
Static data members Constructors and Destructors
CIS 200 Test 01 Review.
Factory Patterns 1.
Creational Pattern: Prototype
Factory Method, Abstract Factory, and More
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
null, true, and false are also reserved.
Object Oriented Design Patterns - Structural Patterns
Singleton design pattern
CS 350 – Software Design Singleton – Chapter 21
Chapter 8, Design Patterns Singleton
CSC 480 Software Engineering
Presentation transcript:

CSC 313 – Advanced Programming Topics

Strategy Pattern Usage public class RubberDuck extends Duck { FlightBehavior flyBehavior; QuackBehavior quackBehavior; public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay(); }

Strategy Pattern Usage public class RubberDuck extends Duck { FlightBehavior flyBehavior; QuackBehavior quackBehavior; public RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay(); } Squeak FlyNoWay RubberDuck

Decorator Pattern Usage Pizza pie = new DeepDish(); pie = new Garlic(pie); pie = new Onion(pie); pie

Zen & the Art of Programming Identify and isolate what will change from what stays the same Favor composition over inheritance Classes should be open for extension, but closed to modification Program to a concept, not a class

Zen & the Art of Programming Identify and isolate what will change from what stays the same Favor composition over inheritance Classes should be open for extension, but closed to modification Program to a concept, not a class

Zen & the Art of Programming Identify and isolate what will change from what stays the same Favor composition over inheritance Classes should be open for extension, but closed to modification Program to a concept, not a class

Zen & the Art of Programming Identify and isolate what will change from what stays the same Favor composition over inheritance Classes should be open for extension, but closed to modification Program to a concept, not a class

Zen & the Art of Programming Identify and isolate what will change from what stays the same Favor composition over inheritance Classes should be open for extension, but closed to modification Program to a concept, not a class

Programming to a Concept

Decorator Pattern Problem Need DoubleGarlicOnionDeepDish class for this? Pizza pie = new Garlic(DeepDish()); pie = new Onion(Garlic(pie));

Strategy Pattern Usage

Relations Between Patterns  Design patterns can be used alone  Intent & purpose differs for each pattern  “Official” patterns in at least 3 industrial projects  Often travel together in code  Many strong relationships between patterns  Combination stronger than using by itself

Improving Constructor  May want to limit creating objects  Control instantiation and when or how it occurs  May want to enforce limit on number instantiated  Boolean instances silly, for example  Wastes time & memory, only have 2: true & false  Constructors limited in their actions, however  Before constructor starts, instance already allocated  Exception only option to disrupt allocation process

Smarter Move public class Bool { static Bool TRUE = new Bool(true); static Bool FALSE = new Bool(false); private boolean value; private Bool(boolean b) { value = b; } static Bool fromBoolean(boolean b) { if (b) return TRUE; else return FALSE; }

Cache Data To Save Time public class Int { static Map map; int value; private Int(int i) { value = i; } public Int fromString(String s) { Int retVal = map.get(s); if (retVal == null) { retVal = new Int(Integer.parseInt(s)); map.put(s, retVal); } return retVal; }

Smart Instantiation

Better Instantiation Factory Methods  Methods like these called Factory Methods  Method creates objects just like it is a factory  Factory methods provide additional features  Meaningful name can be given to method  Using set of methods, creation options clarified  Readable code easy & no jumping through hoops

Factory Method Example public class Int { static Map map; int value; private Int(int i) { value = i; } public Int fromString(String s) { Int retVal = map.get(s); if (retVal == null) { retVal = new Int(Integer.parseInt(s)); map.put(s, retVal); } return retVal; }

Simple Factory Pattern  Also known as Static Factory Pattern  (Only if method static & not instance based)  Pattern uses single factory method as base  Multiple types instantiated in factory method new  Contains specific, hard-coded “ new ” commands  Other classes use method to skip instantiations  Changing or adding types is much easier  Only need to modify factory method  Client code may not know other types exist!

Static Factory Method public class StaticFactory { static Pizza createPizza(String type) { if (type.equals(“cheese”)) { return new CheesePizza(); } else if (type.equals(“fish”)) { return new AnchovyPizza(); } else { throw Exception(“No Pizza for you!”); } } Pizza pie = StaticFactory.createPizza(“fish”);

Simple Factory Method public class SimpleFactory { Pizza createPizza(String type) { if (type.equals(“cheese”)) { return new CheesePizza(); } else if (type.equals(“fish”)) { return new AnchovyPizza(); } else { throw Exception(“No Pizza for you!”); } } SimpleFactory simple =... Pizza pie = simple.createPizza(“fish”);

Creating Simple Factory  Factory method instantiates many types  protected  protected constructors prevents unlimited alloc.  Within method, parameter(s) specify what to allocate  Pluses & minuses to static factory method use  Factory method much easier to find and call  Client tied to factory class via hard-coded call  Keeping factory method non-static means  More generic client code calling factory method  Exactly which method called not clear

For Next Lecture  Lab #3 available on Angel  Asks you to implement Decorator Pattern but  Have time today, but may want help profiling  In your book, read pages  How do we write these factories?  Is there a good way to do this?  What design pattern could be used in these cases?