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

Design Patterns. An introduction

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

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

CS/CE 2336 Computer Science II

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

Design Patterns. Gunnar Gotshalks A4-1

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

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

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

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

SDC Design patterns GoF

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Using Design Patterns in Java Application Development

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

Application Architectures, Design Patterns

1 Software Architecture

A few important patterns and their connections

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

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

Summary of the course lectures

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

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

Design Patterns Reid Holmes

Tuesday, October 4. Announcements

Topics in Object-Oriented Design Patterns

Improve Your SystemVerilog OOP Skills

Object-Oriented Oriented Programming

A Reconnaissance on Design Patterns

Introduction and History

A Metric of the Relative Abstraction Level of Software Patterns

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

3 Product Management Anti-Patterns by Thomas Schranz

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

Foundations of Software Engineering Design Patterns -- Introduction

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

CS251 Software Engineering Lectures 18: Intro to DP

Software Engineering

Design patterns. OOD Lecture 6

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

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

DESIGN PATTERN - INTERVIEW QUESTIONS

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

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

6.3 Patterns. Definition: Design Patterns

Applying the Observer Design Pattern

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

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

UNIT I Introduction to Design Patterns

A Rapid Overview of UML

Applying Design Patterns to SCA Implementations

Introduction to Software Engineering: Object Design I Reuse & Patterns

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

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Information systems modelling UML and service description languages

Extensibility Design Patterns From The Initial Stage of Application Life-Cycle

Design patterns. Jef De Smedt Beta VZW

Object-Oriented Design

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

A Metric for Measuring the Abstraction Level of Design Patterns

Overview of Patterns: Introduction

Exam in TDDB84: Design Patterns,

CPSC 310 Software Engineering. Lecture 11. Design Patterns

WS01/02 - Design Pattern and Software Architecture

An Introduction to Patterns

Design Patterns Reid Holmes

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

UNIT I Introduction to Design Patterns

Applying the Decorator Design Pattern

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Object-oriented Software Design Patterns

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

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

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Design Patterns: Structural and Behavioural

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

A Primer on Design Patterns

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

Software Development Project. Kazi Masudul Alam

The GoF Design Patterns Reference

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

Object-Oriented Design

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

The Design Patterns Matrix From Analysis to Implementation

INSTITUTE OF AERONAUTICAL ENGINEERING

17. GRASP: Designing Objects with Responsibilities

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

Lecture 20: Design Patterns II

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

CS 247: Software Engineering Principles. Design Patterns

What is Design Patterns?

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Object Design II: Design Patterns

Lecture 19: Introduction to Design Patterns

Object-Oriented Design

GRASP Design Patterns A.A. 2018/2019

Reuse at Design Level: Design Patterns

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) Interface segregation principle (ISP) 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. (For the keen: look up the original formulation.) 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. Dependency inversion principle (DIP) From principles to patterns 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. 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

Design Patterns Patterns: background and use 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. 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. Elements of a pattern Example situation 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.) 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.

Naive solution Drawbacks of naive solution 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. 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. Generalising the situation Composite pattern 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. Graphic 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. Line Circle Text Picture for all g g. Composite is a design pattern which describes a well-understood way of doing this.

Benefits of Composite Drawbacks of Composite can automatically have trees of any depth: don t need to do anything special to let containers (Pictures) contain other containers 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 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?) Pattern collections Learning and using patterns The two best known: 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. 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 SEOC 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. Lots of pattern-writing events e.g. PLoP, EuroPLoP.

Creational patterns Structural patterns Abstract Factory Builder Factory Method Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Behavioral patterns Some patterns jargon Chain of responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template method Visitor (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].