More Frameworks Acknowledgements: K. Stirewalt. Johnson, B. Foote Johnson, Fayad, Schmidt Overview eview of object-oriented programming (OOP) principles. Intro to OO frameworks: o Key characteristics. o Uses. o Black-box vs. white-box frameworks. Example study: Animations in the subarctic UI toolkit. ecap on OOP Key features that differentiate OOP from traditional block-structured languages: Polymorphism: a late-binding feature, whereby a procedure is dynamically bound to a message request. Inheritance: allows operations to be defined in one (parent) class and then overridden in another (child) class. Inheritance and polymorphism together are the principle tools of OO frameworks. 1
Inheritance and polymorphism Subclasses provide different methods for same operation. Benefit: Enables decoupling of cooperating classes. Clients need not know how an operation is implemented. Thus, designs are portable and extensible. Code can be reused. Two cases: 1. Superclass provides a method for the operation. we say the subclass overrides this method. subclass method may explicitly invoke superclass method. 2. Superclass provides no method for the operation. we say the operation is abstract in the superclass. Protocol Operations performed on objects by ``sending them messages''. Specification of an object given by its message protocol: Set of messages that can be sent to it; Includes type of arguments of each message. Objects with identical protocols are interchangeable. Interface between objects defined by protocols they expect each other to understand. In most languages, protocol is bound to an object's class 1. Abstract classes Defn: class that cannot have any instances. Only subclasses can have instances. How to specify that a class is abstract: C++: Implicit; abstract class has pure virtual functions. Java: Class can be explicitly declared abstract, or Abstract operations can be grouped into an interface. Used to define standard protocols. 2
Use of Abstract Class to epresent a Protocol Abstract Operations and Design Coupling Imagine: a tool that places figures on a grid. Example: gridding of program icons in a window manager. Example: placement of button figures in a panel. Grid-management code allocates figures to grid positions, avoiding collisions and possibly implementing a fill policy. Code should not be concerned with how to draw a figure. Question: How do you separate grid management from the drawing of figures? Example Decoupling using abstract operations 3
Example: Use of these classes #include "Square.h" #include "Triangle.h" #include "Circle.h" #include "Grid.h"... Grid mygrid;... mygrid.addfigure( new Square ); mygrid.addfigure( new Triangle ); mygrid.addfigure( new Circle ); mygrid.draw(); Example: continued class Figure : public WindowComponent { public: virtual void Draw() = 0; // pure virtual function! }; class Grid : public WindowComponent { public: void AddFigure( Figure* ); void Draw() { for( int i=0; i < 3; ++i ) { figures[i].draw(); } private: Figure* figures[3]; }; Plug Compatibility If several classes define same protocol, then objects in those classes are plug compatible. Thus complex objects can be created by plugging together a set of compatible components. Style of programming called building tool kits. 4
Intro to OO Frameworks Framework Support reuse of detailed designs An integrated set of components: Collaborate to provide reusable architecture for Family of related applications Frameworks 1. Frameworks are semi-complete applications Complete applications are developed by inheriting from, and Instantiating parameterized framework components 2. Frameworks provide domain-specific functionality Ex.: business, telecommunication, dbases, distributed, OS kernels 3. Frameworks exhibit inversion of control at run-time Framework determines which objects and methods to invoke in response to events 5
Class Libraries vs Frameworks vs Patterns Application Specific Networking Logic Invokes Math ADTs Event Loop Math ADTs UI Application Specific Logic Dbase Class Library Architecture Invokes Networking Call Backs Dbase UI Framework Architecture Event Loop Definition: Class Libraries: Self-contained, Pluggable ADTs Frameworks: eusable, semicomplete applications Patterns: Problem, solution, context Characteristics of Frameworks Often: o User-defined methods invoked by the framework code itself. o Framework plays the role of ``main program''. This inversion of control allows frameworks to serve as extensible code skeletons. User-supplied methods tailor generic framework algorithms for a specific application. Object-Oriented Frameworks A.k.a: Object-oriented abstract design. Consists of: o Abstract class for each major component; o Interfaces between components defined in terms of sets of messages. o Usually a library of subclasses that can be used as components in the design. Many well-known examples: o All modern UI toolkits (e.g., Java AWT, subarctic, MFC). o Distributed computing toolkits (e.g., ACE). 6
Example: Java AWT Framework (elided) Whitebox Frameworks Program skeleton: Subclasses are the additions to skeleton Implications: Framework implementation must be understood to use Every application requires the creation of many new subclasses Can be difficult to learn: Need to need how it was constructed (hierarchical structure) State of each instance is implicitly available to all methods in framework (i.e., similar to global vars) Example: Event handling in Java AWT 1.02 and before Events are propagated from components to their containers until such time as the event is serviced. Every component has the operation: public boolean handleevent( Event event ); To handle an event, simply override this operation with a method. Note the return type (boolean) that is used by the framework to decide whether or not the event was handled by the component. If not handled, then the event is propagated to the component's container. 7
Blackbox Frameworks Customize framework by supplying with set of components that provide application-specific behavior Implications: Each component required to understand a particular protocol All or most components from component library Interface between components defined by protocol Need to only understand external interface of components Less flexible Info passed to application components must be explicitly passed. Example: Event handling in Java AWT 1.0 and 1.1 In the 1.0 version of Java AWT, event handling was implemented using inheritance. In 1.1, this model was replaced with a delegation-based model; i.e., one that is built around protocols rather than implementation sharing. Event handling (AWT 1.1) Events are first-class objects. o There is a class Event. o Subclasses identify different kinds of events. Components fire events. Other objects can listen for/act upon these events. Interested objects register themselves as a Listener with the component that fires the event. 8
AWTEvent hierarchy Event routing Lots of different kinds of events. o Different events carry different types of information. o E.g., ActionEvent carries a command, TextEvent carries a string being edited. Components fire these events. E.g., Button fires an ActionEvent. E.g., TextArea fires a TextEvent. All components fire WindowEvents. When fired, events are routed to special Listener objects Listeners A listener is an object to which AWTEvents can be routed by components. Different types of listeners. o One for each subclass of AWTEvent. o E.g., ActionListener, ComponentListener, WindowListener. o A listener class is abstract: o Operation names only; no attributes or methods. o Declared as an interface in Java. 9
Example: An action-event listener class ButtonListener implements ActionListener { public void actionperformed(actionevent event) { System.out.println("Button pressed!!!"); } } public class ActionExample extends Applet { public void init() { Button button = new Button("Push me"); button.addactionlistener( new ButtonListener()); } add(button); } Example: A mouse-event listener class ButtonMouseListener implements MouseListener { public void mouseentered(mouseevent event) { System.out.println("Mouse entered button!"); }; } public void mouseexited(mouseevent event) { System.out.println("Mouse exited button!"); }; public void mousepressed(mouseevent event) public void mouseclicked(mouseevent event) {} public void mouseeleased(mouseevent event) {} Design schema for event routing oute events of class E from graphical component C to object O: 1. Design O to be an E-listener. Class of O must implement the E-listener interface. The corresponding methods must service the event. 2. Add O to C's set of listeners. Pseudo-code: C.addEListener(O); 10
Example OO Framework: Animations in the subarctic UI Toolkit SubArctic and animation SubArctic is a Java-based UI toolkit developed at Georgia Tech and Carnegie Mellon. Powerful support for animations: o High-level model for describing time-based events in a UI. o Allows traditional image (i.e., page-flipping) animations. o Also allows objects to smoothly move about screen, modify color over time, etc. Implemented as an OO framework. Abstract UML of the framework 11
Data Dictionary Interactor: interface defines the API that all objects appearing on the screen and/or accepting input must provide. Defines all basic operations of interactive objects Base_parent_interactor: Base class for objects that support (arbitrary) children. Provides default implementation for all methods defined by interactor interface and supports routines Animatable: Input protocol interface for specifying that an object is animatable. Each interactor that expects to receive animation input must implement animatable input protocol. Anim_mover_container: A container class to move a collection of objects under control of an animation transition. Callback_object: Interface for objects that receive callback from interactors.callbacks are entities which are notified when significant user actions (such as a button press or a menu selection) occur. Implemented as objects in subarctic Transition: primary abstraction for describing how animations are to proceed in subarctic. Combo of animations logical path, optional pacing function assoc. with path (does object move uniformly among values of path), animations time interval (how long does it take and when does it start) Time_interval: first part of trajectory (absolute or relative form) Trajectory: specifies mapping from time to a set of values (e.g., screen space) Pacer: allow non-linear behavior for mapping SubArctic animations Animation = complex collaboration among multiple objects interactor is the object being animated Some classes/interfaces support customized animations For example: trajectory used for positional path variation (straight line, running-start, smooth curve, etc.) pacer used for non-linear time variation Others provide communication hooks. Callback_object receives notifications at the beginning of the animation Application Construction Environments (Toolkits) Defn: Collection of high-level tools that allow a user to interact with an OO framework to configure and construct new applications [Johnson:joop88]. Key observation: Easier to build a tool to specialize classes with well-defined interfaces than to build a general-purpose code generator 12
Influence of OO Frameworks 13