Design Patterns. CSC207 Fall 2017

Similar documents
Design Patterns. CSC207 Winter 2017

Design Patterns. CSC207 Fall 2017

Using Design Patterns in Java Application Development

Tuesday, October 4. Announcements

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

DESIGN PATTERN - INTERVIEW QUESTIONS

Object-Oriented Design

Design Patterns. Gunnar Gotshalks A4-1

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Advanced Topics in Software Engineering (02265) Ekkart Kindler

Lecture 13: Design Patterns

Topics in Object-Oriented Design Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design Patterns. An introduction

SOFTWARE PATTERNS. Joseph Bonello

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Applying the Observer Design Pattern

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Lecture Material. Design Patterns. Visitor Client-Server Factory Singleton

Cocoa Design Patterns. Erik M. Buck October 17, 2009

What is Design Patterns?

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Design Patterns. Definition of a Design Pattern

Object-Oriented Design & Patterns By Cay S. Horstmann READ ONLINE

VOL. 4, NO. 12, December 2014 ISSN ARPN Journal of Science and Technology All rights reserved.

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

An Introduction to Patterns

1 Software Architecture

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Applying the Factory Method Design Pattern

Object-Oriented Design

As a programmer, you know how easy it can be to get lost in the details

Application Architectures, Design Patterns

2.1 Design Patterns and Architecture (continued)

CHAPTER 6: CREATIONAL DESIGN PATTERNS

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

CPSC 310 Software Engineering. Lecture 11. Design Patterns

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Review Software Engineering October, 7, Adrian Iftene

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

An Introduction to Patterns

Design Patterns. James Brucker

2.1 Design Patterns and Architecture (continued)

Inheritance. EEC 521: Software Engineering. Dealing with Change. Polymorphism. Software Design. Changing requirements Code needs to be flexible

Crash course on design patterns

Programmazione. Prof. Marco Bertini

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

Design patterns. OOD Lecture 6

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

What is Design Patterns?

Design Patterns (DP) In the beginning. It s not a course about DP (just a little) A lot of good design and efficient implementation is based on DP

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Introduction to Testing and Maintainable code

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Patterns of learning

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

The Object Recursion Pattern

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

Applying the Decorator Design Pattern

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Work groups meeting 3

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

25.1 Introduction Façade Design Pattern Identification Problem Structure Participants...

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

Responsibilities. Using several specific design principles to guide OO design decisions.

A Rapid Overview of UML

11/12/12. Objectives DESIGN PATTERNS. Design Pattern. Defined Design Patterns. Applying Design Patterns. Motivating Example

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

Second Midterm Review

CPS122 Lecture: Design Patterns Last revised March 20, 2012

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Object Oriented Programming. Michał Bereta

The GoF Design Patterns Reference

The Strategy Pattern Design Principle: Design Principle: Design Principle:

The Software Design Process. CSCE 315 Programming Studio, Fall 2017 Tanzir Ahmed

What is Design Patterns?

L18.1 Introduction... 2

developer.* The Independent Magazine for Software Professionals Factory Chain: A Design Pattern for Factories with Generics by Hugo Troche

Patterns for Decoupling

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Chapter 12 (revised by JAS)

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

Design Patterns. Claus Jensen

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Software Engineering I (02161)


CPS122 Lecture: Design Patterns Last revised March 7, 2017

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Transcription:

Design Patterns CSC207 Fall 2017

Design Patterns A design pattern is a general description of the solution to a well-established problem using an arrangement of classes and objects. Patterns describe the shape of code rather than the details. They re a means of communicating design ideas. They are not specific to any one programming language. You ll learn about lots of patterns in CSC301 (Introduction to Software Engineering) and CSC302 (Engineering Large Software Systems).

Reminder: loose coupling, high cohesion These are two goals of object-oriented design. Coupling: the interdependencies between objects. The fewer couplings the better, because that way we can test and modify each piece independently. Cohesion: how strongly related the parts are inside a class. High cohesion means that a class does one job, and does it well. If a class has low cohesion, then an object has parts that don t relate to each other. Design patterns are often applied to decrease coupling and increase cohesion.

Gang of Four First codified by the Gang of Four in 1995 - Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Original Gang of Four book described 23 patterns - More have been added - Other authors have written books

Book provides an overview of: Name Problem: when to use the pattern motivation: sample application scenario applicability: guidelines for when your code needs this pattern Solution: structure: UML Class Diagram of generic solution participants: description of the basic classes involved in generic solution collaborations: describes the relationships and collaborations among the generic solution participants sample code Consequences, Known Uses, Related Patterns, Anti-patterns

Iterator Design Pattern Context A container/collection object. Problem Want a way to iterate over the elements of the container. Want to have multiple, independent iterators over the elements of the container. Do not want to expose the underlying representation: should not reveal how the elements are stored.

Iterator Design Pattern: Java

Observer Design Pattern Problem: Need to maintain consistency between related objects. Two aspects, one dependent on the other. An object should be able to notify other objects without making assumptions about who these objects are.

Observer: Java Implementation

Observer: Example in Java

Strategy Design Pattern Problem: multiple classes that differ only in their behaviour (for example, use different versions of an algorithm) but the various algorithms should not be implemented within the class want the implementation of the class to be independent of a particular implementation of an algorithm the algorithms could be used by other classes, in a different context want to decouple separate the implementation of the class from the implementations of the algorithms

Strategy: Standard Solution

Example: without the Strategy pattern

Example: using the Strategy pattern

Model-View-Controller (MVC) Design Pattern Model: data, rules that govern access to the data, interactions that update the data Often approximates a real-world process View: presentation of that data that is responsive to changes in the model With a Push Model, the View receives change notifications from the Model With a Pull Model, the View is responsible for calling the model and retrieving the most current data.

Controller: Interaction between User and View are translated by the Controller into actions for the Model to perform. The controller can call different aspects of the View (close one window and/or open another) Graphical User Interfaces (GUI) often comprise the View, where the Controller is the non-visual front end (e.g., what the buttons interact with when they are clicked), and the Model is the data which the GUI displays and with which the User interacts via the Controller.

MVC Pattern: General Structure From: https://www.tonymarston.net/php-mysql/modelview-controller-01.png

MVC: What s happening inside?

Code for this example can be found here: https://www.tutorialspoint.com/design_pattern/ mvc_pattern.htm

Dependency in Object Oriented Programming A dependency relationship between two classes (also called using relationship) means that any change to the second class will change the functionality of the first. For example: class AddressBook depends on class Contact because AddressBook contains instances of Contact. Other examples of dependencies: loggers, handlers, listeners

Dependency Injection Design Pattern Problem: We are writing a class, and we need to assign values to the instance variables, but we don t want to hard-code the types of the values. Instead, we want to allow subclasses as well.

Dependencies in Java Using operator new inside the first class can create an instance of a second class that cannot be used nor tested independently. This is called a hard dependency. We often want to use and test the second class independently from the first and avoid hard dependencies. For example: public class MyClass { private Student student; } public MyClass(Student student){ this.student = student; }

Dependency Injection Example

Dependency Injection Pattern Exercise Consider our previous example of class Person and its subclass Student. Create a class called Email that contains instance variables of type String called: to, from, cc, subject, and message. It should also contain methods printtoscreen and printtofile methods. Class Person should have the ability to send an Email object to another Person object. But that will require our first instance of Person to obtain the second Person object s name or email address.

Without Dependency Injection: The code for creating and sending an email will have to be inside class Person. Any change to the Email class will impact the code in class Person.

With Dependency Injection: We can create a class called EmailService that gathers the information required to compose and send Email objects. The constructor for EmailService can take in the entire Person sender object and Person receiver object and retrieve the values of their respective name and email fields, in order to create a new instance of Email. Any change to the Email class will be contained by the EmailService and shielded from class Person.

Factory Design Pattern Problem: A class cannot anticipate the class of the object it must create. A class wants its subclasses to specify the object it creates. Classes delegate responsibility to one of several helper subclasses and you want to localize knowledge of which helper subclass is the delegate.

When to use Factory Method Design Pattern One class wants to interact with many possible related objects. We want to obscure the creation process for these related objects. At a later date, we might want to change the types of the objects we are creating.

Factory Method: A Standard Solution

Factory Pattern: An example

Code for this example can be found here: https://www.tutorialspoint.com/design_pattern/ factory_pattern.htm

We can easily create new subclasses of Shape and use the ShapeFactory to instantiate them without changing the rest of the code.

Factory Pattern: Another Example

For implementation of the Document Example, see: http://stg-tud.github.io/eise/ws15-se-18- Factory_Method_and_Abstract_Factory_Design_Pat tern.pdf