bartlesville dot net user group design patterns
DESCRIPTION
TRANSCRIPT
C# Design PatternsJason TownsendBartlesville .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.
Factory Method PatternCreates 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.
Singleton PatternRestricts 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.
Façade PatternProvides 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.
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.
Command PatternCreates 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.
Iterator PatternAccesses 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
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 PatternAllows one of a family of algorithms to be selected on-the-fly at runtime.
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