Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
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 Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
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.
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 Trends and Case Study John Hurst June 2005.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
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.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
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.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
GoF: Document Editor Example Rebecca Miller-Webster.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450S – Software Engineering II
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
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.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
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.
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:
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Patterns in programming
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Introduction to Design Patterns
object oriented Principles of software design
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Presented by Igor Ivković
Presentation transcript:

Patterns in programming 1

What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for solving a particular problem. Thinking in terms of design patterns allows you to formulate a high-level solution that is independent of the implementation details.” MSDN Design Patterns Patterns in programming2

Pattern categories Architectural patterns High level Affects the whole application or a major part of an application Examples: Layers, Model-View-Controller (MVC), Model-View-ViewModel (MVVM) Design patterns Intermediate level Affects a few classes Idioms Low Level Affects a single class or part of a single class Example: Unit testing an expected exception Try { … use resource … Assert.Fail(); } Catch (ExpectedException) { /* ignore */ } Source: Buschmann et al. Pattern-Oriented Software Architecture, Wiley 1996, page Patterns in programming3

Design pattern categories Creational patterns How to create object when creation requires decisions Structural patterns How classes and object are composed to form larger structures Behavioral patterns Algorithms and assignment of responsibilities between objects Source: Gamma (GoF) et. al. Design Patterns, Elements of Reusable Object-Oriented Software, Addison Wesley, 1995 Patterns in programming4

Creational patterns Singleton Ensure that a class has only one instance, and provide a global point of access to it. Factory method Define a static method go create (or get) objects Object pool Manages the reuse of objects Patterns in programming5

Singleton Idea Exactly 1 instance of a class A single global point of access for all clients (users of the object) Example Class A { private static A instance = new A(); private A() { /* private constructor */ } public static A Instance { get { return instance; }} } Usage Connections to database You only need one in your application Classes without state Example: SingletonExampleComparer Patterns in programming6

Singleton, eager vs. lazy instantiation Eager instantiation The one-and-only instance is created early Lazy instantiation The one-and-only instance is create late On the first call to get Pros Saves time if the instance takes a lot of time to create, and is unlikely to be used Saves memory if the instances uses a lot of memory, and is unlikely to be used Cons Requires synchronization to be thread safe Spends time if the instances is used a lot (more than once) Patterns in programming7

Static 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 Dogma idea: No public constructors, only factory methods! Naming CreateXxx(…) GetXxx(…) Usage System.Diagnostics.StopWatch.StartNew() System.Net.WebRequest Example: FactoryWebRequestExample Example: FactoryGetOrCreate Patterns in programming8

Object pool Idea Reuse objects which are expensive to create Expensive = takes a lot of time Usages Thread pool Connection pool Connections to databases, and other servers Related patterns Singleton The pool class itself is often a singleton, since the idea is to make all parts of the application share a single pool of objects Patterns in programming9

Structural patterns Decorator Attach additional responsibilities to an object, dynamically Composite Compose objects into tree structure Patterns in programming10

Decorator Idea Enhancing the service provided to the user of the class Extend the functionality of an object – transparent to its users Also known as Wrapper Usages System.Collections.ObjectModel.ReadOnlyCollection Implements IList + aggregates an IList object System.IO.BufferedStream Implements Stream + aggregates a Stream object Examples: DecoratedCoffee + DecoratedStudent Patterns in programming11

Composite Idea Recursively build composite objects from other objects. Objects are composed at runtime using AddXx() / Append() methods to add new sub-objects RemoveXx methods to remove sub-objects Usage StringBuilder Append(str) WPF (Windows Presentation Foundation) Normally composed using XAML Example (very simple): SimpleBrowserAsync (concurrency) Patterns in programming12

Behavioral patterns: it’s about algorithms Iterator Provide a way to access the elements of a collection sequentially without exposing its underlying representation Observer Define a one-to-many dependency between objects, so that when one object changes states, all its dependents are notified. 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

Iterator Idea Being able to visit all the elements in a collections, without knowing the internal structure of the collection. Usage IEnumerable + IEnumerator used a lot on all kinds of collections in the C# API Patterns in programming14

Observer Idea Decouples otherwise dependent classes. Observers register with an observable class. When something happens (like a change of state) in the observable class, all observers are notified Usage GUI frameworks Buttons (etc.) are observable, event handlers are observers Event handling in general Event handlers (observers) register to listen for events Patterns in programming15

Strategy Idea Encapsulate (part of) and algorithm in an interface or delegate. The (part of) algorithm may be changed at runtime Usages List.Sort(IComparer comparer) However the Comparison cannot be changed at runtime List.Sort(Comparison comp) However the Comparison cannot be changed at runtime Example StrategyFindSmallest Related pattern Template method and strategy are often alternatives Patterns in programming16

Template method Idea Encapsulates part of an algorithm in an abstract method implemented by sub-classes An abstract base class has a template method A method in which one (or more) steps are call to abstract methods The abstract method is implemented in a sub-class The base class method calls the sub-class method “The Hollywood Principle” Don’t call us. We will call you Usage Often used in frameworks Example TemplateMethodHotDrink TemplateMethodTcpServer Patterns in programming17

References and further readings 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 Freeman & Freeman Head First Design Patterns, O’Reilly 2004 Kerievsky Refactoring to Patterns, Addison Wesley 2005 Dofactory.NET design patterns Very short descriptions Codeproject.com Design Patterns More elaborate descriptions atterns Patterns in programming18