0% found this document useful (0 votes)
88 views

Mohamed Meligy Senior Software Engineer Silverkey Tech

The document introduces design patterns in software engineering including creational patterns like singleton and abstract factory that deal with object creation, structural patterns like adapter and decorator that deal with class composition, and behavioral patterns like strategy and command that deal with object interaction and distributing work. It provides examples of how different design patterns like singleton, abstract factory, adapter, and decorator can be implemented in C# code to make objects reusable and extensible. The document also recommends additional resources for learning more about design patterns and best practices in C#.

Uploaded by

Mohamed Samy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Mohamed Meligy Senior Software Engineer Silverkey Tech

The document introduces design patterns in software engineering including creational patterns like singleton and abstract factory that deal with object creation, structural patterns like adapter and decorator that deal with class composition, and behavioral patterns like strategy and command that deal with object interaction and distributing work. It provides examples of how different design patterns like singleton, abstract factory, adapter, and decorator can be implemented in C# code to make objects reusable and extensible. The document also recommends additional resources for learning more about design patterns and best practices in C#.

Uploaded by

Mohamed Samy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

weblogs.asp.

net/meligy

Mohamed Meligy Senior Software Engineer SilverKey Tech.

Design Patterns via C# 3.0


Part 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

Dont 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

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 hes 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.0


Part 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 Encapsulate a request as an object, Receiver thereby letting you parameterize clients Example: with different requests, queue or log Queue, Logging, Undo requests, and support undoable
operations.
GoF

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

GoF Patterns

Other Patterns
martinfowler.com (WARNING: java) codebetter.com/blogs/ david.hayden (book) Agile Principles Patterns, and Practices in C# msdn.microsoft.com/enus/library/aa137892.aspx

C# 3.0 Design Patterns (book OReilly) patterns.cs.up.ac.za/examples. shtml C# Design Patterns, A tutorial (book Addison Wesley) dofactory.com codeproject.com aspalliance.com

You might also like