Presentation on theme: "Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they?"— Presentation transcript:
N.A.Shulver@staffs.ac.uk Design Patterns Introduction What is a Design Pattern? Why were they developed? Why should we use them? How important are they? How easy are they to use? Pattern types Example Patterns Resources
N.A.Shulver@staffs.ac.uk Design Patterns What is a Design Pattern? When programming complex systems, some problems arise again and again. Programmers build up expertise and experience of solving these problems. A design pattern can be thought of as an encapsulation of programming best practice. Languages like Java already incorporate some design patterns into their structure.
N.A.Shulver@staffs.ac.uk Design Patterns What is a Design Pattern? A design pattern is not necessarily a single chunk of code (detailed implementation). It may be a style, an approach to solving a problem (architecture). It may be the “best” approach - one that experience has shown to be efficient, reliable, scalable and so on. Many patterns come with UML documentation.
N.A.Shulver@staffs.ac.uk Design Patterns Use Case patterns
N.A.Shulver@staffs.ac.uk Design Patterns Use Case patterns
N.A.Shulver@staffs.ac.uk Design Patterns Class Patterns
N.A.Shulver@staffs.ac.uk Design Patterns What is a Design Pattern? “Design patterns constitute a set of rules describing how to accomplish certain tasks in the realm of software development.” (Pree, 1994) “Design patterns focus more on reuse of recurring architectural design themes, while frameworks focus on detailed design… and implementation.” (Coplien & Schmidt, 1995).
N.A.Shulver@staffs.ac.uk Design Patterns What is a Design Pattern? “A pattern addresses a recurring design problem that arises in specific design situations and presents a solution to it” (Buschmann, et. al. 1996) “Patterns identify and specify abstractions that are above the level of single classes and instances, or of components.” (Gamma, et al., 1993)
N.A.Shulver@staffs.ac.uk Design Patterns Why were they developed? Attempts to make programming into an engineering discipline rather than an art form. Can help to keep costs low. Brings reliability through good design. Speeds development cycles since no need to “reinvent the wheel”. Helps with maintenance (patterns may get updated).
N.A.Shulver@staffs.ac.uk Design Patterns Why should we use them? Design Patterns should help us achieve our development goals with less effort. Communities of programmers will have familiarity with common patterns. Modern languages incorporate some patterns at the API level, for example the Java media loading and event handling systems.
N.A.Shulver@staffs.ac.uk Design Patterns How important are they? In some areas (finance, engineering, user interface design) they are very useful and widely used. For some small, ad-hoc, single-developer projects, design patterns may not have much impact.
N.A.Shulver@staffs.ac.uk Design Patterns How easy are they to use? Some design patterns are very simple to use, others are complex solutions to specific, complex problems. You probably use some already. They are well documented and explained.
N.A.Shulver@staffs.ac.uk Design Patterns Pattern Types Creational patterns build objects for you, so your program does not have to explicitly instantiate some objects. This gives run-time flexibility. Structural patterns help impose order on groups of objects, for example on interrelated GUI components. Behavioral patterns aid in defining communication and data flow between objects.
N.A.Shulver@staffs.ac.uk Design Patterns A Few Example Patterns The design patterns philosophy recommends: Program to an interface (or abstract class) and not to an implementation. Favour object composition (encapsulation) over inheritance. The original Design Patterns book (GoF) describes 23 patterns. These are the core patterns. Others have been added over time.
N.A.Shulver@staffs.ac.uk Design Patterns Example Patterns The Factory Method part of a “decision making” class. It returns one of several possible subclasses of an abstract base class depending on some input data. Example: Your program reads and decodes compressed image data. It supports many compression formats by having a factory choose an appropriate decoder class. All decoders are based on one abstract parent class.
N.A.Shulver@staffs.ac.uk Design Patterns Example Patterns The Singleton Pattern a class of which there can be no more than one instance. It provides a single global point of access to that instance. Useful for resource management-related classes like print spoolers or audio output managers where there must be just one object in control. NB the “Math” class in Java – declared final, with static methods only. One way of making a singleton.
N.A.Shulver@staffs.ac.uk Design Patterns Example Patterns The Adapter Pattern provide an interface in one class to resemble another. An adapter class can be derived from a non- conforming parent and new methods added to make it fit some other API. [class adapter] Or we can include (encapsulate) the original class inside our new one and write API conversion methods. [object adapter] Handy pattern when updating an application to use newer libraries, or to interface with some slightly different client or server layer.
N.A.Shulver@staffs.ac.uk Design Patterns Example Patterns The Façade pattern allows us to simplify program complexity. As a system grows, so does the complexity of the system. We can provide a simplified interface to the system. This simplification may reduce flexibility, but can provide all the functionality required for the majority of situations. Of course we can directly access the underlying classes and methods if we need to. A well known example of a façade is the JDBC interface.
N.A.Shulver@staffs.ac.uk Design Patterns Example Patterns The Strategy Pattern allows you to select one of several algorithms dynamically by encapsulating an algorithm inside a selectable class. The algorithms implement a common interface but may be otherwise unrelated. This helps us avoid the messy conditional statements that can make code hard to read and maintain. E.g. Encode an audio stream as mono, 22KHz, PCM. We manually instantiate a strategy class that does this, then pass it to generic processing methods to handle the data stream (cf Factory pattern).
N.A.Shulver@staffs.ac.uk Design Patterns Resources Design Patterns Java Companion (free book in PDF format): http://www.patterndepot.com/put/8/JavaPatterns.htm http://www.patterndepot.com/put/8/JavaPatterns.htm Example patterns with explanatory text, Antonio García: http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/defa ult.htm http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/defa ult.htm Sun J2EE Patterns: http://java.sun.com/blueprints/patterns/http://java.sun.com/blueprints/patterns/ Java Design Patterns 201: http://www- 106.ibm.com/developerworks/edu/j-dw-javapatt2-i.htmlhttp://www- 106.ibm.com/developerworks/edu/j-dw-javapatt2-i.html Core J2EE Patterns (book on Amazon)book on Amazon Important book: Design Patterns by Gamma, Helm, Johnson and Vlissides (Addison Wesley Longman, 1995. ISBN 0-201-63361-2)Design Patterns Interesting review of the above book here.here
N.A.Shulver@staffs.ac.uk Design Patterns Conclusion Design patterns are worth using in many circumstances. You will have to invest some time in order to understand them and absorb the implications of this style of programming. For corporate developers, design patterns represent a resource of expertise that cannot be ignored.