Design Patterns Via C# 3.0

28
Mohamed Meligy Senior Software Engineer SilverKey Tech. weblogs.asp.net/meligy

description

introduces some Object Oriented Programming (OOP) design principles and 6 of the 23 Gang of Four (GoF) classic design patterns as well as introduction to other topics like C# 3.0 and Domain Driven Design (DDD) and Model View Controller (MVC)

Transcript of Design Patterns Via C# 3.0

Page 1: Design Patterns Via C# 3.0

Mohamed MeligySenior Software Engineer

SilverKey Tech.

weblogs.asp.net/meligy

Page 2: Design Patterns Via C# 3.0
Page 3: Design Patterns Via C# 3.0
Page 4: Design Patterns Via C# 3.0

Design Patterns via C# 3.0Part 1

Page 5: Design Patterns Via C# 3.0

Objects, Objects, Objects, …

Page 6: Design Patterns Via C# 3.0

You thought you knew those little chunks…

• Class• Interface• Inheritance

• Encapsulation• Abstraction• Polymorphism

• SoC• OCP• DRY• …

Page 7: Design Patterns Via C# 3.0

Single Responsibility”“

You should not affect global properties

Page 8: Design Patterns Via C# 3.0

“Don’t Repeat Yourself ”Keep it DRY …

Page 9: Design Patterns Via C# 3.0

Open / Closed!Extension / Modification

Page 10: Design Patterns Via C# 3.0

Once, there was a gang…

GoF

Page 11: Design Patterns Via C# 3.0

23 Classic patterns in 3 main categories

• CreationalThe process of object creation

• Singleton• Abstract Factory

• StructuralThe composition of classes and objects

• Adapter• Decorator

• BehavioralHow classes and objects interact and distribute the work

• Strategy• Command

Page 12: Design Patterns Via C# 3.0

There must be one, and only one …

You want only one instance to be exposed from your singleton class to your clients

• Players:• Singleton

The class that exposes only one object of itself• Client

The code that calls the instance from this class

• Example:HttpContext.Current

Page 13: Design Patterns Via C# 3.0
Page 14: Design Patterns Via C# 3.0

Let the factory decide what product to create!

You want the client code to only call the interface, not the exact class that implements it

• Players:• Abstract Factory• Factory• Product1, Product2, …• Client

• Example:Logging, Plugins

“Provide an interface for creating

families of dependent objects without

specifying their concrete classes”

GoF

Page 15: Design Patterns Via C# 3.0
Page 16: Design Patterns Via C# 3.0

Plug this code into this other code please

Encapsulates the interface of some code inside another interface

• Players:• Adaptee• Target • Adapter • Client

• Example:Serialization, DTOs

“Convert the interface of a class into

another interface clients expect.”

GoF

Page 17: Design Patterns Via C# 3.0
Page 18: Design Patterns Via C# 3.0

He should not know he’s being decorated ;-)

Extend the object without touching it!

• Players:• Component • ConcreteComponent• Decorator• ConcreteDecorator

• Example:Streams

“Attach additional responsibilities to an

object dynamically”

GoF

Page 19: Design Patterns Via C# 3.0
Page 20: Design Patterns Via C# 3.0

Design Patterns via C# 3.0Part 2

Page 21: Design Patterns Via C# 3.0

Define a strategy first, use it, and reuse it.

Define the strategy interface, and use it in other classes, even change it when needed.

• Players:• Strategy• ConcreteStrategy• Context

• Example:Domain Validation “Define a family of algorithms,

encapsulate each one, and make them

interchangeable”.

GoF

Page 22: Design Patterns Via C# 3.0
Page 23: Design Patterns Via C# 3.0

Transaction, Unit Of Work, etc…

Define the strategy interface, and use it in other classes, even change it when needed.

• Players:• Command• ConcreteCommand• Client• Invoker• Receiver

• Example:Queue, Logging, Undo

“Encapsulate a request as an object,

thereby letting you parameterize clients

with different requests, queue or log

requests, and support undoable

operations.”

GoF

Page 24: Design Patterns Via C# 3.0
Page 25: Design Patterns Via C# 3.0

Direct Indirect

• Duplicate Code (DRY)• Large Method/Class

(SoC)• Long Parameter List

(Single Responsibility)• Comments

• Very Little• Too Much

• Difficulty in changing class• Difficulty in having new

behavior• Dependencies

Page 26: Design Patterns Via C# 3.0
Page 27: Design Patterns Via C# 3.0
Page 28: Design Patterns Via C# 3.0

GoF Patterns Other Patterns

• C# 3.0 Design Patterns (book – O’Reilly)

patterns.cs.up.ac.za/examples.shtml

• C# Design Patterns, A tutorial (book – Addison Wesley)

• dofactory.com• codeproject.com• aspalliance.com

• martinfowler.com(WARNING: java)

• codebetter.com/blogs/david.hayden

• (book) Agile Principles Patterns, and Practices in C#

• msdn.microsoft.com/en-us/library/aa137892.aspx