Patterns in programming

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.
Plab – Tirgul 12 Design Patterns
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 ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 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.
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.
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.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
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.
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.
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 SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
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.
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Presented by FACADE PATTERN
Design Patterns: MORE Examples
Abstract Factory Pattern
Unit II-Chapter No. : 5- design Patterns
Factory Method Pattern
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Introduction to Design Patterns
Design Patterns.
Design Patterns Based on slides provided by Abbie Jarrett
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Factory Method Pattern
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Introduction to Design Patterns Part 1
08/15/09 Design Patterns James Brucker.
Object Oriented Design Patterns - Creational Patterns
Chapter 22 Object-Oriented Design
DESIGN PATTERNS : Introduction
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Presented by Igor Ivković
5. Strategy, Singleton Patterns
Presentation transcript:

Patterns in programming Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson What are patterns? Summarize of god programming practice. A solution of a design problem A pattern is described by: It has a name It define the problem It describe a design solution (free from programming language) Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 http://msdn.microsoft.com/en-us/library/ff650734.aspx Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson Why have Patterns? Provide proved solutions to a problem Do not reinvent the wheel A common terminology between IT professionals Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 … } Catch (ExpectedException) { /* ignore */ } Source: Buschmann et al. Pattern-Oriented Software Architecture, Wiley 1996, page 11-15 Patterns in programming/Anders Børjesson

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 programming/Anders Børjesson

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

Patterns in programming/Anders Børjesson 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 Log4Net: LogManager.GetLogger(someName) Example: loggingFirst Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 really need one in your application Classes without state Example: SingletonExampleComparer Patterns in programming/Anders Børjesson

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 programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 programming/Anders Børjesson

Patterns in programming/Anders Børjesson Structural patterns Decorator Attach additional responsibilities to an object, dynamically Composite Compose objects into tree structure Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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<T> Implements IList<T> + aggregates an IList<T> object http://msdn.microsoft.com/en-us/library/ms132476(v=vs.110).aspx System.IO.BufferedStream Implements Stream + aggregates a Stream object http://msdn.microsoft.com/en-us/library/system.io.bufferedstream(v=vs.110).aspx Examples: DecoratedCoffee + DecoratedStudent Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 programming/Anders Børjesson

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 programming/Anders Børjesson

Patterns in programming/Anders Børjesson Iterator Idea Being able to visit all the elements in a collections, without knowing the internal structure of the collection. Usage IEnumerable<T> + IEnumerator<T> used a lot on all kinds of collections in the C# API Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 Log4Net A logger (observer) has a number of Appenders (observable) Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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<T> comparer) However the Comparison cannot be changed at runtime List.Sort(Comparison comp) Log4Net Each Appender has a Layout stragety Example StrategyFindSmallest Related pattern Template method and strategy are often alternatives Patterns in programming/Anders Børjesson

Patterns in programming/Anders Børjesson 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 programming/Anders Børjesson

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 http://www.dofactory.com/Patterns/Patterns.aspx Codeproject.com Design Patterns More elaborate descriptions http://www.codeproject.com/KB/architecture/#Design+P atterns Patterns in programming/Anders Børjesson