Outline • Java beans • Applets CS1007: Object Oriented Design and Programming in Java Lecture #16 Nov 22 • Reading: finish chapter 7, starting 8 Shlomo Hershkop shlomo@cs.columbia.edu Announcements Feedback • 4 more lectures after today • Questions on concepts • 1 more homework after today (no actual – Please stop me and I can give more examples code needed). • Generic example • Plan – Will review some more • No class Thursday (or office hours). • Homework help – Will try to help later in class 1
Generics Errors runtime • Generally when you manipulate a group of • When manipulating objects and casting, if objects, in order for the compiler to be you make a mistake on what you think is aware of the manipulations (error in a collection, will throw errors. checking) you need to explicitly cast the general objects you are working with. List myints = new LinkedList(); myints.add(new Integer(3)); Integer x = (Integer)myints.iterator().next(); Idea Another advantage • Allow you to tell the compiler what you are • Allows you to program algorithms which thinking… don’t work in the dark • Allows you to setup constraints on the objects you allow to be passed to your List<Integer> myints = new methods LinkedList<Integer>(); //adding same Integer x = myints.iterator().next(); 2
wildcarding Confused? • Allows you specify general types and • Please review book bounded general types in your algorithms • Please review last lecture • Email myself/TAs • Can ask other students Map <String, ? extends employee> • Use internet (not responsible for anything you happen to dig up there). Introducing Java Beans • Java component model • Bean has – methods (just like classes) – properties – events 3
Property sheet What kind of pattern can we Façade class extract? • Bean usually composed of multiple • A subsystem consists of multiple classes, classes making it complicated for clients to use • One class nominated as facade class • Implementor may want to change subsystem classes • Clients use only facade class methods • Want to give a coherent entry point 4
How JAVABEAN does it • Define a facade class that exposes all capabilities of the subsystem as methods • The facade methods delegate requests to the subsystem classes • The subsystem classes do not know about the facade class Bean Properties Syntax • Property = value that you can get and/or • Not Java :-( set • C#, JavaScript, Visual Basic • Most properties are get-and-set • b.propertyName = value • Can also have get-only and set-only calls setter • Property not the same as instance field • variable = b.propertyName calls getter • Setter can set fields, then call repaint • Getter can query database 5
Conventions Builder tool • property = pair of methods public X getPropertyName() public void setPropertyName(X newValue) • Replace propertyName with actual name (e.g. getColor/setColor) • Exception for boolean properties: public boolean isPropertyName() • Decapitalization hokus-pokus: getColor -> color getURL -> URL Packaging • Compile bean classes Ch7/carbean/CarBean.java • Create manifest file Ch7/carbean/CarBean.mf • Run JAR tool: • jar cvfm CarBean.jar CarBean.mf *.class • Import JAR file into builder environment 6
Composing Bean • Make new frame • Add car bean, slider to frame • Edit stateChanged event of slider • Add handler code carBean1.setX(jSlider1.getValue()); • Compile and run • Move slider: the car moves Framework Application framework • Set of cooperating classes • Implements services common to a type of applications • Structures the essential mechanisms of a problem domain • Programmer forms subclasses of framework classes • Example: Swing is a GUI framework • Result is an application • Framework != design pattern • Inversion of control: framework controls • Typical framework uses multiple design execution flow patterns 7
Openscience.org/jmol Applet • Applet: Java program that runs in a web browser • Programmer forms subclass of Applet or JApplet • Overwrites – init/destroy – start/stop – paint Applets Example • Interacts with ambient browser • Shows scrolling banner getParameter • init reads parameters showDocument • start/stop start and stop timer • HTML page contains applet tag and parameters <applet code="BannerApplet.class" • paint paints the applet surface width="300" height="100"> <param name="message" value="Hello, Ch8/applet/BannerApplet.java World!"/> <param name="fontname" value="Serif"/> <param name="fontsize" value="64"/> <param name="delay" value="10"/> </applet> 8
Next • Finish homework – Please email me if you get stuck/clarrifications • Do reading – Chapter 8 – 8.5 9
Recommend
More recommend