Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Similar documents
Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

Design Patterns. Paul Jackson. School of Informatics University of Edinburgh

Software Engineering

Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

Design Patterns. Gunnar Gotshalks A4-1

Summary of the course lectures

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

Object-Oriented Design

The following topics will be covered in this course (not necessarily in this order).

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

GRASP Design Patterns A.A. 2018/2019

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

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

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

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

Topics in Object-Oriented Design Patterns

Object-Oriented Design

CSC207H: Software Design SOLID. CSC207 Winter 2018

Bruno Bossola SOLID Design Principles

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

Foundations of Software Engineering Design Patterns -- Introduction

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

Open Closed Principle (OCP)

Improve Your SystemVerilog OOP Skills

Design Patterns. An introduction

17. GRASP: Designing Objects with Responsibilities

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

COURSE 2 DESIGN PATTERNS

Object-Oriented Analysis and Design Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology-Kharagpur

CMPS 115 Winter 04. Class #10 (2004/02/05) Changes/Review Programming Paradigms Principles of OOD <break> Design Patterns

Application Architectures, Design Patterns

CS/CE 2336 Computer Science II

Single Responsibility Principle (SRP)

CPSC 310 Software Engineering. Lecture 11. Design Patterns

1 Software Architecture

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

Design Patterns Reid Holmes

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

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

In this Lecture you will Learn: Design Patterns. Patterns vs. Frameworks. Patterns vs. Frameworks

Object Design II: Design Patterns

Principles of Object-Oriented Design

Intro to: Design Principles

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Design Patterns. CSC207 Winter 2017

PATTERNS AND SOFTWARE DESIGN

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

17.11 Bean Rules persistent

More on object orientation

Lecture 19: Introduction to Design Patterns

An Introduction to Patterns

A few important patterns and their connections

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

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Design Patterns. CSC207 Fall 2017

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

Software Development

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

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

Practical Object-Oriented Design in Ruby

(Refer Slide Time: 06:01)

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Lecture 23: Domain-Driven Design (Part 1)

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Exam in TDDB84: Design Patterns,

Patterns of learning

Princípy tvorby softvéru Dizajnové princípy

Design Patterns Reid Holmes

administrivia today UML start design patterns Tuesday, September 28, 2010

CS211 Lecture: Design Quality; Cohesion and Coupling; Packages

Influence of Design Patterns Application on Quality of IT Solutions

Object-Oriented Design

Introduction to Testing and Maintainable code

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

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

Design Patterns. CSC207 Fall 2017

CHAPTER 5 GENERAL OOP CONCEPTS

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

OO Class Design Principles

A Metric of the Relative Abstraction Level of Software Patterns

Introduction to Design Patterns

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

08. DESIGN PRINCIPLES. Originality is Overrated PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT

Design patterns. OOD Lecture 6

5. Application Layer. Introduction

An Introduction to Patterns

Construction: High quality code for programming in the large

CPS122 Lecture: Design Patterns Last revised March 20, 2012

Introduction to Design Patterns

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

Introduction and History

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

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

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

3 Product Management Anti-Patterns by Thomas Schranz

Design Pattern: Composite

Object-Oriented Design

Object Oriented Software Design - I

Transcription:

Plan Design principles: laughing in the face of change Perdita Stevens School of Informatics University of Edinburgh What are we trying to achieve? Review: Design principles you know from Inf2C-SE Going further: SOLID design Design patterns... What are we trying to achieve? All good things: being able to build software faster, cheaper, better... Overwhelmingly, though: Resilience in the face of change. What kind of change? Especially: change in requirements (functional or non-functional). Recall: this is also the main motivation for using OO in the first place. Structuring the system in terms of enduring features of the domain makes the structure likely to be stable as requirements change. Here the focus is mostly on localising change: when requirements change, we want to be able to change the software easily. This encompasses: not having to change too much, e.g. minimise knock-on effects (low coupling); being able to identify easily what we have to change (understandable design); all the changes being in nearby code, e.g. in same class (high cohesion).

Review: Design principles you know from Inf2C-SE Going further: SOLID design Need to make sure you are confident about the meanings of: High cohesion Low coupling Modularisation Abstraction Encapsulation Separation of interface and implementation (Acronym coined by Robert Martin: principles developed by many people.) Single responsibility principle (SRP) Open closed principle (OCP) Liskov substitution principle (LSP) Interface segregation principle (ISP) Dependency inversion principle (DIP) Single responsibility principle (SRP) Open closed principle (OCP) A class should do be one thing, and do be one thing well. What s a thing here? A responsibility......martin says, a reason to change. Essentially about coherence: idea is that a class may offer several operations, but they should be sufficiently coherent that in practice, the kind of requirements change that makes you change one is likely to make you change all. Bertrand Meyer: software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification i.e. you can modify the behaviour of a class/interface without (necessarily) modifying its code. E.g., you can create a subclass. (For the keen: look at the different historical flavours of this principle, e.g., starting at the Wikipedia page for the principle.)

Liskov substitution principle (LSP) LSP more precisely Barbara Liskov. When you inherit, feel free to add or reimplement functionality, but don t break it. Clients that think they re getting an object of the base class must still work if they re given an object of the subclass. Let T be a type and let S be a subtype of T. There are many formulations of LSP, not always easy to relate to the original papers! The aim is: if we know φ(x) holds for all objects x of type T, then also, φ(x) holds for all objects x of type S (Think of φ(x) as my program, which is written terms of type T, behaves correctly when given x as argument for example, and don t worry too much about the difference between provable and true.) Original formulation: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Interface segregation principle (ISP) Dependency inversion principle (DIP) This is essentially the SRP but for interfaces. A client should not have to rely on parts of an interface it does not need: there should be an interface that includes (almost) only the things the client actually needs. Robert Martin: A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions.

From principles to patterns Design Patterns Any competent designer has to understand the principles that underlie good design. However, you do not have to solve every problem from such first principles. Design patterns capture good, but non-obvious, solutions to common problems that are tricky in the context of OO. (Functional programming could have patterns too: they might capture things that are obvious in OO but tricky in FP.) + Solve the problem - Add complexity Reuse of good ideas A pattern is a named, well understood good solution to a common problem in context. Experienced designers recognise variants on recurring problems and understand how to solve them. Without patterns, novices have to find solutions from first principles. Patterns help novices to learn by example to behave more like experts. But in fact, this may not be the most important thing they do we ll come back to this. Patterns: background and use Elements of a pattern Idea comes from architecture (Christopher Alexander): e.g. Window Place: observe that people need comfortable places to sit, and like being near windows, so make a comfortable seating place at a window. Similarly, there are many commonly arising technical problems in software design. Pattern catalogues: patterns written down in a standard format for easy reference, and to let designers talk shorthand. Pattern languages are a bit more... Patterns also used in: reengineering; project management; configuration management; etc. A pattern catalogue entry normally includes roughly: Name (e.g. Publisher-Subscriber) Aliases (e.g. Observer, Dependants) Context (in what circumstances can the problem arise?) Problem (why won t a naive approach work?) Solution (normally a mixture of text and models) Consequences (good and bad things about what happens if you use the pattern.)

Example situation Naive solution A graphics application has primitive graphic elements like lines, text strings, circles etc. A client of the application interacts with these objects in much the same way: for example, it might expect to be able to instruct such objects to draw themselves, move, change colour, etc. Clearly there should be an interface or an abstract base class, say Graphics, which describes the common features of graphics elements, with subclasses Text, Line, etc. So far so simple. But we also want to be able to group elements together to form pictures, which can then be treated as a whole: for example, users expect to be able to move a composite picture just as they move primitive elements. The collection of available primitive elements, the kinds of grouping available, and even what s primitive, may change relatively frequently as the program evolves. Create a new class, say Container, which contains collection of Graphics elements. Rewrite the clients so that they use both classes and (e.g.) have a variable of each type, and a boolean flag to tell them which they re using. To apply an operation, they 1. check the flag to see whether they are dealing with a container; 2. if so, they get its collection of children and send the message to each child in turn; 3. if not, just sent the message to the simple Graphics object. Drawbacks of naive solution Generalising the situation Every client now has to be aware of the Container class and to do extra work to handle the fact that they might be dealing with a Container. And can a Container contain other Containers? Not if we implement Container and Graphics as unrelated classes with the Container having a collection of Graphics objects. We often want to be able to model tree-like structures of objects: an object may be a thing without interesting structure a leaf of the tree or it may itself be composed of other objects which in turn might be leaves or might be composed of other objects... The collection of kinds of leaves or ways of composing them may be relatively unstable. To avoid spreading dependencies on the precise collection throughout the program, we d like other parts of the program to be able to interact with a single class, insulated from the structure of the tree. Composite is a design pattern which describes a well-understood way of doing this.

Composite pattern Benefits of Composite Graphic can automatically have trees of any depth: don t need to do anything special to let containers (Pictures) contain other containers Line Circle Text Picture for all g g. clients can be kept simple: they only have to know about one class, and they don t have to recurse down the tree structure themselves it s easy to add new kinds of Graphics subclasses, including different kinds of pictures, because clients don t have to be altered Drawbacks of Composite Pattern collections The two best known: It s not easy to write clients which don t want to deal with composite pictures: the type system doesn t know the difference. (A Picture is no more different from a Line than a Circle is, from the point of view of the type checker.) (What could you do about this?) Gang of Four, GoF or Gamma book: Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, Helm, Johnson, Vlissides POSA book: A System of Patterns: pattern oriented software architecture by Buschmann et al. Lots of others. I like Pattern Hatching by Vlissides: not a pattern catalogue, more an explanation of how to use the GoF book Analysis Patterns: reusable object models by Martin Fowler. Lots of pattern-writing events e.g. PLoP, EuroPLoP.

Learning and using patterns Some patterns jargon We ll talk through some but I rejected the idea of trying to put full information on slides. To make serious use of them you ll need (access to) a copy of the GoF book; for exam purposes, slides and required reading references will suffice. Wikipedia articles on each pattern are useful, and good for code examples, but can be down-bogging. The specific patterns you need to know are the ones mentioned in lectures or exercise sheets: highlighted on a list in the next set of slides. (some a bit mystical for me, but you need to have heard the terms) forces QWAN generativity Rule of Three Forces QWAN Interesting design problems often involve potentially conflicting priorities, or forces. E.g. you want your system to be easy to extend which might push you towards including an abstract superclass; but also small and simple, which might push you away from that solution. Alexander in The timeless way of building Quality without a name wholeness; vitality; integrity Some pattern writers, but not the GoF, make forces explicit in their pattern writing. Forces come from the problem: no design pattern is always the right answer.

Generativity The Rule of Three Alexander again. Patterns are, or can be, generative in two senses: a pattern tells you how to build something patterns used well together can have good emergent properties Do patterns generate architectures? It has been claimed, but I think not, at least not unless we allow pattern to include things that have never been written down. Most architectures will involve at most a few GoF patterns. Patterns are supposed to have three independent successful uses before they count - to avoid codifying neat ideas - to get the description at the right level of abstraction GoF book includes a Known uses section for this. Ways in which patterns are useful Cautions on pattern use To teach solutions (not actually the most important) As high-level vocabulary To practise general design skills As an authority to appeal to If a team or organisation writes its own patterns: to make how we do things explicit. Patterns are very useful if you have the problem they re trying to solve. But they add complexity, and often e.g. performance penalties too. Exercise discretion. You ll find the criticism that the GoF patterns in particular are just getting round the deficiencies of OOPLs. This is true, but misses the point. Challenge: write a pattern language for [your favourite non-oo language].