design patterns via c# 3.0

Post on 22-May-2015

7.954 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

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

Mohamed MeligySenior Software Engineer

SilverKey Tech.

weblogs.asp.net/meligy

Design Patterns via C# 3.0Part 1

Objects, Objects, Objects, …

You thought you knew those little chunks…

• Class• Interface• Inheritance

• Encapsulation• Abstraction• Polymorphism

• SoC• OCP• DRY• …

Single Responsibility”“

You should not affect global properties

“Don’t Repeat Yourself ”Keep it DRY …

Open / Closed!Extension / Modification

Once, there was a gang…

GoF

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

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

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

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

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

Design Patterns via C# 3.0Part 2

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

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

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

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

top related