UVA CS 4501 - Object-Oriented Analysis & Design Design Patterns

Unformatted text preview:

CS 494 Object-Oriented Analysis & DesignReadingsIdioms, Patterns, FrameworksExamples of C++ IdiomsDesign Patterns: Essential ElementsPatterns Are (and Aren’t)Describing Design PatternsPattern Format (cont’d)Example 1: Singleton PatternSingleton: Java implementationStatic Factory MethodsThe State Design PatternF-19/28/01© 2001 T. HortonCS 494Object-Oriented Analysis & Design Design Patterns9/28/01 F-2Readings•Chapter 1 of GoF book–Especially pp. 1-10, 24-26–I’ll get this to you (toolkit, reserve, Web?)•Eckel’s Thinking in Patterns, on Web–Chap. 1, “The pattern concept”–Chap. 5, “Factories”•Handouts on various patterns9/28/01 F-3Idioms, Patterns, Frameworks•Idiom: a small language-specific pattern or technique–A more primitive building block•Design pattern: a description of a problem that reoccurs and an outline of an approach to solving that problem–Generally domain, language independent–Also, analysis patterns•Framework:–A partially completed design that can be extended to solve a problem in a domain•Horizontal vs. vertical9/28/01 F-4Examples of C++ Idioms•Use of an Init() function in constructors–If there are many constructors, make each one call a private function Init()•Init() guarantees all possible attributes are initialized•Initialization code in one place despite multiple constructors•Don’t do real work in a constructor–Define an Open() member function•Constructors just do initialization•Open() called immediately after construction–Constructors can’t return errors•They can throw exceptions9/28/01 F-5Design Patterns: Essential Elements•Pattern name–A vocabulary of patterns is beneficial•Problem–When to apply the pattern, what context.–How to represent, organize components–Conditions to be met before using•Solution–Design elements: relationships, responsibilities, collaborations–A template for a solution that you implement•Consequences–Results and trade-offs that result from using the pattern–Needed to evaluate design alternatives9/28/01 F-6Patterns Are (and Aren’t)•Name and description of a proven solution to a problem•Documentation of a design decision•They’re not:–Reusable code, class libraries, etc. (At a higher level)–Do not require complex implementations–Always the best solution to a given situation–Simply “a good thing to do”9/28/01 F-7Describing Design Patterns•The GoF defined a standard format–Generally followed–Not just a UML diagram!•Pattern Format (13 sections):–Pattern name and classification–Intent: what’s it do? Rationale?–Also known as–Motivation•A scenario that illustrates a sample problem and how this patterns helps solve it.–Applicability•For which situations can this be applied?–Structure•Graphical representation (e.g. UML)9/28/01 F-8Pattern Format (cont’d)–Participants•Classes and objects, their responsibilities–Collaborations•How participants interact–Consequences–Implementation•Pitfalls, hints, techniques, language issues–Sample code•Code fragments that illustrate the pattern–Known uses•From real systems–Related patterns•Similar patterns, collaborating patterns9/28/01 F-9Example 1: Singleton Pattern•Context: Only one instance of a class is created. Everything in the system that needs this class interacts with that one object.•Controlling access: Make this instance accessible to all clients•Solution:–The class has a static variable called theInstance (etc)–The constructor is made private (or protected)–Clients call a public operation getInstance() that returns the one instance •This may construct the instance the very first time or be given an initializer9/28/01 F-10Singleton: Java implementationpublic class MySingleton { private static MySingleton theInstance = new MySingleton(); private MySingleton() { // constructor … } public static MySingleton getInstance() { return theInstance; }}9/28/01 F-11Static Factory Methods•Singleton patterns uses a static factory method–Factory: something that creates an instance•Advantages over a public constructor–They have names. Example:BigInteger(int, int, random) vs.BigInteger.probablePrime()–Might need more than one constructor with same/similar signatures–Can return objects of a subtype (if needed)•Wrapper class example: Double d1 = Double .valueOf(“3.14”); Double d2 = new Double (“3.14”);•More info: Bloch’s Effective Java9/28/01 F-12The State Design Pattern•A connection can be in various states–Handles requests differently depending on state•Connection delegates requests to its state object–Which changes


View Full Document

UVA CS 4501 - Object-Oriented Analysis & Design Design Patterns

Download Object-Oriented Analysis & Design Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-Oriented Analysis & Design Design Patterns 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 Object-Oriented Analysis & Design Design Patterns 2 2 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?