240 likes | 378 Views
Frameworks. Framework. Set of cooperating classes/interfaces Structure essential mechanisms of a problem domain Programmer can extend framework classes, creating new functionality Example: Swing package is framework for problem domain of GUI programming. Frameworks Vs. Patterns.
E N D
Framework • Set of cooperating classes/interfaces • Structure essential mechanisms of a problem domain • Programmer can extend framework classes, creating new functionality • Example: Swing package is framework for problem domain of GUI programming
Frameworks Vs. Patterns • Pattern: general design rule for solving a problem • Framework: set of classes providing functionality in a particular domain; often uses multiple patterns
Application Frameworks • Implement services common to an type of application • Supply sets of classes for programmer to augment to build applications (often by subclassing framework classes)
Inversion of Control • Framework classes, not application-specific classes, control flow of execution • Framework determines which methods to call and when
Framework example: Applets • Applet: Java program that can be included in a web document to add interactions and dynamism • Java source is referenced by an HTML document • Package java.applet is a simple application framework • consists of superclasses to make applets • programmer adds classes, overrides methods to create actual applets
Applet characteristics • No main() method • Programmer overrides some or all of the following methods: • init() • start() • stop() • destroy() • paint()
How applets work • Since an applet has no main() method, initialization of object takes place in method init(), which takes the place of both main() and the constructor • Method init() is typically used to perform initializations of short duration; longer running initializations, e.g. loading a file across a network, should be done in separate threads
Methods of Applet class • Method init() is empty, and must be overridden by the child class • Method start() is called to set up the applet to run • may create several threads • called when applet is loaded & initialized the first time, resized, or when user returns to page from elsewhere • paint() automatically called after start()
Methods of Applet class • Method stop() is called to stop the applet - usually means halting threads started by start() when, for example, the user leaves the page • Method destroy() performs final cleanup before the applet is unloaded
Methods of Applet class • Control sequence is: • init() • start() • paint() • then stop() or destroy(), as appropriate • Applet can transition many times between start() and stop() states • May define all, some, or none of these methods when extending an Applet; should only define those necessary for specific applet
HTML for applet inclusion <APPLET codebase = “URL for code location” code = class name width = # height = #> <param name = somename value = value> Alternate text to display in lieu of applet </APPLET>
Applet HTML dissected • Codebase: URL web address where code can be found (can be just a file name) • code: name of class (name of file in an application) • height and width tell browser how much space to allocate for applet • param tags: provide parameters, analogous to command-line parameters - values can be accessed within applet via call to getParameter()
More fun facts about applets • Applet class is actually subclass of Panel - so inherits graphical component attributes • Events, in particular, are handled same way as other graphical components, and window repainting same as application • Default layout for applet is flow layout, not border layout
Other methods provided by Applet class • getImage(URL): takes URL and retrieves image from that location (must be gif or jpg) • getAudioClip(URL): returns audio object from given location; can subsequently be asked to play - shorthand version play(URL) combines get and play methods
Other methods provided by Applet class • getCodeBase() returns URL for codebase specified for applet • getParameter() takes String argument and returns associated String value if <param> tag was provided - returns null if none was provided
Java class URL • URL class is provided by java.net.* • URL object supplies methods to parse URL, open network connections, and retrieve information • Example: URL newURL = new URL(String s); creates URL object newURL with specification s
Java class URL • Can create URLs using Java’s URL class - address is formed from a String, or previous URL plus a String - latter from could be used, for example, to retrieve several files from the same directory • URL class provides method openStream, which returns an InputStream value - once an URL object is created, can use this method to read from the URL using normal InputStream methods
Using applet to open a new page • Can use showDocument() method of class appletContext to instruct browser to load a new web page: appletContext.showDocument(URL) • URL refers to a Java URL object (not a string) • browser will display the page corresponding to the URL
Applets vs. Applications • An applet has no main method, as it is structured to run inside another application • The Java Virtual Machine runs applications, but not applets • Can use appletviewer program to test and debug applet code • Most applets are event-driven (but don’t have to be)
Applets vs. Applications • Applets do I/O through the browser GUI, and displays error messages (only) to stdout • An applet is a subclass of Applet from java.applet • Applets have security restrictions: can’t read or write files, run local executable programs, communicate with hosts other than the server, or learn many facts about the client computer
Creating Applet/Application Hybrids • Applet class pays no attention to static methods contained in class definition • Key idea is that Applet is a Panel • can nest inner class within Applet to create Frame necessary for application • only component created for this Frame will be Panel constructed by Applet • Method main(), which is ignored when executing as applet, creates instance of applet when run as an application • Applet then creates instance of Frame, placing itself in center • Constructor for Frame executes requisite init() and start()
Applets as Framework • Programmer for individual applet uses inheritance to extend the applet framework class to specific program • Applet class deals with behavior common to all applets; programmer only fills in custom behavior for specific application
Applets as Framework • Inversion of control means applet programmer not concerned with overall control flow - fills in handlers for initialization, starting, stopping and painting, but has no control over when these methods are called