Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Plab – Tirgul 12 Design Patterns
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
1 Scenario: Audio Clip Imagine that your application played an audio clip Based on user action, a different audio clip may begin playing You want only.
NJIT Designing for Visibility Chapter 19 Applying UML and Patterns Craig Larman.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Trends and Case Study John Hurst June 2005.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Design Patterns. Now you are ready for Design Patterns Design patterns are recurring solutions to software design problems you find again and again in.
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.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Y2 eProjects Session 4 – Advanced Topics. Objectives  Dynamic Models  Design Patterns (Optional)  Software testing (for S4) ACCP i7.1\Sem3_4\eProject\T4.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Logging1. 2 Introduction Ships must keep a written log telling speed, direction, destination, etc. –A kind of diary of the ship. Large programs should.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Design Patterns Singleton & Factory Pattern Eriq Muhammad Adams J. Mail : | Blog :
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
CSC 480 Software Engineering Design With Patterns.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
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.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Refactoring1 Improving the structure of existing code.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Programming with Patterns Jeremy Cronan Alliance Safety Council
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Patterns in programming
Design Patterns: MORE Examples
Chapter 10 Design Patterns.
Behavioral Design Patterns
Design Patterns Based on slides provided by Abbie Jarrett
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
GoF Design Patterns (Ch. 26)
08/15/09 Design Patterns James Brucker.
CSC 480 Software Engineering
Singleton Pattern Pattern Name: Singleton Pattern Context
Presented by Igor Ivković
Presentation transcript:

Patterns in programming1

2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems –Note: Actually more important to understand situations than specific solutions But knowing solutions also required

Patterns in programming3 Pattern categories Architectural patterns –High level: Affects a whole application or a major part of an application. Design patterns –Intermediate level: Affects a few classes. Idioms –Low level: Affects a single class or part of a single class. –Closing a resource like a file or network connection try { … use resource … } finally { close resource } –Testing an expected exception in JUnit try { methodThatThrowsExceptions(); fail(…); } catch (SomeException ex) { /* ignore */ }

Patterns in programming4 Design pattern categories Creational patterns –How to create objects when creation requires decisions. Structural patterns –How classes and objects are composed to form larger structures. Behavioral patterns –Algorithms and assignment of responsibilities between objects.

Patterns in programming5 Creational patterns Factory method –Define an interface for creating an object, but let subclasses decide which class to instantiate. Singleton –Ensures a class has only one instance, and provides a global point of access to it. Object pool –Manages the reuse of objects when a type of object is expensive to create or only a limited number of objects can be created.

Patterns in programming6 Factory method Idea –Can return an instance of the specified class, or one of its subclasses A constructor can only “return” an instance of its own class –Does not have to construct a new object A constructor must create a new object –Can have any name A constructor must have the same name as the class Naming conventions createXxx()‏ getXxx()‏ Java API usage –JDBC Connection DriverManager.getConnection(…)‏ Statement Connection.createStatement(…)‏ ResultSet Statement.executeQuery(…)‏ –javax.net.SocketFactory / javax.net.ssl.SSLSocketFactory

Patterns in programming7 Singleton Idea –Exactly 1 instance of a class –A single global point of access for all clients Example class A { private static A INSTANCE = new A(); // eager initialization public static A getInstance() { return INSTANCE; } private A() { … } // more methods } Variations –Lazy initialization The instance is not created before it is needed. If it is never needed, it does not use resources. getInstance method might need synchronization. –Protected constructor Makes it possible to subclass the singleton class

Patterns in programming8 Object pool Idea –Reuse of objects that are expensive to create Usages –Thread pool –Connection pool Connections to databases, networks, etc. Related patterns –Singleton The pool class is often a singleton, since the whole idea is to make all parts of an application share a single pool of objects. Java API usage –Thread pool Java.util.concurrent.Executors

Patterns in programming9 Structural patterns Composite –Compose objects into tree structures. Decorator –Attach additional responsibilities to an object dynamically.

Patterns in programming10 Composite Idea –Recursively build composite objects from other objects. –Objects are composed at runtime using addXx methods to add a new sub-object removeXx methods to remove a sub-object Object diagram looks like a tree –Composite element as root and interior nodes –Non-composite elements as leafs Usages –Swing / AWT Object of class Container contains other visual components. JButton extends Container, i.e. we can add JButton’s on a JButton!

Patterns in programming11 Decorator Idea –Enhancing the service provided to the user of the class. –Extend the functionality of an object – transparent to its clients. Also known as –Wrapper Usages –Java.io BufferedReader decorates another reader –Java.util.Collections Synchronized wrappers Immutable wrappers ReverseOrder on natural order / comparator

Patterns in programming12 Behavioral patterns Observer –Define a one-to-many dependency between object so that when one object changes state, all its dependents are notified (and updated) automatically. Strategy –Define a family of algorithms, encapsulate each one, and make them interchangeable at runtime. Template method –Define a skeleton of an algorithm, deferring some steps to subclasses.

Patterns in programming13 Observer Idea –Decouples otherwise dependent classes. –Observers register with an observable class. –When something happens (like change of state) in the observable, all observers are notified. Usage –Swing / AWT Visual components send events to listeners. –JavaBeans Components sends property change events to listeners. –Logging Logger sends messages to handlers. Variations –Data is sent with the notification. –Notification does not contain data. Data is fetched from the observable using get-methods.

Patterns in programming14 Strategy Idea –Encapsulate (part of) an algorithm in an interface. –The algorithm may be changed at runtime. Usages –Logging java.util.logging Handlers have a Filter and a Formatter Handler.setFilter(Filter filter)‏ Handler.setFormatter(Formatter formatter)‏ Related patterns –Template method Alternative to strategy

Patterns in programming15 Template method Idea –Encapsulate part of an algorithm in an abstract method implemented by subclasses. Example abstract class SuperClass { abstract protected T doPartOfSomething(); public void doSomething() { …doPartOfSomething(); … } } class SubClass1 extends SuperClass { protected T doPartOfSomething() { … } } Usage –Often used in frameworks Users of the framework are supposed to extend the abstract classes. Related patterns –Strategy Modifies the logic / algorithm of individual objects (specified at run-time)‏ Template method modifies the logic of an entire class (specified at compile-time)‏

Patterns in programming16 References Gamma et al. Design Patterns, Addison Wesley 1994 –First book on design patterns. –Examples in C++ and Smalltalk. –Written by the so-called “Gang of Four” (GoF)‏ Buschmann et al. Pattern-Oriented Software Architecture, Volume 1, Wiley 1996 Mark Grand Design Patterns in Java, Volume 1 2 nd edition, Wiley 2002 Freeman & Freeman Head first design patterns, O’Reilly 2004 Joshua Kerievsky Refactoring to Patterns, Addison Wesley Professional –Includes a catalog of patterns atterncatalog.htm atterncatalog.htm