Unformatted text preview:

More on Design CSCI 5828 Foundations of Software Engineering Lecture 23 Kenneth M Anderson Outline Additional Design Related Topics Design Patterns Design by Convention Singleton Strategy Model View Controller Inversion of Control also Dependency Injection Refactoring high level overview A graphical example details in a later lecture Design Patterns Addison Wesley book published in 1995 Authors ISBN 0 201 63361 2 Erich Gamma Richard Helm Ralph Johnson John Vlissides Known as The Gang of Four Presents 23 Design Patterns What are Patterns Christopher Alexander talking about buildings and towns Each pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to that problem in such a way that you can use this solution a million times over without ever doing it the same way twice Alexander et al A Pattern Language Oxford University Press 1977 Patterns continued Patterns can have different levels of abstraction In Design Patterns the book Patterns are not classes Patterns are not frameworks Instead Patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context Patterns continued So patterns are formalized solutions to design problems They describe techniques for maximizing flexibility extensibility abstraction etc These solutions can typically be translated to code in a straightforward manner Elements of a Pattern Pattern Name More than just a handle for referring to the pattern Each name adds to a designer s vocabulary Enables the discussion of design at a higher abstraction The Problem Gives a detailed description of the problem addressed by the pattern Describes when to apply a pattern Often with a list of preconditions Elements of a Pattern continued The Solution Describes the elements that make up the design their relationships responsibilities and collaborations Does not describe a concrete solution Instead a template to be applied in many situations Elements of a Pattern continued The consequences Describes the results and tradeoffs of applying the pattern Critical for evaluating design alternatives Typically include Impact on flexibility extensibility or portability Space and Time tradeoffs Language and Implementation issues Design Pattern Template Pattern Name and Classification Creational Structural Behavioral Intent Also Known As Motivation Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related Patterns Examples Singleton Strategy Model View Controller Singleton Intent Ensure a class has only one instance and provide a global point of access to it Motivation Some classes represent objects where multiple instances do not make sense or can lead to a security risk e g Java security managers Singleton continued Applicability Use the Singleton pattern when there must be exactly one instance of a class and it must be accessible to clients from a well known access point when the sole instance should be extensible by subclassing and clients should be able to use an extended instance without modifying their code Singleton Structure Singleton static Instance public SingletonOperation public GetSingletonData private static uniqueInstance private singletonData return uniqueInstance Singleton continued Participants Collaborations Just the Singleton class Clients access a Singleton instance solely through Singleton s Instance operation Consequences Controlled access to sole instance Reduced name space versus global variables Permits a variable number of instances if desired Implementation import java util Date public class Singleton private static Singleton theOnlyOne private Date d new Date private Singleton public synchronized static Singleton instance if theOnlyOne null theOnlyOne new Singleton return theOnlyOne public Date getDate return d Using our Singleton Class public class useSingleton public static void main String args Singleton a Singleton instance Singleton b Singleton instance System out println a getDate System out println b getDate System out println a System out println b Output Sun Apr 07 13 03 34 MDT 2002 Sun Apr 07 13 03 34 MDT 2002 Singleton 136646 Singleton 136646 Names of Classes in Patterns Are the class names specified in a pattern required No Consider an environment where a system has access to only one printer Would you want to name the class that provides access to the printer Singleton No you would want to name it something like Printer On the other hand Incorporating the name of the classes of the pattern can help to communicate their use to designers Oh I see you have a PrinterObserver class are you using the Observable design pattern Names continued So if names are unimportant what is Structure We can name our Singleton class anything so long as it has a private or protected constructor need a protected constructor to allow subclasses has a static instance operation to retrieve the single instance Strategy Separate an object and its behavior by encapsulating the behavior in a separate class This allows you to change an object s behavior dynamically by switching from one behavior implementation to another Strategy continued MyClass can exhibit different behaviors simply by pointing at different instances of Strategy subclasses A dependency injection pattern could be used to wire these classes together Model View Controller A pattern for manipulating information that may be displayed in more than one view Model data structure s being manipulated may be capable of notifying observers of state changes View a visualization of the data structure having more than one view is fine MVC keeps all views in sync as the model changes Controller handle user input on views make changes to model as appropriate more than one controller means more than one interaction style is available MVC Architecture Outline Additional Design Related Topics Design Patterns Design by Convention Singleton Strategy Model View Controller Inversion of Control also Dependency Injection Refactoring high level overview A graphical example details in a later lecture Design by Convention There has always been a lot of interest in how frameworks for application development are designed Frameworks are typically a set of classes that aid a developer in quickly creating an application for a particular application domain Web frameworks Web applications GUI frameworks GUI applications etc Design by


View Full Document

CU-Boulder CSCI 5828 - More on Design

Documents in this Course
Drupal

Drupal

31 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

22 pages

Load more
Loading Unlocking...
Login

Join to view More on Design and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More on Design and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?