weblogs.asp.

net/meligy

Mohamed Meligy Senior Software Engineer SilverKey Tech.

.

0 Part 1 .Design Patterns via C# 3.

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 .

GoF Once. there was a gang… .

23 Classic patterns in 3 main categories • Creational The process of object creation • Singleton • Abstract Factory • Structural The composition of classes and objects • Adapter • Decorator • Behavioral How 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 .

.

Plugins “Provide an interface for creating families of dependent objects without specifying their concrete classes” GoF . Product2. not the exact class that implements it • Players: • Abstract Factory • Factory • Product1.Let the factory decide what product to create! You want the client code to only call the interface. … • Client • Example: Logging.

.

” 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.

.

-) Extend the object without touching it! • Players: • Component • ConcreteComponent • Decorator • ConcreteDecorator • Example: Streams “Attach additional responsibilities to an object dynamically” GoF .He should not know he’s being decorated .

.

0 Part 2 .Design Patterns via C# 3.

and make them interchangeable”. use it. GoF .Define a strategy first. • Players: • Strategy • ConcreteStrategy • Context • Example: Domain Validation “Define a family of algorithms. encapsulate each one. and reuse it. Define the strategy interface. and use it in other classes. even change it when needed.

.

Logging.” GoF . • Receiver thereby letting you parameterize clients • Example: with different requests. and use it in other classes. etc… Define the strategy interface.Transaction. even change it when needed. and support undoable operations. queue or log Queue. Undo requests. Unit Of Work. • Players: • Command • ConcreteCommand • Client • Invoker “Encapsulate a request as an object.

.

Direct • Duplicate Code (DRY) • Large Method/Class (SoC) • Long Parameter List (Single Responsibility) • Comments • Very Little • Too Much Indirect • Difficulty in changing class • Difficulty in having new behavior • Dependencies .

.

.

za/examples.0 Design Patterns (book – O’Reilly) patterns.up.GoF Patterns Other Patterns martinfowler.com • codeproject.aspx • • C# 3.microsoft.com • aspalliance. A tutorial (book – Addison • Wesley) • dofactory.ac.com (WARNING: java) codebetter. and Practices in C# msdn.cs.com .com/enus/library/aa137892.com/blogs/ david.hayden (book) Agile Principles Patterns.• shtml • • C# Design Patterns.

Sign up to vote on this title
UsefulNot useful