CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park



Similar documents
Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

How To Design Your Code In Php (Php)

Designing and Writing a Program DESIGNING, CODING, AND DOCUMENTING. The Design-Code-Debug Cycle. Divide and Conquer! Documentation is Code

Design Patterns. Advanced Software Paradigms (A. Bellaachia) 1

Structural Design Patterns Used in Data Structures Implementation

Adapter, Bridge, and Façade

Contents. Introduction and System Engineering 1. Introduction 2. Software Process and Methodology 16. System Engineering 53

The Interface Concept

Programming Methods & Java Examples

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Glossary of Object Oriented Terms

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Introduction to Patterns and Frameworks

CS330 Design Patterns - Midterm 1 - Fall 2015

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

INPUT AND OUTPUT STREAMS

Iterators. Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.

ABSTRACT. Development. Associate Professor Mark Austin Institute for Systems Research and the Department of Civil and Environmental Engineering

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

A DESIGN PATTERNS PERSPECTIVE ON DATA STRUCTURES. Virginia Niculescu

El Dorado Union High School District Educational Services

Moving from CS 61A Scheme to CS 61B Java

Génie Logiciel et Gestion de Projets. Patterns

Twin A Design Pattern for Modeling Multiple Inheritance

Computer Programming I

A Catalogue of General-Purpose Software Design Patterns

Computing Concepts with Java Essentials

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

CS 111 Classes I 1. Software Organization View to this point:

Chapter 8: Bags and Sets

Java Design Patterns. Java Design Patterns

Tutorial on Writing Modular Programs in Scala

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Automated Software Evolution via Design Pattern Transformations

Java SE 8 Programming

D06 PROGRAMMING with JAVA

Curriculum Map. Discipline: Computer Science Course: C++

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

AVRO - SERIALIZATION

Data Structures in the Java API

Design Patterns. V Software Engineering Lecture 8. Adapted from Prof. Necula CS 169, Berkeley 1

Design Patterns in Java

Java Application Developer Certificate Program Competencies

Data Structures and Algorithms

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Chapter 1 Fundamentals of Java Programming

Assignment # 2: Design Patterns and GUIs

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Developer Guide. Christian W. Günther Library version: 1.0 RC7 Revision: 7

ABSTRACT FACTORY AND SINGLETON DESIGN PATTERNS TO CREATE DECORATOR PATTERN OBJECTS IN WEB APPLICATION

Java SE 7 Programming

Java SE 7 Programming

Zabin Visram Room CS115 CS126 Searching. Binary Search

Iterator Pattern. CSIE Department, NTUT Chien-Hung Liu. Provide a way to access the elements of an aggregate object sequentially

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

COMPUTER SCIENCE A. Course Description

History OOP languages Year Language 1967 Simula Smalltalk

Automaton Programming and Inheritance of Automata

Class 32: The Java Collections Framework

Roulette: Inheritance Case Study

COMPUTER SCIENCE. Paper 1 (THEORY)

Java Interview Questions and Answers

2/9/2010. Standard Approach to Distribution. Remote Procedure Calls (RPC) Example: Music Jukebox in the Cloud

ios Design Patterns Jackie Myrose CSCI 5448 Fall 2012

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

AP Computer Science AB Syllabus 1

Java SE 7 Programming

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

A Pattern for Designing Abstract Machines

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Collections and iterators

Searching Algorithms

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Aerospace Software Engineering

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

TDDB84 Design Patterns Exam

How To Design Software

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Exercise 8: SRS - Student Registration System

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Software Architectures

Software Development Methodologies

Lecture 2 - Design. 1) Why would you want to make a radical problem normal? We know how to solve normal problems already.

Computer Programming I

15-214: Principles of Software Construction 8 th March 2012

Realizing Enterprise Integration Patterns in WebSphere

AP Computer Science Java Subset

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Java EE Web Development Course Program

EpochX: Genetic Programming in Java with Statistics and Event Monitoring

Using Files as Input/Output in Java 5.0 Applications

Android Application Development Course Program

Cours de C++ Utilisations des conteneurs

Input Output. 9.1 Why an IO Module is Needed? Chapter 9

Semantic Analysis: Types and Type Checking

Transcription:

CMSC 132: Object-Oriented Programming II Design Patterns I Department of Computer Science University of Maryland, College Park

Design Patterns Descriptions of reusable solutions to common software design problems (e.g, Iterator pattern) Captures the experience of experts Goals Solve common programming challenges Improve reliability of solution Aid rapid software development Useful for real-world applications Design patterns are like recipes generic solutions to expected situations Design patterns are language independent Recognizing when and where to use design patterns requires familiarity & experience Design pattern libraries serve as a glossary of idioms for understanding common, but complex solutions Design patterns are used throughout the Java Class Libraries

Documentation Format 1. Motivation or context for pattern 2. Prerequisites for using a pattern 3. Description of program structure 4. List of participants (classes & objects) 5. Collaborations (interactions) between participants 6. Consequences of using pattern (good & bad) 7. Implementation techniques & issues 8. Example codes 9. Known uses 10. Related patterns

Types of Design Patterns Creational Deal with the best way to create objects Structural Ways to bring together groups of objects Behavioral Ways for objects to communicate & interact

Creational Patterns 1. Abstract Factory- Creates an instance of several families of classes 2. Builder - Separates object construction from its representation 3. Factory Method - Creates an instance of several derived classes 4. Prototype - A fully initialized instance to be copied or cloned 5. Singleton - A class of which only a single instance can exist

Structural Patterns 6. Adapter - Match interfaces of different classes 7. Bridge - Separates an object s interface from its implementation 8. Composite - A tree structure of simple and composite objects 9. Decorator - Add responsibilities to objects dynamically 10. Façade - Single class that represents an entire subsystem 11. Flyweight - Fine-grained instance used for efficient sharing 12. Proxy - Object representing another object

Behavioral Patterns 13. Chain of Responsibility - A way of passing a request between a chain of objects 14. Command - Encapsulate a command request as an object 15. Interpreter - A way to include language elements in a program 16. Iterator - Sequentially access the elements of a collection 17. Mediator - Defines simplified communication between classes 18. Memento - Capture and restore an object's internal state

Behavioral Patterns (cont.) 19. Observer - A way of notifying change to a number of classes 20. State - Alter an object's behavior when its state changes 21. Strategy - Encapsulates an algorithm inside a class 22. Template Method - Defer the exact steps of an algorithm to a subclass 23. Visitor - Defines a new operation to a class without changing class

Iterator Pattern Definition Move through collection of objects without knowing its internal representation Where to use & benefits Use a standard interface to represent data objects Uses standard iterator built in each standard collection, like List, Sort, or Map Need to distinguish variations in the traversal of an aggregate Example Iterator for collection Original Examine elements of collection directly Using pattern Collection provides Iterator class for examining elements in collection

Iterator Example public interface Iterator<V> { bool hasnext(); V next(); void remove(); } Iterator<V> it = mycollection.iterator(); while ( it.hasnext() ) { V x = it.next(); // finds all objects // in collection }

Singleton Pattern Definition One instance of a class or value accessible globally Where to use & benefits Ensure unique instance by defining class final Access to the instance only via methods provided Example public class Employee { public static final int ID = 1234; // ID is a singleton } public final class MySingleton { // declare the unique instance of the class private static MySingleton uniq = new MySingleton(); // private constructor only accessed from this class private MySingleton() { } // return reference to unique instance of class public static MySingleton getinstance() { return uniq; } }

Adapter Pattern Definition Convert existing interfaces to new interface Where to use & benefits Help match an interface Make unrelated classes work together Increase transparency of classes Example Adapter from integer Set to integer Priority Queue Original Integer set does not support Priority Queue Using pattern Adapter provides interface for using Set as Priority Queue Add needed functionality in Adapter methods

Adapter Example public interface PriorityQueue { void add(object o); int size( ); Object removesmallest( ); } // Priority Queue public class PriorityQueueAdapter implements PriorityQueue { Set s; PriorityQueueAdapter(Set s) { this.s = s; } public void add(object o) { s.add(o); } int size( ) { return s.size(); } } public Integer removesmallest( ) { Integer smallest = Integer.MAX_VALUE; for (Integer i : s) { if (i.compareto(smallest) < 0) smallest = i; } s.remove(smallest); return smallest; }

Factory Pattern Definition Provides an abstraction for deciding which class should be instantiated based on parameters given Where to use & benefits A class cannot anticipate which subclasses must be created Separate a family of objects using shared interface Hide concrete classes from the client Example Car Factory produces different Car objects Original Different classes implement Car interface Directly instantiate car objects Need to modify client to change cars Using pattern Use car factory class to produce car objects Can change cars by changing car factory

Factory Example class Ferrari implements Car; // fast car class Bentley implements Car; // antique car class Explorer implements Car; // family SUV Car fast = new Ferrari(); // returns fast car public class carfactory { public static Car create(string type) { if (type.equals("fast")) return new Ferrari(); if (type.equals( antique")) return new Bentley(); else if (type.equals( family ) return new Explorer(); } } Car fast = carfactory.create("fast"); // returns fast car

Decorator Pattern Definition Attach additional responsibilities or functions to an object dynamically or statically Where to use & benefits Provide flexible alternative to subclassing Add new function to an object without affecting other objects Make responsibilities easily added and removed dynamically & transparently to the object Example Pizza Decorator adds toppings to Pizza Original Pizza subclasses Combinatorial explosion in # of subclasses Using pattern Pizza decorator classes add toppings to Pizza objects dynamically Can create different combinations of toppings without modifying Pizza class Example: PizzaDecoratorCode

Decorator Pattern Examples from Java I/O Interface InputStream Concrete subclasses FileInputStream, ByteArrayInputStream Decorators BufferedInputStream, DataInputStream Code InputStream s = new DataInputStream( new BufferedInputStream (new FileInputStream()));

Marker Interface Pattern Definition Label semantic attributes of a class Where to use & benefits Need to indicate attribute(s) of a class Allows identification of attributes of objects without assuming they are instances of any particular class Example Classes with desirable property GoodProperty Original Store flag for GoodProperty in each class Using pattern Label class using GoodProperty interface Examples from Java Cloneable Serializable

Marker Interface Example public interface SafePet { } // no methods class Dog implements SafePet { } class Piranha { } Dog dog = new Dog(); Piranha piranha = new Piranha(); if (dog instanceof SafePet) if (piranha instanceof SafePet) // True // False