TDDB84. Summary & wrap-up & some tips & some design patterns & some other stuff. tisdag 20 oktober 15

Similar documents
TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

TDDB84: Lecture 5. Singleton, Builder, Proxy, Mediator. fredag 27 september 13

TDDB84. Lecture 2. fredag 6 september 13

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

Design Patterns: Composite, Memento, Template Method, Decorator, Chain of Responsibility, Interpreter

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

Writing your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO

TDDB84 Design Patterns Lecture 06

Design Patterns Reid Holmes

Design Patterns: Template Method, Strategy, State, Bridge

Tecniche di Progettazione: Design Patterns

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Design Patterns. State. Oliver Haase

CSE 70 Final Exam Fall 2009

CS342: Software Design. November 21, 2017

SDC Design patterns GoF

Exam in TDDB84: Design Patterns,

MORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011

An Introduction to Patterns

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

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

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

FACADE & ADAPTER CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 7 09/13/2011

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

Applying the Factory Method Design Pattern

Design Pattern and Software Architecture: IV. Design Pattern

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

Object-Oriented Oriented Programming

Factories, Builders and Singletons. Steven R. Bagley

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

Design Patterns (Part 2) CSCE Lecture 18-10/25/2016 (Partially adapted from Head First Design Patterns by Freeman, Bates, Sierra, and Robson)

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Abstract Classes and Interfaces

Design patterns. Jef De Smedt Beta VZW

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

Design Pattern- Creational pattern 2015

1 Software Architecture

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

The Design Patterns Matrix From Analysis to Implementation

Design Patterns Reid Holmes

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

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Object Oriented Paradigm

Introduction to Software Engineering: Object Design I Reuse & Patterns

Application Architectures, Design Patterns

Tecniche di Progettazione: Design Patterns

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Singleton Pattern Creational

Software Eningeering. Lecture 9 Design Patterns 2

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Software Design COSC 4353/6353 D R. R A J S I N G H

Topics in Object-Oriented Design Patterns

The GoF Design Patterns Reference

Design Patterns Lecture 2

g Baking with OO Goodness

A Reconnaissance on Design Patterns

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

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

Tuesday, October 4. Announcements

Design Patterns. (and anti-patterns)

The Singleton Pattern. Design Patterns In Java Bob Tarr

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

CSC207H: Software Design Lecture 6

DESIGN PATTERN - INTERVIEW QUESTIONS

Brief Note on Design Pattern

CHAPTER 6: CREATIONAL DESIGN PATTERNS

TDDB84 Design Patterns Lecture 05. Builder, Singleton, Proxy. pelab

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

COSC 3351 Software Design. Design Patterns Behavioral Patterns (II)

Applying the Observer Design Pattern

What is Design Patterns?

Software Design COSC 4353/6353 D R. R A J S I N G H

Design Patterns: State, Bridge, Visitor

Improve Your SystemVerilog OOP Skills

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

7. MULTITHREDED PROGRAMMING

Software Design Patterns. Aliaksei Syrel

Using Design Patterns in Java Application Development

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Programmazione. Prof. Marco Bertini

2.1 Design Patterns and Architecture (continued)

UNIT I Introduction to Design Patterns

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

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Midterm assessment - MAKEUP Fall 2010

Tecniche di Progettazione: Design Patterns

DESIGN PATTERNS FOR MERE MORTALS

Object-oriented Software Design Patterns

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Template Method, Iterator & Composite

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

What is Design Patterns?

Data Abstraction. Hwansoo Han

CIS 110: Introduction to computer programming

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

Transcription:

TDDB84 Summary & wrap-up & some tips & some design patterns & some other stuff

Agenda Course goals, expectations Writing papers: FAQ Design Patterns revisited Course summary Life after the course

Course goals Identify and explain software design principles and design patterns in existing object-oriented software. Lab 1, 2

Course goals Apply software design principles and design patterns when working with existing object-oriented software. Lab 1, 3

Course goals Describe the purpose and consequences of design patterns in object-oriented software. Lab 2, 3, final paper

Course goals Critically evaluate software design principles and design patterns in relationship to software qualities. Lab 3, final paper

Course goals Analyze the relationship between software design principles, design patterns, programming languages and application frameworks. Final paper

Expectations Learn about design patterns Learn when to use design patterns Learn about design patterns in real software Learn about the consequences of design patterns

Writing Papers, FAQ

Writing Papers, tips Be specific precise formal direct unapologetic What is complexity? Design complexity, cyclomatic complexity? This is not a popular science text. Refrain from wording that is ambiguous or less well defined ( forgiving, good, bad, easy ) Do not say something is very hard, use a reference and be precise about what exactly that reference says Do not say you will do something, or intends, be direct and state what you DO, or have DONE. You do not have to state someone made me write this, or Due to time constraints : you will ALWAYS have external constraints on your work, as does everyone else. Write about what you have done, do not make excuses for what you have not done.

Writing Papers, tips Use references the style of research papers as a guide the hourglass style your team Back up all claims with references Check your wording & justifications against academic publications, not previous years reports. Everything before the narrow part should lead to the waist, and everything after should relate to the waist. Don t let your paper be overly focused on background material: you need to introduce only that which is necessary for someone else of approximately your own backgrounds (before the course) to understand the core of your paper: your evaluation of a specific design pattern. Please do conduct a second round of reviews

Design Patterns revisited

Principles + Problem = Pattern

Principles = SOLID + Some more tips

Some more tips Encapsulate what varies Program to an interface not to an implementation Favor Composition over Inheritance Don t call us, we ll call you

Some more tips Depend upon abstractions. Do not depend upon concrete classes. Strive for loosely coupled designs between objects that interact Only talk to your friends

Some Design Patterns

You choose! Strategy Factory Method Decorator Template Method Composite Abstract Factory (+ Dependency Injection) Singleton (+ example in Ruby) Builder Adapter Bridge Observer Chain of Responsibility Memento Command

Strategy

Strategy Strategy Context Client

Strategy: Consequences + Can choose implementation of a strategy at run time + Eliminate hardcoded conditionals - Clients must be aware of different strategies - Communication required between context and strategies + Avoids excessive subclassing - Potentially many strategy objects created

Factory Method

Factory method (before)

Factory method (before) Pizza pizza = null; if (style.equals("ny")) { if (type.equals("cheese")) { pizza = new NYStyleCheesePizza(); } else if (type.equals("veggie")) { pizza = new NYStyleVeggiePizza(); } else if (type.equals("clam")) { pizza = new NYStyleClamPizza(); } else if (type.equals("pepperoni")) { pizza = new NYStylePepperoniPizza(); } } else if (style.equals("chicago")) { if (type.equals("cheese")) { pizza = new ChicagoStyleCheesePizza(); } else if (type.equals("veggie")) { pizza = new ChicagoStyleVeggiePizza(); } else if (type.equals("clam")) { pizza = new ChicagoStyleClamPizza(); } else if (type.equals("pepperoni")) { pizza = new ChicagoStylePepperoniPizza(); } } else { System.out.println("Error: invalid type of pizza"); return null; }

Factory method

Factory method + Decouples clients from specific dependency classes + Eliminates hardcoded conditionals + Connects parallel class hierarchies (NY*Pizza, Chicago*Pizza) - Requires keeping factory methods in sync with domain classes

Decorator

Component Decorator Beverage b = new Coffee(); b = new SweetenedBeverage(new SweetenedBeverage(b)); Decorator return 5+beverage.cost();

Decorator + Dynamically adds behavior to specific instances of a class + Customizes an abstract class without knowing the implementations - - Decorator objects are not of the same type as the objects it comprises May result in many small objects

Template Method

class Beverage { public: void preparerecipe(); void boilwater(); void pourincup(); }; class Coffee{ public: void preparerecipe(); void boilwater(); void brewcoffeegrinds(); void pourincup(); void addsugarandmilk(); }; class Tea{ public: void preparerecipe(); brew void boilwater(); void steepteabag(); addcondiments void pourincup(); void addlemon(); };

Template method: Consequences + Can isolate the extensions possible to an algorithm + Isolates clients from algorithm changes

Template method

Default implementations (hooks) public abstract class CaffeineBeverageWithHook { void preparerecipe() { boilwater(); brew(); pourincup(); if (customerwantscondiments()) { addcondiments(); } } boolean customerwantscondiments() { return true; } public class CoffeeWithHook extends CaffeineBeverageWithHook { [... ] public boolean customerwantscondiments() { return getuserinput().tolowercase().startswith("y"); } }

Duck[] ducks = { new Duck("Daffy", 8), new Duck("Dewey", 2), new Duck("Howard", 7), new Duck("Louie", 2), new Duck("Donald", 10), new Duck("Huey", 2) }; Template method? Arrays.sort(ducks, new Comparator<Duck>(){ @Override public int compare(duck arg0, Duck arg1) { return new Integer(arg1.weight).compareTo(arg0.weight); } No }); public class Duck implements Comparable<Duck> { String name; int weight; public Duck(String name, int weight) { this.name = name; this.weight = weight; } public String tostring() { return MessageFormat.format("{0} weighs {1}", name, weight); } } public int compareto(duck object) { return new Integer(this.weight).compareTo(object.weight); } Yes

// Java 8: With inline lambda Arrays.sort(ducks, (arg0, arg1) -> new Integer(arg1.weight).compareTo(arg0.weight));

Composite

Waiter Diner menu print() printmenu() Pizza menu Breakfast menu print() Clam Pizza Cheese Pizza Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs print() Dark roast Coffee Tea Espresso print()

Client Component Composite Leaf

Composite: consequences - + Allow us to treat composite objects and individual objects uniformly Creates composite classes that violate the principle of a single responsibility + Allows arbitrarily complex trees - The composite cannot rely on components to implement all methods

Abstract factory

Ingredients Pizza Store Clients Fresh Clam Mozzarella Cheese NY Thin Crust Dough I Want a Cheese Pizza Frozen Clam Parmesan Cheese Chicago Thick Crust Dough

Concrete Factory Abstract Factory Abstract Products

Clients

Abstract factory: consequences + Isolates clients from concrete dependencies + Makes interchanging families of products easier

Strategy When related classes only differ in behavior You need different variants of an algorithm An algorithm uses data the clients don t need to know A class uses conditionals for selecting behavior Behavioral Abstract Factory A system should be independent of how its products are created A system should be configured with one of multiple families of products You want to provide a class library of products, and only expose their interfaces Creational

Design principles Encapsulate what varies Program to an interface, not to an implementation Favor composition over inheritance Classes should be open for extension but closed for modification Don t call us, we ll call you

Dependency Injection

ICheesePizza -cheese <<ICheese> > StandardCheesePizza MozzarellaCheese ParmesanCheese IClamPizza -cheese -clam <<IClam>> FancyClamPizza FreshClam FrozenClam NYStyle ChicagoStyle Distinguished by namespaces in C#

DI: How? 1. Declare dependencies as constructor arguments of interface types 2. Register classes (components) in an Inversion-of-Control Container 3. Resolve the top-level object from an interface through the Container

1. Dependencies namespace DITest {! public class FancyClamPizza: IClamPizza! {!! private IClam clam;!! private ICheese cheese;!! public FancyClamPizza (IClam clam, ICheese cheese)!! {!!! this.clam = clam;!!! this.cheese = cheese;!! }!! public String ClamType() {!!! return String.Format("fancy {0}",clam);!! }!! public String Describe() {!!! return String.Format("fancy clam pizza with {0} and {1}",ClamType(), cheese);!! }! } }

2. Registration namespace DITest {! public class IoCInstaller: IWindsorInstaller! {!! public void Install(IWindsorContainer container, IConfigurationStore store)!! {!!! container.register(classes!!!.fromthisassembly()!!!.innamespace("ditest.nystyle")!!!.withserviceallinterfaces());!!! container.register (Classes!!!.FromThisAssembly()!!!.AllowMultipleMatches()!!!.InSameNamespaceAs<IoCInstaller>()!!!.WithServiceAllInterfaces());!! }! } } Castle Windsor, http://www.castleproject.org

3. Resolution!!! var container = new WindsorContainer();!!! // adds and configures all components using WindsorInstallers from executing assembly!!! container.install(fromassembly.this());!!! // instantiate and configure root component and all its dependencies and their dependencies and...!!! var p = container.resolve<icheesepizza>();!!! Console.WriteLine (p.describe ());

Singleton

public class Singleton { private static Singleton instance = new Singleton(); private String name; public String getname() { return name; } public static void someothermethod(){ System.out.println("Hi there!"); } What about static methods? private Singleton() { [... ] } } private Singleton() { try { // Very expensive job indeed Thread.sleep(100); } catch (InterruptedException e) { e.printstacktrace(); } name = Math.random() > 0.5? "Jonas" : "Anders"; } Our app takes forever to load

Thread t1 = new Thread(new StaticMethodInvocation()); Thread t2 = new Thread(new SingletonLookup()); t0 = System.nanoTime(); t1.start(); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printstacktrace(); } someothermethod invoked Singleton name: Anders Singleton lookup took 1 003 348 000 ns Static method invocation took 1 002 463 000 ns

How about now? private static Singleton instance; public static Singleton getinstance() { if (instance == null) { instance = new Singleton(); } return instance; } someothermethod invoked Static method invocation took 899 000 ns Singleton name: Anders Singleton lookup took 1 002 277 000 ns

What about threads?

private Singleton() { try { // Very expensive job indeed Thread.sleep(100); } catch (InterruptedException e) { e.printstacktrace(); } name = Math.random() > 0.5? "Jonas" : "Anders"; } private static final class SingletonLookup implements Runnable { @Override public void run() { System.out.println(MessageFormat.format("Singleton name: {0}", Singleton.getInstance().getName())); } } public static void main(string[] args) { Thread t1 = new Thread(new SingletonLookup()); Oops! Thread t2 = new Thread(new SingletonLookup()); t0 = System.nanoTime(); t1.start(); t2.start(); try { t1.join(); Singleton name: Anders t2.join(); Singleton name: Jonas } catch (InterruptedException e) { Singleton name after our threads have run: Anders // TODO Auto-generated catch block e.printstacktrace(); } System.out.println("Singleton name after our threads have run: "+Singleton.getInstance().getName()); }

public static synchronized Singleton getinstance() { if (instance == null) { instance = new Singleton(); } return instance; } Singleton name: Anders Singleton name: Anders Singleton lookup took 1 003 340 000 ns Singleton lookup took 1 003 286 000 ns Singleton name after our threads have run: Anders Woohoo!

Singleton as Enum public enum EnumSingleton { } INSTANCE; private String name; private int age; public String getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; }

Singletons in Ruby Object MetaClass A class A; end Class Singleton Class for foo class << foo; end Singleton Class for A class << A; end foo foo = A.new A class A; end

class A end a = A.new class << A def new raise "Illegal!" end end irb(main):014:0> a #<A:0x007f8d6b92bcb0> irb(main):016:0> A.new RuntimeError: Illegal! from (irb):10:in `new' from (irb):16 from /Users/olale/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>' Now we have one object, but we cannot produce another of the same class

Singleton: consequences - + Ensures single objects per class Violates several design principles!

Singleton considered dangerous Encapsulate what varies Program to an interface, not to an implementation Favor composition over inheritance Classes should be open for extension but closed for modification Don t call us, we ll call you Depend on abstractions, do not depend on concrete classes Classes should only have one reason to change Strive for loosely-coupled design

Builder

Client Director Builder build() builda() buildb() buildc() getproduct()

Client Director Builder

Abstract Factory Client receives a Factory Client requests a product from Factory Client receives an abstract product Builder Client initializes Director with Builder Client asks Director to build Client requests product from Builder Client receives a builder-specific product

Builder: consequences + Can control the way objects are created + Can produce different products using the same Director - - Not necessarily a common interface for products Clients must know how to initialize builders and retrieve products

Adapter

Class Adapter Object Adapter

Multiple back-end objects Client Target do() Adapter -adaptee1 -adaptee2 do() Adaptee1 +perform() Adaptee2 +perform()

Multiple back-end methods Client Target do() Adapter -adaptee do() Adaptee1 +foo() +bar() adaptee.foo() adaptee.bar()

public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly(); } public class TurkeyAdapter implements Duck { Turkey turkey; } public TurkeyAdapter(Turkey turkey) { this.turkey = turkey; } public void quack() { turkey.gobble(); } public void fly() { for(int i=0; i < 5; i++) { turkey.fly(); } } public class DuckAdapter implements Turkey { Duck duck; Random rand; } public DuckAdapter(Duck duck) { this.duck = duck; rand = new Random(); } public void gobble() { duck.quack(); } public void fly() { if (rand.nextint(5) == 0) { duck.fly(); } }

Adapter: consequences + Isolates interface changes to the adapter class - Class adapters require target interfaces or multiple inheritance in the language

Bridge

Abstraction == That which we (should) care about

TV Samsung LG Logitech Harmony On() Off() On() Off() Remote One For All On() Off() On() Off()

Message type Password recovery Signup E-mail Send() Send() Transmission type SMS Send() Send()

Bridge Strategy Intent Collaborations Decouple two class hierarchies (abstraction/ implementation) The Bridge forwards requests to the Implementor Allow for exchangeable algorithms The Context and Strategy collaborate, passing data between them

Bridge Adapter Intent Decouple two class hierarchies (abstraction/ implementation) Convert an existing class to fit a new interface Applicability In a new system In an existing system

Design principles Encapsulate what varies Program to an interface, not to an implementation Favor composition over inheritance Classes should be open for extension but closed for modification Don t call us, we ll call you Depend on abstractions, do not depend on concrete classes Classes should only have one reason to change

Bridge: consequences + Lets two class hierarchies with common superclasses vary independently - If some implementation classes do not support an abstract concept, the abstraction breaks

Observer

Weather Station Humidity Display Average Temp Display subscribe() subscribe() publish() publish() unsubscribe() publish()

Subject Concrete Observer

Mediator vs Observer

Design principles Encapsulate what varies Program to an interface, not to an implementation Favor composition over inheritance Classes should be open for extension but closed for modification Don t call us, we ll call you Depend on abstractions, do not depend on concrete classes Classes should only have one reason to change Strive for loosely-coupled design

Chain of Responsibility

SPAM Filter isspam(message) accept Size Filter istoolarge(message) accept Sorting Filter sort(message) process reject reject Message arrived Message rejected

Examples Logging Input management in GUI:s

CoR: consequences + provides the Observer with more control over invocation of targets - A handler does not know if it will receive a message, depending on the behavior of other handlers in the chain

Memento

Iterative Optimizer iteration current target value current solution Optimize() Abort() GetState() SetState() SolverMemento iteration current target value current solution Client - memento - optimizer Optimize() Abort() ResetOptimizer(SolverMemento)

Mementos in GUI:s - Undo/Redo User Name Age gi Ginnie 24 1 2 gi jo User Name Ginnie Johnny Age 24 37 3 Undo Redo Cut Copy Paste Paste special... 4 gi User Name Ginnie Age 24 Delete Select All

Memento: consequences - + Can externalize object state for later restoration within the lifetime of the object + Encapsulates access to the objects inner state Depending on implementation, access to private fields requires memento classes as inner/friend classes to each domain class

Command

Command Client Waiter Order Chef Order Place Order Cook

Remote control Joe s Ultimate Remote Control Fan High() Low() Off() GetSpeed() On Off On Off On Off On Off On Off On Off On Off Undo On() Off() Dim() Lamp TV TurnOn() TurnOff() SetChannel() TDDB84 Design Patterns

Client Invoker -command perform() Command +execute() Receiver +perform() Concrete Command -receiver

Client Invoker Concrete Commands Receiver Command

Command: consequences + Allows extensions of commands + Decouples the execution from the specification of the command - - Bad design if not needed! May be confusing if it removes the receiver from responsibilities

Course summary Practical work: Intro seminar + three labs Reflection and analysis: Reading a research paper, and studying design patterns in a real context

Life after the course Writing your papers :) Masters theses: Info @ 12:15! Research project in Software Engineering Exam? No.

Thanks for your participation, and good luck with your papers!