220 likes | 321 Views
What Are Design Patterns and Why Do I Need Them?. Software professionals may be familiar with the term "Design Patterns," but many have no idea of where they come from and what they truly are.
E N D
What Are Design Patterns and Why Do I Need Them? • Software professionals may be familiar with the term "Design Patterns," but many have no idea of where they come from and what they truly are. • Consequently, some do not see the value and benefits design patterns bring to the software development process, especially in the areas of maintenance and code reuse. • This talk will provide an introductory to design patterns from a historical perspective.
Is This A New Idea? • Town planners and architects talk of design patterns all the time. • Design patterns have their roots in the work of Christopher Alexander an architect. When talking about patterns in buildings and towns in 1977, he wrote • “Each pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to the problem, in such a way that you can use this solution a million times over, without doing it the same way twice.” Christopher Alexander,
Shortly after, software professionals began to incorporate Alexander’s principle into the creation of early design pattern documentation as a guide to novice developers. • Design patterns gained popularity in computer science after a certain book was published in 1994 by four authors, commonly know as Gang of Four or GOF
Gang of Four The GoF Book • Gang of Four (GoF) Gamma, Helm, Johnson, Vlissides, - founders of movement. • Gamma et al, Design Patterns: Elements of Reusable Object-Oriented Software, Addison Wesley, 1995. • They offer advice and help for developing quality software • An object-oriented design pattern systematically names, explains and evaluates an important and recurring design in object-oriented systems
Different authors use different templates to describe their patterns • Information is not always presented in the same way. • Consult your manual/source !!!
Types of Pattern • There are 3 types of pattern … • Creational: Concerned with the process of object creation • Structural: Concerned with the composition of classes and objects • Behavioral: Concerned with the ways in which classes and objects interact and distribute the work
Types of Design Patterns Creational Structural Behavioural Adapter Bridge Composite Decorator Façade Flyweight Proxy Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Abstract Factory Builder Factory Prototype Singleton
Memento • Intent • Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later. • Applicability – use Memento when: • A snapshot of (some portion of) an object’s state must be saved so that it can be restored to that state later, and • A direct interface to obtaining the state would expose implementation details and break the object’s encapsulation.
Memento Memento x,y Memento save() void restore(Memento) RectCaretaker Rect x,y Memento save() void restore(Memento)
Proxy • Intent • Provide a surrogate or placeholder for another object to control access to it. • Applicability • You want a normally inaccessible or unavailable object to appear like an ordinary object. For example, the object might be: • in a different process • in a disk archive • compressed • not computed yet • Forces • Turning an object into a Proxy, or vice versa, should be transparent
Some Proxy Flavours! • Additional Behaviour Leads to Different Types of Proxies • A remote proxy provides a local representative for an object in a different address space. • A virtual proxy creates expensive objects on demand. (lazy loading) • A protection proxy controls access to the original object Protection proxies are useful when objects should have different access rights. • A cache proxy caches the output of the Subject class. A Proxy can cache stable information about the subject to postpone accessing it.
Cached Proxy CalculatorBase Calculate() Client NumberMultiplierProxy Calculate() NumberMultiplier Calculate()
Virtual Proxy Imager GetImage() ImageName Client ImageProxy GetImage() ImageName QuickImage GetImage() ImageName FinalImage GetImage() ImageName
Participants of Proxy pattern • Proxy • Maintains a reference that lets the proxy access the real object • Provides an interface identical to the Subject’s so that a proxy can be substituted for the real subject. • Controls access to the real subject and may be responsible for creating and deleting it. • Other responsibilities depend on the kind of proxy
State • Intent – Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. • Applicability – use in either of the following cases: – An object’s behavior depends on its state, and it must change its behavior at run-time depending on that state. – Operations have large, multipart conditional statements which depends on the object’s state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object’s state as an object in its own right that can vary independently from other objects.
State IStatus Issue() Pay() Cancel() Invoice PendingState Issue() Pay() Cancel() PaidState Issue() Pay() Cancel() CancelledState Issue() Pay() Cancel()
Summary • Design patterns assist developers by: • Providing templates for common problems • Creating a common nomenclature • Creating components that are more reusable and flexible • The .NET Framework includes features that makes some of the patterns easier to implement.
For more Information • Websites • Microsoft patterns & practices • http://ww.microsoft.com/patterns • http://www.dofactory.com/Patterns/Patterns.aspx • Books • Design patterns: Elements of Reusable Object Oriented Software (Addison-Wesley, 1995) • Head First Design Patterns - O’Reilly Media