More on DesignCSCI 5828: Foundations ofSoftware EngineeringLecture 23Kenneth M. AndersonOutline Additional Design-Related Topics Design Patterns Singleton Strategy Model View Controller Design by Convention 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 ISBN 0-201-63361-2 Authors Erich Gamma Richard Helm Ralph Johnson John Vlissides Known as “The Gang of Four” Presents 23 Design PatternsWhat are Patterns? Christopher Alexander talking about buildingsand towns “Each pattern describes a problem which occursover and over again in our environment, and thendescribes the core of the solution to that problem,in such a way that you can use this solution amillion times over, without ever doing it the sameway twice” Alexander, et al., A Pattern Language. OxfordUniversity Press, 1977Patterns, continued Patterns can have different levels ofabstraction In Design Patterns (the book), Patterns are not classes Patterns are not frameworks Instead, Patterns are descriptions ofcommunicating objects and classes that arecustomized to solve a general design problem in aparticular contextPatterns, continued So, patterns are formalized solutions todesign problems They describe techniques for maximizingflexibility, extensibility, abstraction, etc. These solutions can typically be translated tocode in a straightforward mannerElements 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 higherabstraction The Problem Gives a detailed description of the problemaddressed by the pattern Describes when to apply a pattern Often with a list of preconditionsElements of a Pattern,continued The Solution Describes the elements that make up the design,their relationships, responsibilities, andcollaborations Does not describe a concrete solution Instead a template to be applied in many situationsElements of a Pattern,continued The consequences Describes the results and tradeoffs of applying thepattern Critical for evaluating design alternatives Typically include Impact on flexibility, extensibility, or portability Space and Time tradeoffs Language and Implementation issuesDesign Pattern Template Pattern Name andClassification Creational Structural Behavioral Intent Also Known As Motivation Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related PatternsExamples Singleton Strategy Model View ControllerSingleton Intent Ensure a class has only one instance, andprovide a global point of access to it Motivation Some classes represent objects where multipleinstances do not make sense or can lead to asecurity risk (e.g. Java security managers)Singleton, continued Applicability Use the Singleton pattern when there must be exactly one instance of a class, and itmust be accessible to clients from a well-knownaccess point when the sole instance should be extensible bysubclassing, and clients should be able to use anextended instance without modifying their codeSingleton StructureSingletonstatic Instance(){return uniqueInstance}public SingletonOperation()public GetSingletonData()private static uniqueInstanceprivate singletonDataSingleton, continued Participants Just the Singleton class Collaborations Clients access a Singleton instance solely through Singleton’sInstance operation Consequences Controlled access to sole instance Reduced name space (versus global variables) Permits a variable number of instances (if desired)Implementationimport 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 Classpublic 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 2002Sun Apr 07 13:03:34 MDT 2002Singleton@136646Singleton@136646Names of Classes in Patterns Are the class names specified in a pattern required? No! Consider an environment where a system has access toonly one printer Would you want to name the class that provides access tothe 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 canhelp to communicate their use to designers “Oh, I see you have a “PrinterObserver” class, are you using theObservable design pattern?Names, continued So, if names are unimportant, what is? Structure! We can name our Singleton class anythingso long as it has a private or protected constructor need a protected constructor to allow subclasses has a static “instance” operation to retrieve thesingle instanceStrategy Separate an object and its behavior byencapsulating the behavior in a separateclass This allows you to change an object’s behaviordynamically by switching from one behaviorimplementation to anotherStrategy, continuedMyClass can exhibit different behaviors, simply by pointingat different instances of Strategy subclasses. (A dependencyinjection pattern could be used to wire these classes together!)Model View Controller A pattern for manipulating information that may bedisplayed 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 “interactionstyle” is availableMVC ArchitectureOutline
View Full Document