Deconstruction Kevlin Henney kevlin@curbralan.com @KevlinHenney S - - PowerPoint PPT Presentation

deconstruction
SMART_READER_LITE
LIVE PREVIEW

Deconstruction Kevlin Henney kevlin@curbralan.com @KevlinHenney S - - PowerPoint PPT Presentation

SOLID Deconstruction Kevlin Henney kevlin@curbralan.com @KevlinHenney S O L I D Single e Responsib onsibil ility ity Open-Close osed Liskov ov Substituti itution on Inter erfac face e Segregati egation Depen enden dency


slide-1
SLIDE 1

SOLID Deconstruction

Kevlin Henney

kevlin@curbralan.com @KevlinHenney

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4

S O L I D

slide-5
SLIDE 5

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-6
SLIDE 6
slide-7
SLIDE 7

principle

  • a fundamental truth or proposition that serves as the

foundation for a system of belief or behaviour or for a chain of reasoning.

  • morally correct behaviour and attitudes.
  • a general scientific theorem or law that has numerous

special applications across a wide field.

  • a natural law forming the basis for the construction or

working of a machine.

Oxford Dictionary of English

slide-8
SLIDE 8

pattern

  • a regular form or sequence discernible in the way in

which something happens or is done.

  • an example for others to follow.
  • a particular recurring design problem that arises in

specific design contexts and presents a well-proven solution for the problem. The solution is specified by describing the roles of its constituent participants, their responsibilities and relationships, and the ways in which they collaborate.

Concise Oxford English Dictionary Pattern-Oriented Software Architecture, Volume 5: On Patterns and Pattern Languages

slide-9
SLIDE 9

Expert ert Profic icien ient Compete tent nt Advan anced ed Beginn inner er Novice

slide-10
SLIDE 10

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-11
SLIDE 11

In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.

http://en.wikipedia.org/wiki/Single_responsibility_principle

slide-12
SLIDE 12

The term was introduced by Robert C. Martin [...]. Martin described it as being based on the principle of cohesion, as described by Tom DeMarco in his book Structured Analysis and Systems Specification.

http://en.wikipedia.org/wiki/Single_responsibility_principle

slide-13
SLIDE 13
slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17

We refer to a sound line of reasoning, for example, as coherent. The thoughts fit, they go together, they relate to each

  • ther. This is exactly the characteristic
  • f a class that makes it coherent: the

pieces all seem to be related, they seem to belong together, and it would feel somewhat unnatural to pull them apart. Such a class exhibits cohesion.

slide-18
SLIDE 18

This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together.

Doug McIlroy

slide-19
SLIDE 19

The hard part isn’t writing little programs that do one thing well. The hard part is combining little programs to solve bigger

  • problems. In McIlroy’s summary,

the hard part is his second sentence: Write programs to work together.

John D Cook

http://www.johndcook.com/blog/2010/06/30/where-the-unix-philosophy-breaks-down/

slide-20
SLIDE 20

The harder part isn’t writing little programs that do one thing well. The harder part is combining little programs to solve bigger

  • problems. In McIlroy’s summary,

the harder part is his second sentence: Write programs to work together.

slide-21
SLIDE 21

Software applications do things they’re not good at for the same reason companies do things they’re not good at: to avoid transaction costs.

John D Cook

http://www.johndcook.com/blog/2010/06/30/where-the-unix-philosophy-breaks-down/

slide-22
SLIDE 22
slide-23
SLIDE 23

utility

  • the state of being useful, profitable or beneficial
  • useful, especially through having several functions
  • functional rather than attractive

Concise Oxford English Dictionary

slide-24
SLIDE 24

#incl clude ude <st stdlib. lib.h>

slide-25
SLIDE 25

Every class should embody only about 3–5 distinct responsibilities.

Grady Booch, Object Solutions

slide-26
SLIDE 26
slide-27
SLIDE 27

One of the most foundational principles of good design is: Gather together those things that change for the same reason, and separate those things that change for different reasons. This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change.

slide-28
SLIDE 28

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-29
SLIDE 29

Interface inheritance (subtyping) is used whenever one can imagine that client code should depend on less functionality than the full

  • interface. Services are often partitioned into

several unrelated interfaces when it is possible to partition the clients into different roles. For example, an administrative interface is often unrelated and distinct in the type system from the interface used by “normal” clients.

"General Design Principles"

CORBAservices

slide-30
SLIDE 30

The dependency ncy should uld be on the interfac face, e, the whole le interfac face, e, and nothing ng but ut the interface face.

slide-31
SLIDE 31
slide-32
SLIDE 32

We refer to a sound line of reasoning, for example, as coherent. The thoughts fit, they go together, they relate to each

  • ther. This is exactly the characteristic
  • f a class that makes it coherent: the

pieces all seem to be related, they seem to belong together, and it would feel somewhat unnatural to pull them apart. Such a class exhibits cohesion.

slide-33
SLIDE 33

We refer to a sound line of reasoning, for example, as coherent. The thoughts fit, they go together, they relate to each

  • ther. This is exactly the characteristic of

an interface that makes it coherent: the pieces all seem to be related, they seem to belong together, and it would feel somewhat unnatural to pull them apart. Such an interface exhibits cohesion.

slide-34
SLIDE 34

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-35
SLIDE 35

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-36
SLIDE 36

generalisation specialisation

commonality variation

slide-37
SLIDE 37
slide-38
SLIDE 38

public class RecentlyUsedList { ... public int Count { get ... } public string this[int index] { get ... } public void Add(string newItem) ... ... }

slide-39
SLIDE 39

public class RecentlyUsedList { private IList<string> items = new List<string>(); public int Count { get { return items.Count; } } public string this[int index] { get { return items[index]; } } public void Add(string newItem) { if(newItem == null) throw new ArgumentNullException(); items.Remove(newItem); items.Insert(0, newItem); } ... }

slide-40
SLIDE 40

public class RecentlyUsedList : List<string> { public override void Add(string newItem) { if(newItem == null) throw new ArgumentNullException(); items.Remove(newItem); items.Insert(0, newItem); } ... }

slide-41
SLIDE 41

namespace List_spec { ... [TestFixture] public class Addition { private List<string> list; [Setup] public void List_is_initially_empty() { list = ... } ... [Test] public void Addition_of_non_null_item_is_appended() ... [Test] public void Addition_of_null_is_permitted() ... [Test] public void Addition_of_duplicate_item_is_appended() ... ... } ... }

slide-42
SLIDE 42

namespace List_spec { ... [TestFixture] public class Addition { private List<string> list; [Setup] public void List_is_initially_empty() { list = new List<string>(); } ... [Test] public void Addition_of_non_null_item_is_appended() ... [Test] public void Addition_of_null_is_permitted() ... [Test] public void Addition_of_duplicate_item_is_appended() ... ... } ... }

slide-43
SLIDE 43

namespace List_spec { ... [TestFixture] public class Addition { private List<string> list; [Setup] public void List_is_initially_empty() { list = new RecentlyUsedList(); } ... [Test] public void Addition_of_non_null_item_is_appended() ... [Test] public void Addition_of_null_is_permitted() ... [Test] public void Addition_of_duplicate_item_is_appended() ... ... } ... }

slide-44
SLIDE 44

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-45
SLIDE 45

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-46
SLIDE 46

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-47
SLIDE 47

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-48
SLIDE 48

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-49
SLIDE 49

The principle stated that a good module structure should be both open and closed:

  • Closed, because clients need the module's

services to proceed with their own development, and once they have settled on a version of the module should not be affected by the introduction of new services they do not need.

  • Open, because there is no guarantee that we will

include right from the start every service potentially useful to some client.

Bertrand Meyer Object-Oriented Software Construction

slide-50
SLIDE 50

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-51
SLIDE 51

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-52
SLIDE 52

A type hierarchy is composed of subtypes and

  • supertypes. The intuitive idea of a subtype is one

whose objects provide all the behavior of objects

  • f another type (the supertype) plus something
  • extra. What is wanted here is something like the

following substitution property: If for each

  • bject 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.

Barbara Liskov

"Data Abstraction and Hierarchy"

slide-53
SLIDE 53

A myth in the object-oriented design community goes something like this: If you use object-oriented technology, you can take any class someone else wrote, and, by using it as a base class, refine it to do a similar task.

Robert B Murray C++ Strategies and Tactics

slide-54
SLIDE 54

Published Interface is a term I used (first in Refactoring) to refer to a class interface that's used

  • utside the code base that it's defined in.

The distinction between published and public is actually more important than that between public and private. The reason is that with a non-published interface you can change it and update the calling code since it is all within a single code base. [...] But anything published so you can't reach the calling code needs more complicated treatment.

Martin Fowler

http://martinfowler.com/bliki/PublishedInterface.html

slide-55
SLIDE 55

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-56
SLIDE 56

In object-oriented programming, the dependency inversion principle refers to a specific form of decoupling where conventional dependency relationships established from high- level, policy-setting modules to low-level, dependency modules are inverted (i.e. reversed) for the purpose of rendering high-level modules independent of the low-level module implementation details.

http://en.wikipedia.org/wiki/Dependency_inversion_principle

slide-57
SLIDE 57
slide-58
SLIDE 58

Stewart Brand, How Buildings Learn

See also http://www.laputan.org/mud/

slide-59
SLIDE 59

package kage com.sun .sun…;

slide-60
SLIDE 60

       

Scenario buffering by dot-voting possible changes and invert dependencies as needed

slide-61
SLIDE 61

One of the most foundational principles of good design is: Gather together those things that change for the same reason, and separate those things that change for different reasons. This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change.

slide-62
SLIDE 62

S O L I D

slide-63
SLIDE 63

Single e Responsib

  • nsibil

ility ity Open-Close

  • sed

Liskov

  • v Substituti

itution

  • n

Inter erfac face e Segregati egation Depen enden dency cy Inver ersion sion

slide-64
SLIDE 64

Cohesion ion by Usage Cohesion ion by Change ge Conforman mance Consistenc stency y over Space Consistenc stency y over Time

slide-65
SLIDE 65

At some level the style becomes the substance.