Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Chapter 10: Introduction to Inheritance
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
1 Computer Science 340 Software Design & Testing Inheritance.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Huang-Ming Huang, Shih-Ling Chen, Sajeeva Pallemulle.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Strategy Design Pattern
Design Patterns Lecture part 2.
A Brief Introduction to Design Patterns
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Low Budget Productions, LLC
Software Design and Architecture
Creational Pattern: Prototype
Software Design and Architecture
Object Oriented Analysis and Design
More Design Patterns 1.
One class is an extension of another.
More Design Patterns 1.
Informatics 122 Software Design II
Week 6 Object-Oriented Programming (2): Polymorphism
Decorator Pattern Richard Gesick.
Generation Gap By Kurt Rehwinkel
Software Design Lecture : 12.
Structural Patterns: Adapter and Bridge
CIS 199 Final Review.
Creational Patterns.
Informatics 122 Software Design II
Object-Oriented PHP (1)
Decorator Pattern.
Presentation transcript:

Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson Generation Gap Pattern Hatching - John Vlissides Pages 85 – 101 Todd Anderson

Pattern Basics PATTERN NAME Generation Gap CLASSIFICATION Class Structural INTENT Modify or extend generated code just once no matter how many times it is regenerated. MOTIVATION Having a computer generate code for you is usually preferable to writing it yourself, provided the code it generates is correct, efficient enough, functionally complete, and maintainable.

Motivation (continued) Many code generation tools have no problem generating code that's correct and efficient. The difficulty with these tools is their attempt at creating functionally complete software that is maintainable. Developer has to write some of the code. Developer needs to be able to understand what the generated code in order to modify.

Example – Alarm Clock An alarm clock application built with an interface builder application Tool provides developer with: Widgets (buttons, scroll bars, etc) Primitive graphical objects (lines, circles, etc) Ability to associate behavior with drawn items.

Clock Interface

Clock Operations Once the interface has been generated the developer would add functionality to the code. A 1-second interval event Rotating the clock hands Setting the alarm Turning alarm off Etc… Does anyone see a problem with this?

Maintenance Problem What if you want to rearrange the user interface? What if you want to move buttons? What if you resize the clock? Regenerating the code blindly will clobber any changes or at least force you to reapply them.

Simple Solution Generation Gap is a pattern that solves this problem through class inheritance. It encapsulates generated code in a class and then splits that class into two: One class for encapsulating generated code (Clock_core) One for encapsulating modifications (Clock) Core class is Never modified by subclass. Code intantiates object with subclass (Clock) Modifications go into subclass (Clock)

Simple Solution

Applicability Apply Generation Gap when all of the following are true: Generated code can be encapsulated in one or more classes. Code is generated automatically. Regenerated code usually retains the inteface and instance variables of the previous generation. Generated classes usually aren't integrated into existing class hierarchies.

Structure

Participants CoreClass (Clock_core) an abstract class containing a tool-generated implementation. is never modified by hand. is overwritten by the tool on regeneration. CoreSubclass (Clock) a trivial subclass of CoreClass. implements extensions of or modifications to CoreClass. A programmer may change it to add state and/or extend, modify, or override CoreClass behavior. extensions or modifications are preserved across regenerations. Client instantiates and refers to CoreSubclass only.

Collaborations CoreSubclass inherits tool-generated behavior from CoreClass, overriding or extending its behavior. CoreClass exposes and/or delegates select functionality to CoreSubclass to allow modification or extension of its behavior.

Consequences - Benefits Modifications are decoupled from generated code Modifications can have privileged access to implementation details. CoreClass & ExtensionClass may be developed and tested independently Subsequent regeneration does not require reapplying the modifications. While the modifications will not need to be reapplied, they may need modification themselves if modifications refer to members that no longer exist regenerated code differs semantically from its previous incarnation so that operations no longer mean the same thing Both incompatibilities diminish the pattern's effectiveness, so they must not be inherent to the code being generated.

Consequences - Liabilities Doubles the number of classes. Integrating generated classes into existing class hierarchies may be difficult. Making the extension class inherit from an existing class requires multiple inheritance.

Implementation Disallowing modifications to the core class. Cardinal requirement for this pattern Controlling access to core class internals. The more information CoreClass exposes to subclasses, the more likely regenerating the code will break modifications to ExtensionClass. Naming conventions. CoreClass should derive its name from extension subclass Granularity of CoreClass operations. Keep operations fine-grained enough so that programmers can override precisely the functionality they’re interested in.

Sample Code – Core Class class Clock_core : public MonoScene { public: Clock_core(const char*); protected: Interactor* Interior(); virtual void SetTime(); virtual void SetAlarm(); virtual void Snooze(); Picture* _clock; SF_Polygon* _hour_hand; SF_Rect* _min_hand; Line* _sec_hand; };

Sample Code – Ext. Class Extension class before modification: class Clock : public Clock_core { public: Clock (const char*); };

Sample Code – Ext. Class II Extension class after modification: class Clock : public Clock_core { public: Clock (const char*); void Run(); virtual void SetTime(); virtual void SetAlarm(); virtual void Snooze(); virtual void Update(); private: void GetSystemTime(int& h, int& m, int& s); void SetSystemTime(int h, int m, int s); void Alarm(); float _time; float _alarm; };

Known Uses Generation Gap did not make the cut because of a lack of known uses at the time. Ibuild – Interface builder Reader’s noted other examples Blue Sky’s Visual Programmer CORBA Stub generator

Related Patterns Core classes often use template methods to maximize reuse of generated functionality Factory Methods can give extension classes control over the objects that the core class uses internally.

Questions