design patterns for 70% of programmers in the world

60
Design Patterns for 70% of genious programmers Aamir Khan Solution Architect

Upload: saurabh-moody

Post on 16-Apr-2017

42.309 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Design Patterns For 70% Of Programmers In The World

Design Patternsfor 70% of genious programmers

Aamir KhanSolution Architect

Page 2: Design Patterns For 70% Of Programmers In The World

Design Patternsfor 70% of programmers in the world

Saurabh VermaSolution Architect I blog at http://www.domaindrivendesign.info

Page 3: Design Patterns For 70% Of Programmers In The World

Agenda• Do you OOPs?

• Design Patterns? Why

• Design Patterns? What

• Design Patterns? How

• I hate my job

Page 4: Design Patterns For 70% Of Programmers In The World

Normal Classes vs Abstract Classes vs InterfacesNormal Class: This is a normal class

Abstract Class: These cannot be instantiated directly. They may provide partial implementations and exists solely for the purpose of inheritance.

Interfaces: Defines a contract. Concrete implementations needs to implement each property and method as per contract.

Shut up ! We know all this

Page 5: Design Patterns For 70% Of Programmers In The World

Million Dollar Question? What is Why?

Page 6: Design Patterns For 70% Of Programmers In The World

Normal Class vs Abstract Class?

If a particular class is a entity which is not

required to be instantiated by the

application directly, go for Abstract Class

Employee can be abstract class

Page 7: Design Patterns For 70% Of Programmers In The World

Abstract Class vs Interfaces?

If a particular class contains any default

behavior, go for Abstract Class

If every concrete class has its own

implementation, go forInterfacesWork – Management, Programming

Page 8: Design Patterns For 70% Of Programmers In The World

MOVIES & ACTORS

Shahrukh KhanSalman Khan riding bike

Actors in a Movie

Page 9: Design Patterns For 70% Of Programmers In The World

IS-A InheritanceShahrukh Khan is an ACTOR

HAS-A CompositionMovie has ACTORS

CAN-DO InterfacesSalman Khan can DRIVE BIKE

IS-A vs HAS-A vs CAN-DOIS-A Inheritance

Shahrukh Khan is an ACTOR

HAS-A CompositionMovie has ACTORS

CAN-DO InterfacesSalman Khan can DRIVE BIKE

Page 10: Design Patterns For 70% Of Programmers In The World

What is a Pattern

• An 'element of reusable software’

• A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems.

• It describes the problem, the solution, when to apply the solution, and its consequences.

• It also gives implementation hints and examples. The solution is a general arrangement of objects and classes that solve the problem. The solution is customized and implemented to solve the problem in a particular context.

Page 11: Design Patterns For 70% Of Programmers In The World

History of Software Patterns

Page 12: Design Patterns For 70% Of Programmers In The World

What Wikipedia says,

• A design pattern is a general repeatable solution to a commonly occurring problem in software design.

• A design pattern is not a finished design that can be transformed directly into code.

• Algorithms are not thought of as design patterns.

Page 13: Design Patterns For 70% Of Programmers In The World

Let’s understand design patterns by purpose

Page 14: Design Patterns For 70% Of Programmers In The World

Let’s understand the purpose

+

Creational Patterns

Structural Patterns

Final Product

Behavioral Patterns

Page 15: Design Patterns For 70% Of Programmers In The World

Let’s understand the purpose

Page 16: Design Patterns For 70% Of Programmers In The World

Design Patterns as per their PURPOSE

Page 17: Design Patterns For 70% Of Programmers In The World

Creational Patterns

Page 18: Design Patterns For 70% Of Programmers In The World

Factory Method

Page 19: Design Patterns For 70% Of Programmers In The World

Factory MethodDefine an interface for creating an object, but let subclass decide which class to instantiate

• Prevents creation of any additional instances, while simultaneously allowing global access

• Creation can be delayed until it is actually required

Page 20: Design Patterns For 70% Of Programmers In The World

Factory MethodSystem.Data.SqlClient.SqlClientFactory

Page 21: Design Patterns For 70% Of Programmers In The World

Abstract Factory Pattern

Page 22: Design Patterns For 70% Of Programmers In The World

Abstract Factory PatternProvide an interface for creating families of related or dependent objects without specifying their concrete classes.

• Provides creation of families of related or dependent objects without specifying their concrete class

Page 23: Design Patterns For 70% Of Programmers In The World

Abstract Factory MethodSystem.Data.Common.DbProviderFactories

Page 24: Design Patterns For 70% Of Programmers In The World

Singleton Pattern

Page 25: Design Patterns For 70% Of Programmers In The World

Singleton PatternEnsure a class has only one instance, and provide a global point to it.

• Prevents creation of any additional instances, while simultaneously allowing global access

• Creation can be delayed until it is actually required

Page 26: Design Patterns For 70% Of Programmers In The World

Singleton PatternSystem.Data.SqlClient.SqlClientFactory

Page 27: Design Patterns For 70% Of Programmers In The World

Structural Patterns

Page 28: Design Patterns For 70% Of Programmers In The World

Façade Pattern

Page 29: Design Patterns For 70% Of Programmers In The World

Façade PatternProvide a unified interface to set of interfaces in a subsystem.

• Is generally one side of the exterior of a building, especially the front

• Origin from a FRENCH word means “face” or “front”

Page 30: Design Patterns For 70% Of Programmers In The World

Façade PatternIts all about the service

Page 31: Design Patterns For 70% Of Programmers In The World

Decorator Pattern

Page 32: Design Patterns For 70% Of Programmers In The World

Decorator PatternAttach additional responsibilities to an object dynamically.

• Flexible alternative to subclassing for extending functionality

• Allows dynamic and transparent addition and removal of responsibilities without affecting object

simple actress(act)

in role of modern girl(go out and act)

in role of homely girl(be in home and act)

Page 33: Design Patterns For 70% Of Programmers In The World

Decorator PatternSystem.IO.BufferedStream:Stream

Page 34: Design Patterns For 70% Of Programmers In The World

Composite Pattern

Page 35: Design Patterns For 70% Of Programmers In The World

Composite PatternCompose objects into tree structures to represent part-whole hierarchies

• When dealing with collections of objects, there are often operations that are appropriate for both a single object and the entire collection

• Treats individual objects and composition of objects uniformly

Example: a)Every object “IS-A” shapeb)Every shape “HAS-A” draw behavior

Page 36: Design Patterns For 70% Of Programmers In The World

Composite PatternSystem.Web.UI.Control (Render Mechanism)

Page 37: Design Patterns For 70% Of Programmers In The World

Composite Pattern

Page 38: Design Patterns For 70% Of Programmers In The World

Adapter Pattern

Page 39: Design Patterns For 70% Of Programmers In The World

Adapter PatternConvert the interface of a class into another interface clients expect

• Lets classes work together that couldn’t work otherwise

• Works good for making compliance

ApplicationFrameworkAdapter

LegacyComponent

Page 40: Design Patterns For 70% Of Programmers In The World

Adapter PatternGearsTypelib.HtmlDialogHostClass

(Interop.GearsTypelib)

Page 41: Design Patterns For 70% Of Programmers In The World

Decorator vs AdapterDecorator looks like Adapter which “decorates” an existing class to make it compatible. Though, Decorator doesn’t wants you to decorate every method in the Adapted class. But who has stopped from doing it.

“Never underestimate the power of a developer”

Page 42: Design Patterns For 70% Of Programmers In The World

Decorator vs FaçadeFaçade is a way of hiding a complex system inside a simpler interface, whereas Decorator adds function by wrapping a class.

Page 43: Design Patterns For 70% Of Programmers In The World

Behavioural Patterns

Page 44: Design Patterns For 70% Of Programmers In The World

Command Pattern

Page 45: Design Patterns For 70% Of Programmers In The World

Command PatternEncapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests.

• If all commands can service a specific request, they can be entertained by the invoker

• Concrete command performs the action on Receiver

• Encapsulate method invocationOne Receiver

andMultiple actions

Page 46: Design Patterns For 70% Of Programmers In The World

Command PatternSystem.ComponentModel.Design.MenuCommand

Page 47: Design Patterns For 70% Of Programmers In The World

Command PatternSaurabh.Learning.DesignPatterns.Patterns.CommandPa

ttern

Page 48: Design Patterns For 70% Of Programmers In The World

Strategy Pattern

Page 49: Design Patterns For 70% Of Programmers In The World

Strategy PatternDefine strategies for a performing a behavior and change them dynamically as per requirement

• This lets the algorithm vary independently from clients that uses it

• It is about choice, which affects outcomes

Page 50: Design Patterns For 70% Of Programmers In The World

Strategy PatternSystem.Collections.ArrayList

Page 51: Design Patterns For 70% Of Programmers In The World

Template Method

Page 52: Design Patterns For 70% Of Programmers In The World

Template MethodDefine the skeleton of an algorithm in an operations, deferring some steps to subclass

• Main algorithm skeleton is defined in a base class and subclasses can then plug in their own details without affecting the algorithm itself

• Uses Inheritance – Abstract Class defines the template (algorithm) and Concrete class manages the implementation.

• Template uses the implementation on demand

Page 53: Design Patterns For 70% Of Programmers In The World

Template Method

Page 54: Design Patterns For 70% Of Programmers In The World

Strategy Pattern vs Template Method• Strategy allows callers to change the algorithm or order of steps whereas Template method allow steps to be modified

• Strategy uses Interfaces whereas Template method uses Inheritance

Page 55: Design Patterns For 70% Of Programmers In The World

I hate my job

Page 56: Design Patterns For 70% Of Programmers In The World

Top 5 ADVICES NOT TO FOLLOW

Page 57: Design Patterns For 70% Of Programmers In The World

Reflect when you want

Page 58: Design Patterns For 70% Of Programmers In The World

Answers, anybody?

Page 59: Design Patterns For 70% Of Programmers In The World

CONTENT STOLEN FROM BOOKS:Head First Design PatternsDesign Patterns in C#Design PatternsWEBSITES:Wikipedia, MSDN, doFactory and GOOGLE

Page 60: Design Patterns For 70% Of Programmers In The World

Learn it all, at my blog http://www.domaindrivendesign.info

Don’t contact me at,saurabh.net [at] gmail.com

Saurabh VermaSolution Architect