Docsity

Design Patterns: Object-Oriented Solutions for Software Development, Lecture notes of Computers and Information technologies

Partial preview of the text

Download Design Patterns: Object-Oriented Solutions for Software Development and more Lecture notes Computers and Information technologies in PDF only on Docsity!

09/25/2019 System Building 2 PeterDench (a few slides are 1

System Building 2

Lecture 15

Design Patterns

09/25/2019 System Building 2 PeterDench (a few slides are 2

Design Patterns

Design is always a compromise

What makes a “Good Design”

Experience

Skill

A Black Art

Minimising the effects of change

Patterns

 Developed by skilled and experienced practitioners

 Encapsulate “good design practices”

 Couple a problem to “good solution(s)” (Pairing)

 Problem (identifying the problem –and its pair!)

09/25/2019 System Building 2 PeterDench (a few slides are 4

Design Patterns

Patterns

Peter Coad 1992

“An object-oriented pattern is an

abstraction of a doublet, triplet, or other

small grouping of classes that is likely to

be helpful again and again in object-

oriented development.”

09/25/2019 System Building 2 PeterDench (a few slides are 5

Design Patterns

Patterns

Gang of Four 1995.

Gamma E.

 Helm R.

 Johnson R.

Vlissides J.

Gamma’s doctoral thesis (91) used as

a basis for work started in 93

25 patterns (3 groups)

09/25/2019 System Building 2 PeterDench (a few slides are 7

Design Patterns Expert

General Responsibility Assignment

Software Patterns.

Expert

 Assign responsibility to the information expert

the class that has all the information

necessary to fulfil the responsibility

 Use the highest (heirarchicaly) such class (if one

exists)

 Do not add information to a class use a subclass

09/25/2019 System Building 2 PeterDench (a few slides are 8

Design Patterns Creator

General Responsibility Assignment Software

Patterns.

 Creator

 Assign class B the responsibility to create an instance of

class A if:

1. B contains A for the duration of A’s life

2. B is a composition of A

1. B is an aggregation of A and

1. Nothing is a composition of A

2. It is the best candidate of all who aggregate A

3. B has the initializing data (is the expert) for A

4. B records A

5. B closely uses A

 Assign the responsibility to the class that is the highest in

the list

09/25/2019 System Building 2 PeterDench (a few slides are 10

Design Patterns Low Coupling

General Responsibility Assignment Software

Patterns.

Low Coupling

 A generic concept (pattern) since 1960s

 The “need to know” principle

 No construct should be aware of any other construct

that is out side of itself

 Except perhaps (via)

 It’s immediate parent

 It’s parameters

 It’s attribute types (this one is iffy)

09/25/2019 System Building 2 PeterDench (a few slides are 11

Design Patterns High Cohesion

General Responsibility Assignment

Software Patterns.

High Cohesion

 A generic concept (pattern) since 1960s

A construct should relate to or do one thing

 -and only one thing

 It should do/be the minimum

 Leave the bells and whistles to be added later by

something else (if absolutely necessary)

09/25/2019 System Building 2 PeterDench (a few slides are 13

Design Patterns Pure Fabrication

General Responsibility Assignment Software

Patterns.

Pure Fabrication

 Create (almost ad hoc) classes/methods

 To facilitate and support the use of other patterns

 Artificial

 Not of the real problem domain

 Provide additional structure

 Remove coupling

 Increase cohesion

 Promotes reuse (of other classes)

 Abstracts problem specific constructs/artefacts

09/25/2019 System Building 2 PeterDench (a few slides are 14

Design Patterns Indirection

General Responsibility Assignment

Software Patterns.

Indirection

 Like pure fabrication

Introduced to reduce coupling

 Mediator between

 Classes

 Components and services

 Objects and interfaces

 Use methods to access Attributes

09/25/2019 System Building 2 PeterDench (a few slides are 16

Pattern Description (not laws just guidance PPSD)

Expert

Assign a responsibility to the information expert -- the class that has the information necessary to fulfill the

responsibility.

Creator

Assign class B the responsibility to create an instance of class A if one of the following is true:

1.B contains A

2.B aggregates A

3.B has the initializing data for A

4.B records A

5.B closely uses A

Controller

Assign the responsibility for handling a system event message to a class representing one of these choices:

1.The business or overall organization or overall "system" (a façade controller).

2.An animate thing in the domain that would perform the work (a role controller).

3.An artificial class (Pure Fabrication) representing the use (a use case controller).

Low Coupling Assign responsibilities so that coupling remains low. (to reduce reliance on external objects)

High Cohesion Assign responsibilities so that cohesion remains high. (to keep complexity manageable)

Polymorphism

When related alternatives or behaviours vary by type (class), assign responsibility for the behaviour—using

polymorphic operations—to the types for which the behaviour varies.

Pure

Fabrication

Assign a highly cohesive set of responsibilities to an artificial class that does not represent anything in the

problem domain, in order to support high cohesion, low coupling, and reuse.

Indirection

Assign the responsibility to an intermediate object to mediate between other components or services, so

that they are not directly coupled.

Don't Talk to

Strangers

Within a method, messages can only be sent to the following objects:

The this object (or self ).

A parameter of the method

A attribute of self.

An element of a collection which is an attribute of self.

An object created within a method

Edited From Larman 1998

09/25/2019 System Building 2 PeterDench (a few slides are 17

Why Patterns (MP)

 The balanced assignment of responsibilities is extremely

important on the quality of the design

 Determining the assignment of responsibilities often occurs

during construction of interaction (e.g. Sequence) diagrams

 Patterns are named problem/solution pairs that codify good

advice and principles for assigning responsibilities

 These patterns built on past experience from software

engineers:

 Good design, and more importantly

 Bad design

 An effort to make the tacit knowledge (“art”) into explicit.

09/25/2019 System Building 2 PeterDench (a few slides are 19

OOP examples of coupling (MP/PD)

Coupling between classes

Sometimes you have to live with it

 Class A has an attribute that refers to a class B

instance or to class B itself

 Class A has a method that references an instance of

class B or class B itself. (parameter, local variable or

return value)

 Class A is a direct or indirect subclass of class B

 Class B is an interface and class A implements that

interface

The above are examples of low coupling

09/25/2019 System Building 2 PeterDench (a few slides are 20

Coupling (MP)

Assign a responsibility so that coupling remains low

Coupling is a measure of how strongly one class is connected to,

has knowledge of, or relies upon other classes.

 A class with low (weak) coupling is not dependent on too

many classes

 A class with high (strong) coupling relies upon many other

classes (undesirable)

 Changes in related classes force changes in local classes

 The class is harder to understand in isolation

 It is harder to re-use because its use requires the presence of

the classes it depends on

 Any object that is ‘aware of’ and directly uses or manipulates

the inner structure of another has high (poor) coupling