Bartlesville Dot Net User Group Design Patterns


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Bartlesville Dot Net User Group Design Patterns

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