C# Design Patterns Jason Townsend Bartlesville .NET User Group
The Gang of Four Available at  http://www.is.gd/wlb Has become the reference book for design patterns. Prerequisite for understanding modern patterns.
Why Design Patterns? Design patterns draw on years of design expertise. Using design patterns is reuse of tried software engineering techniques. Design patterns create and efficient vocabulary for talking about software design.
Creational Patterns Abstract Factory Builder Factory Method Prototype Singleton These patterns have to do with class instantiation. They can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation to get the job done.
Abstract Factory Groups object factories that have a common theme.
Abstract Factory Details High Frequency of Use Good to use when you need more control over the creation process. Beneficial for object caching, maintaining object counts, maintaining type counts, reuse of objects.
Abstract Factory in .NET Framework You can find the abstract factory pattern in the DbProviderFactory and the DbProviderFactories on the ADO .NET libraries. Naming convention in the framework is to append Factory to the name of the type that will be created. Used in Enterprise Library, Smart Client Factory, Web Services Factory, and Web Client Factory. Try searching the Framework for more factories.
Builder Pattern Constructs complex objects by separating construction and representation.
Builder Details Medium to Low Frequency of Use. Used frequently for code generators. Gives more control over each step of the creation process than the factory patterns.
Builder in .NET Framework Not widely used. Can find in VBCodeProvider and CSharpCodeProvider with the CreateGenerator methods.  These return a ICodeGenerator interface which can be used to control the code generation.
Factory Method Pattern Creates objects without specifying the exact class to create.
Factory Method Details High frequency of use. Use when the client application will not know which classes to instantiate.  Key objective of Factory Method Pattern is extensibility.
Prototype Pattern Creates objects by cloning an existing object.
Singleton Pattern Restricts object creation for a class to only one instance
Singleton Pattern Details High frequency of use. A singleton can be used when you have an object that is created multiple times yet is stateless, this will improve performance. Often used for global variables.  Global variables are often considered a bad coding practice, however there are times when they are necessary.
Singleton in .NET Framework Singletons are used with .NET remoting when launching server-activated objects.  Through the Singleton activation mode.
Structural Patterns Adapter Bridge Composite Decorator Façade Flyweight Proxy These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
Adapter Pattern Allows classes with incompatible interfaces to work together by wrapping it’s own interface around that of an already existing class.
Bridge Pattern Decouples an abstraction from its implementation so that the two can vary independently
Composite Pattern Composes one-or-more similar objects so that they can be manipulated as one object.
Decorator Pattern Dynamically adds/overrides behavior in an existing method of an object.
Façade Pattern Provides a simplified interface to a large body of code
Façade Pattern Details High frequency of use. Provide a unified interface to a set of interfaces in a subsystem. More common pattern in 3-tier architecture modeled applications. Hide the ugly. Often implemented as singleton abstract factories. Can implement using static methods on the Façade Using Façade should be a conscious decisions.  Facades take control away from the user of you API, and may decrease performance.  Must way performance vs development time.
Façade in .NET Framework Microsoft calls them aggregate components, in component-oriented designs. Can find in System.Diagnostics.EventLog, System.Web.Mail.SmtpMail, System.IO.SerialPort., System.Messaging.MessageQueue.
Flyweight Pattern Reduces the cost of creating and manipulating a large number of similar objects
Proxy Pattern Provides a placeholder for another object to control access, reduce cost, and reduce complexity
Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor These design patterns are about classes objects communication. They are specifically concerned with communication between  objects .
Chain of Responsibility Pattern Delegates commands to a chain of processing objects.
Command Pattern Creates objects which encapsulate actions and parameters.
Command Pattern Details High frequency of use. Encapsulates an action or request as an object. Classic usage is in menuing systems. All commands implement the same interface, so they can be handled polymorphically. Typically the interface include Do/Undo or Execute/Undo.
Command in .NET Framework Used in Smart Client Software Factory. Used in Web Service Software Factory. Used in Web Client Software Factory. ADO.NET DbCommand. Used in Visual Studio .NET for the menuing system, toolbars, etc… One of the biggest complaints is the Microsoft did not include that pattern as part of a larger unified WinForms command routing architecture.
Interpreter Pattern Implements a specialized language.
Iterator Pattern Accesses the elements of an object sequentially without exposing its underlying representation.
Iterator Pattern Details High Frequency of Use Used to traverse and manipulate a collection of objects. Common traversals are front/back, back/front, every x object, etc… Seperates the collection of objects from the traversal logic.
Iterator in .NET Framework Iterator is actually a part of the .NET language itself: Foreach IEnumerable / IEnumerator Array, ArrayList, AttributesCollection, Generics
Mediator Pattern Allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
Memento Pattern Provides the ability to restore an object to its previous state (undo).
Observer Pattern Publish/subscribe pattern which allows a number of observer objects to see an event.
State Pattern Allows an object to alter its behavior when its internal state changes.
Strategy Pattern Allows one of a family of algorithms to be selected on-the-fly at runtime.
Template Pattern Defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
Visitor Pattern Separates an algorithm from an object structure by moving the hierarchy of methods into one object.
 
Further Resources My Blog http://www.okcodemonkey.com Linkedin http://www.linkedin.com/in/okcodemonkey Bartlesville .NET User Group http://www.bdnug.com Twitter http://twitter.com/okcodemonkey Email [email_address]

Bartlesville Dot Net User Group Design Patterns

  • 1.
    C# Design PatternsJason Townsend Bartlesville .NET User Group
  • 2.
    The Gang ofFour Available at http://www.is.gd/wlb Has become the reference book for design patterns. Prerequisite for understanding modern patterns.
  • 3.
    Why Design Patterns?Design patterns draw on years of design expertise. Using design patterns is reuse of tried software engineering techniques. Design patterns create and efficient vocabulary for talking about software design.
  • 4.
    Creational Patterns AbstractFactory Builder Factory Method Prototype Singleton These patterns have to do with class instantiation. They can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation to get the job done.
  • 5.
    Abstract Factory Groupsobject factories that have a common theme.
  • 6.
    Abstract Factory DetailsHigh Frequency of Use Good to use when you need more control over the creation process. Beneficial for object caching, maintaining object counts, maintaining type counts, reuse of objects.
  • 7.
    Abstract Factory in.NET Framework You can find the abstract factory pattern in the DbProviderFactory and the DbProviderFactories on the ADO .NET libraries. Naming convention in the framework is to append Factory to the name of the type that will be created. Used in Enterprise Library, Smart Client Factory, Web Services Factory, and Web Client Factory. Try searching the Framework for more factories.
  • 8.
    Builder Pattern Constructscomplex objects by separating construction and representation.
  • 9.
    Builder Details Mediumto Low Frequency of Use. Used frequently for code generators. Gives more control over each step of the creation process than the factory patterns.
  • 10.
    Builder in .NETFramework Not widely used. Can find in VBCodeProvider and CSharpCodeProvider with the CreateGenerator methods. These return a ICodeGenerator interface which can be used to control the code generation.
  • 11.
    Factory Method PatternCreates objects without specifying the exact class to create.
  • 12.
    Factory Method DetailsHigh frequency of use. Use when the client application will not know which classes to instantiate. Key objective of Factory Method Pattern is extensibility.
  • 13.
    Prototype Pattern Createsobjects by cloning an existing object.
  • 14.
    Singleton Pattern Restrictsobject creation for a class to only one instance
  • 15.
    Singleton Pattern DetailsHigh frequency of use. A singleton can be used when you have an object that is created multiple times yet is stateless, this will improve performance. Often used for global variables. Global variables are often considered a bad coding practice, however there are times when they are necessary.
  • 16.
    Singleton in .NETFramework Singletons are used with .NET remoting when launching server-activated objects. Through the Singleton activation mode.
  • 17.
    Structural Patterns AdapterBridge Composite Decorator Façade Flyweight Proxy These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
  • 18.
    Adapter Pattern Allowsclasses with incompatible interfaces to work together by wrapping it’s own interface around that of an already existing class.
  • 19.
    Bridge Pattern Decouplesan abstraction from its implementation so that the two can vary independently
  • 20.
    Composite Pattern Composesone-or-more similar objects so that they can be manipulated as one object.
  • 21.
    Decorator Pattern Dynamicallyadds/overrides behavior in an existing method of an object.
  • 22.
    Façade Pattern Providesa simplified interface to a large body of code
  • 23.
    Façade Pattern DetailsHigh frequency of use. Provide a unified interface to a set of interfaces in a subsystem. More common pattern in 3-tier architecture modeled applications. Hide the ugly. Often implemented as singleton abstract factories. Can implement using static methods on the Façade Using Façade should be a conscious decisions. Facades take control away from the user of you API, and may decrease performance. Must way performance vs development time.
  • 24.
    Façade in .NETFramework Microsoft calls them aggregate components, in component-oriented designs. Can find in System.Diagnostics.EventLog, System.Web.Mail.SmtpMail, System.IO.SerialPort., System.Messaging.MessageQueue.
  • 25.
    Flyweight Pattern Reducesthe cost of creating and manipulating a large number of similar objects
  • 26.
    Proxy Pattern Providesa placeholder for another object to control access, reduce cost, and reduce complexity
  • 27.
    Behavioral Patterns Chainof Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor These design patterns are about classes objects communication. They are specifically concerned with communication between objects .
  • 28.
    Chain of ResponsibilityPattern Delegates commands to a chain of processing objects.
  • 29.
    Command Pattern Createsobjects which encapsulate actions and parameters.
  • 30.
    Command Pattern DetailsHigh frequency of use. Encapsulates an action or request as an object. Classic usage is in menuing systems. All commands implement the same interface, so they can be handled polymorphically. Typically the interface include Do/Undo or Execute/Undo.
  • 31.
    Command in .NETFramework Used in Smart Client Software Factory. Used in Web Service Software Factory. Used in Web Client Software Factory. ADO.NET DbCommand. Used in Visual Studio .NET for the menuing system, toolbars, etc… One of the biggest complaints is the Microsoft did not include that pattern as part of a larger unified WinForms command routing architecture.
  • 32.
    Interpreter Pattern Implementsa specialized language.
  • 33.
    Iterator Pattern Accessesthe elements of an object sequentially without exposing its underlying representation.
  • 34.
    Iterator Pattern DetailsHigh Frequency of Use Used to traverse and manipulate a collection of objects. Common traversals are front/back, back/front, every x object, etc… Seperates the collection of objects from the traversal logic.
  • 35.
    Iterator in .NETFramework Iterator is actually a part of the .NET language itself: Foreach IEnumerable / IEnumerator Array, ArrayList, AttributesCollection, Generics
  • 36.
    Mediator Pattern Allowsloose coupling between classes by being the only class that has detailed knowledge of their methods.
  • 37.
    Memento Pattern Providesthe ability to restore an object to its previous state (undo).
  • 38.
    Observer Pattern Publish/subscribepattern which allows a number of observer objects to see an event.
  • 39.
    State Pattern Allowsan object to alter its behavior when its internal state changes.
  • 40.
    Strategy Pattern Allowsone of a family of algorithms to be selected on-the-fly at runtime.
  • 41.
    Template Pattern Definesthe skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
  • 42.
    Visitor Pattern Separatesan algorithm from an object structure by moving the hierarchy of methods into one object.
  • 43.
  • 44.
    Further Resources MyBlog http://www.okcodemonkey.com Linkedin http://www.linkedin.com/in/okcodemonkey Bartlesville .NET User Group http://www.bdnug.com Twitter http://twitter.com/okcodemonkey Email [email_address]