design patterns

26
OZGUR RAHMI DONMEZ DESIGN PATTERNS

Upload: flint

Post on 19-Feb-2016

21 views

Category:

Documents


0 download

DESCRIPTION

DESIGN PATTERNS. OZGUR RAHMI DONMEZ. WHAT IS DESIGN PATTERN ?. Design Patterns: Elements of Reusable Object-Oriented Software - GoF (Gang of four) Erich Gamma( JUnit ), Richard Helm, Ralp Johnson,John Vlissides Recurring solutions to common software design problems - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DESIGN PATTERNS

O Z G U R RA H M I D O N M E Z

DESIGN PATTERNS

Page 2: DESIGN PATTERNS

WHAT IS DESIGN PATTERN ?

• Design Patterns: Elements of Reusable Object-Oriented Software

-GoF (Gang of four)Erich Gamma(JUnit), Richard Helm, Ralp Johnson,John Vlissides

• Recurring solutions to common software design problems

• Make OO designs more flexible, elegant and reusable. Improve even documentation and maintenance

Page 3: DESIGN PATTERNS

WHAT IS DESIGN PATTERN ?

• Each pattern describes a problem and which occurs over and over again.

• And describes the solution to that problem in such a way that you can use it million times over

Page 4: DESIGN PATTERNS

WHAT IS DESIGN PATTERN ?

• This is not an object oriented course -Encapsulation, Inheritance, polymorphism, interfaces. We assume we know them all.

• Do not worry if you don’t understand all patterns at the first sight.

-Gof says “We didn’t understand all patterns on the first write”

• REFER EXAMPLES AGAIN and AGAIN , FIND PATTERN FITS YOUR PROBLEM

Page 5: DESIGN PATTERNS

1.CREATIONAL PATTERNS

• Factory • Abstract Factory• Prototype• Builder• Singleton

Page 6: DESIGN PATTERNS

FACTORY

When to use : We have many objects of the same type.And want to delegate creation of this objects from clients to the “object factory”. i.e. : Circle,Square,Line (type : shape)

• Decouples client from the instantiated classes-Client code does not change when creating newly added type of product

• Reduces code duplication (creation of objects)

• Abstract class or interface can be used for abstraction

• Example : AsinusLoggerFactory,MCSConnectionFactory…

Page 7: DESIGN PATTERNS

ABSTRACT FACTORY

When to use : We have many objects of the same type and every object belongs to a family.And we want to delegate creation of these objects from clients to the “factory of family factories”.

i.e. : MACCircle,MACSquare,MACLine,WindowsCircle,WindowsSquare,WindowsLine,UnixCircle,UnixSquare,UnixLine(type : shape, family : os)

• One more level of abstraction

Same benefits as Factory pattern.

Example : URLPathGeneratorFactory . (factory produced object HTMLURLPathGenerator is also a factory)

Page 8: DESIGN PATTERNS

SINGLETON

When to use : Sometimes it is important to have only one instance of a class. i.e. Manager Classes, Factory classes

• Instantiates itself, only one instance• Provides a global point of access• For centralized management of resources

Page 9: DESIGN PATTERNS

PROTOTYPE

When to use : Sometimes object cloning is better than object creation• Object creation is expensive or too complex• Decouples client from the instantiated classes

In java it is easier because there is a Cloneable Interface.

Page 10: DESIGN PATTERNS

BUILDERWhen to use : -To create complex (or composite) objects with parts those must be created in the same order. -There is a step by step creation sequence.

i.e. To draw animal picture : first draw head,second body, third legs…. Same construction sequence can be applied when drawing all animals.

• Isolate (or encapsulate) construction process of an object from its representation. Finer control over construction process.

• Code reusability.

Page 11: DESIGN PATTERNS

2.BEHAVIORAL PATTERNS

• Command• Chain of Responsibility• Mediator• Observer• State• Strategy• Visitor• Memento

Page 12: DESIGN PATTERNS

COMMAND

When to use : To encapsulate client requests as objects. We can save (and execute now or later), redo or undo them.They store enough information to be executed later.

Page 13: DESIGN PATTERNS

CHAIN OF RESPONSIBILITY

When to use : We can have several objects that can do some job on the same object but we do not want to know which one or ones actually is doing it.• Decouples the sender of the request to its

recievers

Example : web filtersExample : Response Processors

Page 14: DESIGN PATTERNS

OBSERVER

When to use : Imagine there are number of objects who depend on another object’s state. All dependent objects can be notified (when state changes) with using OBSERVER pattern.

Example : ConfigurationManager

Page 15: DESIGN PATTERNS

MEDIATOR

When to use : We have lots of classes interacting each other-spagetti relations between them.

• Mediator objects encapsulates all interactions.• Dependency minimized. All classes depends on

Mediator.

Page 16: DESIGN PATTERNS

STRATEGYWhen to use : One class have more than one behavior (or have behavior family) We can encapsulate these behaviors and make them interchangeable.• Use this pattern if you need to use one of the several

algorithms dynamically• Behaviors decoupled from client and managed independently• Switch , if statement hater pattern

Examples :BasicAuthenticationStrategy,FacebookAuthenticationStrategy,OpenIdAuthenticationStrategy…JDBC,Spring,Hibernate DataAccess strategy…

Page 17: DESIGN PATTERNS

VISITOR

When to use : If there may lots of unrelated operations defined on some objects structures. We can separate (or decouple) those operation logics from objects using VISITOR pattern.

• Reduces unnecessary interfaces belong to those operation• Add one more operation is very easy

Page 18: DESIGN PATTERNS

STATE

When to use : Some objects changes behavior when their states changes. Those objects appear to have lots of if statements in the methods.If state == …;else if state == …;else if state == …i.e. : Vending machine• Delegates state behaviors to different STATE

classes• If , switch statement hater again

Page 19: DESIGN PATTERNS

MEMENTO

When to use : To record an object internal state without violating encapsulation, reclaim it later without knowlegde of the original object.

• Memento can UNDO via restoring STATE• Command pattern can UNDO via compansating

actions

Page 20: DESIGN PATTERNS

3.STRUCTURAL PATTERNS

• Adapter• Facade• Bridge• Decorator• Composite• Proxy

Page 21: DESIGN PATTERNS

ADAPTER PATTERN

When to use : Have a class almost does what I need. But wrong access methods. Has Incompatible interface that I need.Build (a Wrapper class) on incompatible class with adapter pattern.

Example : In Java primitive int wrapped by Integer ->To make it object. Has toString,hashcode methods…

• Example : You can apply “ADAPTER PATTERN” to 3rd party APIs that you do not have control on them. No

Page 22: DESIGN PATTERNS

FACADE PATTERN

When to use : If we want a simpler interface in front of interfaces.

• Honestly, two patterns could be implemented the same way programmatically -- the difference is in intent.

• Façade is also an adapter (also an wrapper) but it is for providing a simpler “new” interface , not to adapt to an existing one.

• A facade is designed to organize multiple services behind a single service gateway. An adapter is designed to provide a way to use a known interface to access an unknown one.

Page 23: DESIGN PATTERNS

BRIDGE PATTERN

When to use : Same UML diagram as STRATEGY pattern Honestly, two patterns could be implemented the same way programmatically -- the difference is in intent.

• Strategy pattern abstracts behaviour.• Strategy pattern abstracts the

implementation.

Page 24: DESIGN PATTERNS

COMPOSITE PATTERN

When to use : To treat the nodes and the leafs in the same manner in the tree structure.Example : Directory is also handled like a file. File and Directory has some common interface. There is a tree structure.

• Client doesn’t know the differences between the composition of object(Node) and the individual object(Leaf).

Page 25: DESIGN PATTERNS

DECORATOR PATTERN

When to use : To add additional responsibilities dynamically without affecting the original object and other objects.

• Uses composition instead of inheritance. • More flexibility than inheritance• Object hierarchy does not grow deeper

• Example : sitemesh

Page 26: DESIGN PATTERNS

PROXY PATTERNWhen to use : To control access to an object. • Representing expensive resource or impossible to duplicate:

Network connection, large object in memory, open fileWHAT IS THE DIFFERENCE (PROXY and DECORATOR)• Decorator Pattern focuses on dynamically adding functions

to an object• Proxy Pattern focuses on controlling access to an object• In Proxy Pattern, we usually create an instance of abject

inside the proxy class. • In Decorator Pattern, we typically pass the original object as

a parameter to the constructor of the decorator.