Design Patterns. OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

Design Patterns Pepper. Find Patterns Gang of Four created 23 Siemens published another good set x
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.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Object-Oriented Analysis and Design
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.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Games Development 2 Entity / Architecture Review CO3301 Week
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CS 210 Introduction to Design Patterns September 28 th, 2006.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
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.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Programming in C# Observer Design Pattern
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
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.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
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:
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
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.
Programming with Patterns Jeremy Cronan Alliance Safety Council
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Copyright © Jim Fawcett Spring 2017
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Common Design Patterns
Low Budget Productions, LLC
Observer Design Pattern
How to be a Good Developer
Design Patterns with C# (and Food!)
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Factory Method, Abstract Factory, and More
How to be a Good Developer
Multiuser Protection and the Mediator Pattern
Software Engineering Lecture 7 - Design Patterns
Design Patterns in Game Design
Introduction to Behavioral Patterns (1)
OO Design Patterns - Decorator
Decorator Pattern Richard Gesick.
Review: Design Pattern Structure
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
CS 325: Software Engineering
Presentation transcript:

Design Patterns

OO-Concepts Don’t rewrite code Encapsulation Inheritance Write flexible code

Design Patterns Time to level up! Necessary when the projects are large Course projects (possibly including CSE442) – Small projects – Requirements don’t change – No design patterns needed – Can get away without proper OO usage Large projects with poor design – Do you want to refactor 10,000 lines of code? – 100,000 lines?

Caution Design patterns should be used in moderation This is a hammer – Not all problems are nails

Let’s learn these Singleton Observer pattern Decorator Pattern Factory Pattern //Command pattern Adapter Pattern State pattern MVC

Singleton There can be only one! When your codebase needs to share a single object (not a single class) Private constructor – What? Controversial – Introduces global state

Singleton

No need to pass the settings in every constructor and function call Can cause issues – What if I want multiple sets of settings concurrently? – Panic – Then run multiple instances of the program and let the OS take care of it

Lazy Initialization Don’t create an object until it’s about to be used If an object is never used, it’s never created Can prevent multiple instantiations Can greatly improve efficiency At least spreads out the computation – If that’s what you’re into

Observer Pattern A single object has many other objects that need to know when it updates On an update, broadcast to all concerned objects But how? Thermostat temp sensor – Furnace – AC – Mobile app

Observer Pattern

Loose coupling Observers can be registered and unregistered during runtime – Flexible – Dynamic No need to hardcode and notify every possible object that might ever be concerned Subject is only coded once – This is a big deal!

Scenario Inspired from Head First Design PatternsHead First Design Patterns Need to compute the cost of a burger Condiments cost extra

Scenario How do we adjust to added requirement?

Scenario Customer orders double cheese and triple bacon

Decorator Pattern Wrapper classes Add features by adding wrapper classes Outer object interacts with the world Original/inner object is hidden Especially useful when original class is in a library and lacks needed funtionality

Decorator Pattern

Food Item cost() Food Item cost() Condiment -Food Item cost() Condiment -Food Item cost() Burger cost() Burger cost() Relish cost() Relish cost() Cheese cost() Cheese cost() Ketchup cost() Ketchup cost() Bacon cost() Bacon cost()

Scenario Need to create an enemy (abstract class) Type of enemy (concrete class) depends on: – Player’s level – Player’s location – Some randomness Enemies are created throughout the codebase What do you do?

Factory Pattern A class that makes objects – Don’t all classes do that? Factory pattern – Create objects of an abstract type – No concern about the concrete class that’s instantiated

Command pattern AI doesn’t push the a button AI and human mixed games

Command pattern AI doesn’t push the a button AI and human mixed games

Command pattern AI doesn’t push the a button AI and human mixed games

Adapter Pattern Interface between 2 different protocols Don’t modify the source code of either Often needed when combining code bases – Libraries Your code uses a single interface Adapters extend the interface and make external function calls

Library we need expects this: Our code is built around this:

Adapter Pattern

State Pattern CSE116 – Make a program without using “if” CSE396 – DFA’s Delegate functionality to a state object Functionality changes as state chnges

State Pattern

MVC Model – Where all the action View – What the user sees – Outputs Controller – How the user interacts – Inputs