1 / 22

What Are Design Patterns and Why Do I Need Them?

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.

Télécharger la présentation

What Are Design Patterns and Why Do I Need Them?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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,

  3. 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

  4. 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

  5. Design Patterns Template

  6. Different authors use different templates to describe their patterns • Information is not always presented in the same way. • Consult your manual/source !!!

  7. 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

  8. 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

  9. 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.

  10. Memento

  11. Memento Memento x,y Memento save() void restore(Memento) RectCaretaker Rect x,y Memento save() void restore(Memento)

  12. 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

  13. Proxy

  14. 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.

  15. Cached Proxy CalculatorBase Calculate() Client NumberMultiplierProxy Calculate() NumberMultiplier Calculate()

  16. Virtual Proxy Imager GetImage() ImageName Client ImageProxy GetImage() ImageName QuickImage GetImage() ImageName FinalImage GetImage() ImageName

  17. 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

  18. 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.

  19. State Design Pattern

  20. State IStatus Issue() Pay() Cancel() Invoice PendingState Issue() Pay() Cancel() PaidState Issue() Pay() Cancel() CancelledState Issue() Pay() Cancel()

  21. 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.

  22. 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

More Related