Patterns COM379 University of Sunderland James Malone.

Slides:



Advertisements
Similar presentations
Chapter 6: Using Design Patterns
Advertisements

Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Design Patterns CS is not simply about programming
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Design Patterns – Part 3 Sources:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Chapter 22 Object-Oriented Design
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns.
Draw a small class diagram (2 or 3 classes) for each of the following examples: For Netflixs, you have to keep track of the movie details AND the copies.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
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.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Structural Design Patterns
ECE450S – Software Engineering II
CSE 240 Lecture 9. © Lethbridge/Laganière 2001 Chapter 5: Modelling with classes2 Overview Chapter 6 Start on Chapter 7 if time permits.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Introduction to Design Patterns Part 1. © Lethbridge/Laganière 2001 Chapter 6: Using design patterns2 Patterns - Architectural Architectural Patterns:
Title Carolina First Steering Committee October 9, 2010 Online Voting System Design Yinpeng Li and Tian Cao May 3, 2011.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Reuse Earlier we have covered the re-usable Architectural Styles as design patterns for High-Level Design. At mid-level and low-level, design patterns.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Subtopics: 1. Frameworks :Reusable systems 2. Design Patterns 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Design Patterns (Chapter 6 of Text Book – Study just 8)
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Common Design Patterns
Introduction to Design Patterns
Behavioral Design Patterns
object oriented Principles of software design
Chapter 6: Using Design Patterns
Introduction to Design Patterns Part 1
Frameworks And Patterns
Informatics 122 Software Design II
Design Patterns – Part 3 Sources:
Structural Patterns: Adapter and Bridge
Informatics 122 Software Design II
Presentation transcript:

Patterns COM379 University of Sunderland James Malone

Resources Gamma, et al., 1995, Design Patterns, Addison-Wesley. (a free download, and the code is also available!)

Introduction to Patterns The recurring aspects of designs are called design patterns. –A pattern is the outline of a reusable solution to a general problem encountered in a particular context –Many of them have been systematically documented for all software developers to use –A good pattern should Be as general as possible Contain a solution that has been proven to effectively solve the problem in the indicated context. Studying patterns is an effective way to learn from the experience of others

Pattern description Context: The general situation in which the pattern applies Problem: A short sentence or two raising the main difficulty. Forces: The issues or concerns to consider when solving the problem Solution: The recommended way to solve the problem in the given context. —‘to balance the forces’ Antipatterns: (Optional) Solutions that are inferior or do not work in this context. Related patterns: (Optional) Patterns that are similar to this pattern. References: Who developed or inspired the pattern.

Creational Patterns Factory Method Singleton

Factory Method You need an object, but which subtype of the object needs to be decided later. This can be a particularly problem during initialization, as it means the file/object loader has to know every possible subclass. The solution to this is called the ‘factory method’ or ‘object factory’. Positives: –Flexibility –Can connect parallel class hierarchies Negatives: –Forces subclassing of the base class

Example Factory Method A database class wants an Employ object but does not need or want details of all possible subclasses of Employ EngineerDirector Employ FactoryDatabase getClass

Singleton (Creation or Non-Creation Pattern?) Ensures a class has a globally accessible unique instance. The use of a public constructor cannot guarantee that no more than one instance will be created. The singleton instance must also be accessible to all classes that require it Sometimes you need a class with only one instance, and sometimes you need global access to a class. Positives: –Avoids the need to link objects at system initialization. –Sometimes you need it badly. –Supports persistence. The local copy can be a proxy for a copy kept on disk. –Reduced name space pollution. –Can be modified to support multiple instances using a Map. Negatives: –Has to be carefully implemented. –Sometimes it’s just not useful.

Singleton –Solution: Company theCompany Company «private» getInstance if (theCompany==null) theCompany= new Company(); return theCompany; «Singleton» theInstance getInstance getInstance method

Structural Patterns Adapter Flyweight Proxy MVC (discussed earlier)

Adapter (or Wrapper) This pattern is basically how ‘glue code’ is written to convert one interface to another, allowing unrelated classes to work together. Two approaches: –Inheritance (class adapter)—the adapter inherits from the nonconforming one and adds the methods we need to make the new derived class match the desired interface. –Object composition by reference to implementation (object adapter)—the adapter contains an instance of the class providing the function. The reference can be to a common superclass, allowing the adapter to adapt multiple subclasses (like a wrapper around the old class). Positives –Already used in Java Negatives –A class adapter creates a concrete subclass and cannot handle subclasses of the original class. –An object adapter makes it hard to override the object subclass behavior.

The Adapter Pattern –Context: You are building an inheritance hierarchy and want to incorporate it into an existing class. The reused class is also often already part of its own inheritance hierarchy. –Problem: How to obtain the power of polymorphism when reusing a class whose methods –have the same function –but not the same signature as the other methods in the hierarchy? –Forces: You do not have access to multiple inheritance or you do not want to use it.

Adapter –Solution: «Adaptee» adaptedMethod «Superclass» polymorphicMethod «Adapter» polymorphicMethod() return adaptee.adaptedMethod(); { }

Adapter Example: ImportedCircle calcArea Shape area SquareCircle Client

Flyweight Use sharing to support large numbers of fine-grained objects efficiently. E.g., the characters and other images on a screen need not individually contain all the data needed to draw them. Similarly, in C#, where there are no primitive types, the overhead to generate individual numbers as objects, can be overwhelming. Approach: –Create sharable instances of a class that are allocated when needed. Either create a basic pool at initialization or as needed. Note that this approach can be used for complex objects as well. Positives: –Avoids overhead. –Saves storage. –Useful with databases. –Often combined with the composite pattern. Negatives: –Hard to do in Java.

Flyweight Example Suppose we want to draw a small folder icon with a name under it for each person in a an organization. If this is a large organization, there could be a large number of such icons, but they are actually all the same graphical image. Even if we have two icons, one for “is Selected” and one for “not Selected” the number of different icons is small. In such a system, having an icon object for each person, with its own coordinates, name and selected state is a waste of resources. Instead, we’ll create a FolderFactory that returns either the selected or the unselected folder drawing class, but does not create additional instances once one of each has been created. Since this is such a simple case, we just create them both at the outset and then return one or the other:

Proxy Allows you to represent a complex object with a simpler or more accessible one. May support copy on write for large objects that may or may not change. There are a number of useful applications. Approach: –Define a proxy class with the same method signatures as the complex class. Those methods provide the proxy access to the complex class object. Positives: –Provides remote access, virtual access, protected access, or smart access. Negatives: –Non-trivial to implement. –Overhead and delayed access.

The Proxy Pattern –Context: Often, it is time-consuming and complicated to create instances of a class (heavyweight classes). There is a time delay and a complex mechanism involved in creating the object in memory –Problem: How to reduce the need to create instances of a heavyweight class? –Forces: We want all the objects in a domain model to be available for programs to use when they execute a system’s various responsibilities. It is also important for many objects to persist from run to run of the same program

Proxy –Solution: «interface» «ClassIF» ******* «Client» «HeavyWeight»«Proxy»

Proxy Example: «interface» ListIF The list elements will be loaded into local memory only when needed. ListProxyPersistentList «interface» Student PersistentStudentStudentProxy

Model-View-Controller (MVC) In the MVC pattern, the model and the view are strictly kept apart from each other Their interaction is mediated by the controller component: the glue that binds them together MVC consists of three kinds of objects: –Model – the application object –View – UI (screen presentation) –Controller – defines the way the UI reacts to user inputs

MVC – Class Diagram controller view model

Behavioral Patterns Command Iterator Observer Template Method

Command Encapsulate a request as an object. Supports queueing, logging, and undo operations. The Java 1.1 event model implements this. Approach: –Encapsulate the request as an object. Positives: –Hides the private details of the request. –Commands are first class objects. –Allows priority queueing. –Supports a macro or scripting language approach. Negatives: –Overhead –Lots of little classes

Iterator An iterator is an object that supports the traversal of a collection. The interface between an algorithm and a collection should be an iterator. – you have seen this already in banking example Approach: –See the Iterator interface. Consider implementing by using inner classes. Positives: –Raises the level of abstraction without a major performance penalty. Negatives: –There are some esoteric traps to avoid if the collection is simultaneously being modified. –Protected access –Typecasting is necessary in Java

Observer Supports separation between model and view. Approach: see Swing – you will learn about observable classes Positives: –Decouples observers from models Negatives: –Lack of coordination between observers. Who has responsibility? –What sort of messages should be sent? Adapter classes may be required.

Observer –Solution:

Template Method Define the skeleton of an algorithm, but defer details to subclasses. Fundamental to Java. In other words, if your baseclass is an abstract class you are using the template pattern Approach: –Abstract classes provide this capability. Positives: –Normal part of OO programming. Negatives: –Not as powerful as in C++.