Presentation on theme: "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."— Presentation transcript:
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, 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
Design Patterns Template TermDescription Pattern NameDescribes the essence of the pattern in a short, but expressive, name IntentDescribes what the pattern does Also Known AsList any synonyms for the pattern MotivationProvides an example of a problem and how the pattern solves that problem ApplicabilityLists the situations where the pattern is applicable StructureSet of diagrams of the classes and objects that depict the pattern ParticipantsDescribes the classes and objects that participate in the design pattern and their responsibilities CollaborationsDescribes how the participants collaborate to carry out their responsibilitiescollaborate ConsequencesDescribes the forces that exist with the pattern and the benefits, trade-offs, and the variable that is isolated by the pattern
Namemeaningful text that reflects the problem, e.g. Bridge, Mediator, Flyweight Problem addressedintent of the pattern, objectives achieved within certain constraints Contextcircumstances under which it can occur Forcesconstraints or issues that solution must address, forces may conflict! Solutionthe static and dynamic relationships among the pattern components. Structure, participants, collaboration. Solution must resolve all forces! 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
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.
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
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. Some Proxy Flavours!
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.
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 Books Design patterns: Elements of Reusable Object Oriented Software (Addison-Wesley, 1995) Head First Design Patterns - O’Reilly Media