Go f designpatterns 130116024923-phpapp02

30
GoF Design patterns Jagath Bandara

Transcript of Go f designpatterns 130116024923-phpapp02

GoF Design patterns - Jagath Bandara

Introduction Each pattern describes a problem which occurs over and over again in software

programming environment and then describes the core of solution to that problem.

Uses of Design Patterns Finding appropriate objects. Determining object granularity. Specifying object interfaces. Specifying object implementations. Programming to an interface not to an implementation.

Types of Design Patterns• Which can be used while creating objects.• Factory, Abstract Factory, Singleton, Prototype, BuilderCreational• Which can be used to combine objects and classes in

order to build structured objects.• Adapter, Façade, Proxy, Composite, etc.Structural• Which can be used to build a computation and to control

data flows.• Iterator, Observer, Strategy, State, etc.Behavioral

Decorator pattern Also known as “Wrapper”.

Add extra functionality (at runtime),

while keeping the interface the same.

Attach additional functionalities to an object dynamically.

Decorator example

• Sometimes we want to add responsibilities to individual objects, not to an entire class. A graphical user interface toolkit, for example, should let you add properties like borders or behaviors like scrolling to any user interface component.

Decorator Applicability

• To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.

• For responsibilities that can be withdrawn.

• when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.

Facade Pattern A facade is an object that provides a simplified interface to a larger body of code,

such as a class library. Make a software library easier to use, understand and test, since the facade has

convenient methods for common tasks. Make the library more readable. Reduce dependencies of outside code on the inner workings of a library, since

most code uses the facade, thus allowing more flexibility in developing the system. Wraps a poorly designed collection of APIs with a single well-designed API.

Facade UML

Façade Example

Flywieght pattern

Use sharing to support large numbers of fine-grained objects efficiently.

Some applications could benefit from using objects throughout their design, but

a naive implementation would be prohibitively expensive.

Flyweight example Object-oriented document editors typically use objects to represent embedded

elements like tables and figures. However, they usually stop short of using an object for each character in the document, even though doing so would promote flexibility at the finest levels in the application.

Imagine you are using one font type in a document and you have only 26 English letters. Imagine the document have 10000 letter.

If we use one object per character, then we will have 10000+ objects.

But if we use the methods described in flyweight, then their will only be lower

than 30 character objects with commas and the dots.

Flyweight

A flyweight is a shared object that can be used in multiple contexts simultaneously.

Intrinsic state is stored in the flyweight; it consists of information that's independent of the flyweight's context, thereby making it sharable. Extrinsic state depends on and varies with the flyweight's context and therefore can't be shared.

Client objects are responsible for passing extrinsic state to the flyweight when it needs it.

Let’s see how it is happening

Flyweight

Proxy pattern

Also known as “surrogate” Provides a class which limits the access to the original class. It might be for

Security reasons To reduce memory foot print To avoid complex object usage

Proxy example Lets take a word processing application as an example. There can be images in a document which is currently showing. But the images are not in the now showing area. But if we load the document

completely, we have to load the images an it will be a huge memory usage. We can use the proxy pattern for this kind of situation. We can use an image proxy

and it refer to the image on the disk. The image can be loaded when it is needed using that reference.

Proxy Facts Introduces a level of indirection when accessing an object. Can hide the fact that the object resides in different address space. Can perform optimizations such as creating an object on demand.

Chain of responsibility

Define a method of passing a request among a chain of objects.

In this pattern we don’t specify an object to handle the event. The event goes from object to object and the one who is capable of handling it will handle the event.

This will Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.

Chain of responsibility example

Consider a context-sensitive help facility for a graphical user interface. The user can obtain help information on any part of the interface just by clicking on it.

What if a button doesn't’ have help information?

If you used the chain of responsibility pattern, then their will be a more generalized help information from a super class of that button. That means someone which can handle that mouse click event will response to your request

Chain of responsibility

Command pattern

Also known as “Action”, “Transaction“.

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Sometimes it's necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request. That’s where we use the command pattern.

Command example Take a user interface toolkit as an example. There are buttons in that toolkit and

the toolkit designer has no idea what the developer is going to do in his application using this buttons.

The Command pattern lets toolkit objects make requests of unspecified application objects by turning the request itself into an object.

So then the application developer can use this request objects in any where of the application to any purpose.

Command pattern This is an another example for command. A user can use paste function in so

many thins as charactors, pictures or clip arts in a word processing application. But the thing happening is that it runs according to the thing copied to the clip board at the corresponding time.

Interpreter pattern

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

If a particular kind of problem occurs often enough, then it might be worthwhile to express instances of the problem as sentences in a simple language. Then you can build an interpreter that solves the problem by interpreting these sentences.

Interpreter exampleexpression ::= literal | alternation | sequence | repetition |'(' expression ')'alternation ::= expression '|' expressionsequence ::= expression '&' expressionrepetition ::= expression '*'literal ::= 'a' | 'b' | 'c' | ... { 'a' | 'b' | 'c' | ... }*

• In this example, the pattern describes how to define a grammar for regular expressions, represent a particular regular expression, and how to interpret that regular expression.

• The symbol expression is the start symbol, and literals a terminal symbol defining simple words.

• The Interpreter pattern uses a class to represent each grammar rule. Symbols on the right-hand side of the rule are instance variables of these classes.

Interpreter patternUses Use the Interpreter pattern when there is a language to interpret, and you can

represent statements in the language as abstract syntax trees.

Iterator pattern

Also known as “Cursor” Provide a way to access the elements of an aggregate object sequentially without

exposing its underlying representation. An iterator is used to traverse through the container and access the container’s

elements. The essence of the Iterator Factory method Pattern is to "Provide a way to access

the elements of an aggregate object sequentially without exposing its underlying representation."

Iterator Example Lets take a list as an example to the iterator. There are so many elements in a list and there should be a way to access those

elements without exposing it’s internal structure. There are so many ways to traverse through the list and you don’t want to bloat the lists interface by writing different traversals. That’s where the Iterator comes in to the play.

You can create an Iterator class which defines the interface to access the all the elements of the list and it is responsible of the traversals.

Iterator applicability To access an aggregate object's contents without exposing its internal

representation. To support multiple traversals of aggregate objects. To provide a uniform interface for traversing different aggregate structures (that is,

to support polymorphic iteration).

Thank you !