21
[email protected] Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they? How easy are they to use? Pattern types Example Patterns Resources

[email protected] Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

Embed Size (px)

Citation preview

Page 1: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsIntroduction

What is a Design Pattern?Why were they developed?Why should we use them?How important are they?How easy are they to use?Pattern typesExample PatternsResources

Page 2: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsWhat is a Design Pattern?

When programming complex systems, some problems arise again and again.

Programmers build up expertise and experience of solving these problems.

A design pattern can be thought of as an encapsulation of programming best practice.

Languages like Java already incorporate some design patterns into their structure.

Page 3: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsWhat is a Design Pattern?

A design pattern is not necessarily a single chunk of code (detailed implementation).

It may be a style, an approach to solving a problem (architecture).

It may be the “best” approach - one that experience has shown to be efficient, reliable, scalable and so on.

Many patterns come with UML documentation.

Page 4: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsUse Case patterns

Login

User

Login

Register

Remind UserID

Remind Password«extends»

«extends»

«extends»

Page 5: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsUse Case patterns

Sales Order Processing

CreateOrder

UpdateOrder

DeleteOrder

FindOrder

CustomerCustomer

Stock ControllerStock Controller

Uses

Uses

CustomerCustomer

Page 6: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsClass Patterns

+changeLastOrderDate() : int+changeTotalSpend() : decimal

-lastOrderDate : int-totalSpend : decimal

<<business>>customer

+creditAcount() : decimal+debitAccount() : decimal+suspend()+re-instate()

-currentBalnace : decimal-suspended : bool

<<business>>member

+changeFirstName() : string+changeSecondName() : string+changeAccessRights() : string

-firstName : string-secondName : string-acessRights : string

<<business>>employee

+create() : string+delete()+changeEmail() : string

-email : string

<<business>>user

+changeUserName() : string+changePassWord() : string

-userName : string-passWord : string

<<business>>secureUser

Page 7: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsWhat is a Design Pattern?

“Design patterns constitute a set of rules describing how to accomplish certain tasks in the realm of software development.” (Pree, 1994)

“Design patterns focus more on reuse of recurring architectural design themes, while frameworks focus on detailed design… and implementation.” (Coplien & Schmidt, 1995).

Page 8: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsWhat is a Design Pattern?

“A pattern addresses a recurring design problem that arises in specific design situations and presents a solution to it” (Buschmann, et. al. 1996)

“Patterns identify and specify abstractions that are above the level of single classes and instances, or of components.” (Gamma, et al., 1993)

Page 9: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsWhy were they developed?

Attempts to make programming into an engineering discipline rather than an art form.

Can help to keep costs low.Brings reliability through good design.Speeds development cycles since no need to

“reinvent the wheel”.Helps with maintenance (patterns may get

updated).

Page 10: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsWhy should we use them?

Design Patterns should help us achieve our development goals with less effort.

Communities of programmers will have familiarity with common patterns.

Modern languages incorporate some patterns at the API level, for example the Java media loading and event handling systems.

Page 11: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsHow important are they?

In some areas (finance, engineering, user interface design) they are very useful and widely used.

For some small, ad-hoc, single-developer projects, design patterns may not have much impact.

Page 12: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsHow easy are they to use?

Some design patterns are very simple to use, others are complex solutions to specific, complex problems.

You probably use some already.They are well documented and explained.

Page 13: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsPattern Types

Creational patternsbuild objects for you, so your program does not have to explicitly instantiate some objects. This gives run-time flexibility.

Structural patterns help impose order on groups of objects, for example on interrelated GUI components.

Behavioral patternsaid in defining communication and data flow between objects.

Page 14: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsA Few Example Patterns

The design patterns philosophy recommends:Program to an interface (or abstract class) and not

to an implementation.Favour object composition (encapsulation) over

inheritance.

The original Design Patterns book (GoF) describes 23 patterns.

These are the core patterns. Others have been added over time.

Page 15: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsExample Patterns

The Factory Methodpart of a “decision making” class.

It returns one of several possible subclasses of an abstract base class depending on some input data.

Example: Your program reads and decodes compressed image data.

It supports many compression formats by having a factory choose an appropriate decoder class.

All decoders are based on one abstract parent class.

Page 16: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsExample Patterns

The Singleton Patterna class of which there can be no more than one instance.

It provides a single global point of access to that instance.

Useful for resource management-related classes like print spoolers or audio output managers where there must be just one object in control.

NB the “Math” class in Java – declared final, with static methods only. One way of making a singleton.

Page 17: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsExample Patterns

The Adapter Patternprovide an interface in one class to resemble another.

An adapter class can be derived from a non-conforming parent and new methods added to make it fit some other API. [class adapter]

Or we can include (encapsulate) the original class inside our new one and write API conversion methods. [object adapter]

Handy pattern when updating an application to use newer libraries, or to interface with some slightly different client or server layer.

Page 18: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsExample Patterns

The Façade patternallows us to simplify program complexity. As a system grows, so does the complexity of the system. We can provide a simplified interface to the system.

This simplification may reduce flexibility, but can provide all the functionality required for the majority of situations.

Of course we can directly access the underlying classes and methods if we need to.

A well known example of a façade is the JDBC interface.

Page 19: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsExample Patterns

The Strategy Patternallows you to select one of several algorithms dynamically by encapsulating an algorithm inside a selectable class.

The algorithms implement a common interface but may be otherwise unrelated.

This helps us avoid the messy conditional statements that can make code hard to read and maintain.

E.g. Encode an audio stream as mono, 22KHz, PCM. We manually instantiate a strategy class that does this, then pass it to generic processing methods to handle the data stream (cf Factory pattern).

Page 20: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsResources

Design Patterns Java Companion (free book in PDF format): http://www.patterndepot.com/put/8/JavaPatterns.htm

Example patterns with explanatory text, Antonio García: http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/default.htm

Sun J2EE Patterns: http://java.sun.com/blueprints/patterns/Java Design Patterns 201:

http://www-106.ibm.com/developerworks/edu/j-dw-javapatt2-i.htmlCore J2EE Patterns (book on Amazon) Important book: Design Patterns by Gamma, Helm, Johnson and

Vlissides (Addison Wesley Longman, 1995.  ISBN 0-201-63361-2) Interesting review of the above book here.

Page 21: N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?

[email protected]

Design PatternsConclusion

Design patterns are worth using in many circumstances.

You will have to invest some time in order to understand them and absorb the implications of this style of programming.

For corporate developers, design patterns represent a resource of expertise that cannot be ignored.