1 / 82

Review

Review. Why a NullPointerException?. class FloorView implements Observer { private Elevator elevator; // the subject private JLabel label; public FloorView(Elevator elev) { Elevator elevator = elev; JLabel label = new JLabel( "Floor " +elev.getFloor() ); ... }

Télécharger la présentation

Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review

  2. Why a NullPointerException? class FloorView implements Observer { private Elevator elevator; // the subject private JLabel label; public FloorView(Elevator elev) { Elevator elevator = elev; JLabel label = new JLabel( "Floor "+elev.getFloor() ); ... } public void update(Observable obj, Object arg) { int floor = elevator.getFloor( ); label.setText( "Floor "+floor ); } NullPointerException NullPointerException

  3. What is a URL? URL = Uniform Resource Locator http://www.google.com ftp://se.cpe.ku.ac.th/download/readme.html file:///C:/temp/somefile.txt Even more complex URLs are possible: jdbc:mysql://se.cpe.ku.ac.th:3306/world

  4. How do you read a URL? Java has a URL class. You can create an InputStream to read or write to/from a URL. URL url = new URL( "http://www.google.com/" ); InputStream instream = url.openStream( ); // process input data as you like Scanner scanner = new Scanner( instream ); while ( scanner.hasNextLine() ) { ... }

  5. How do you create a Color? What do these numbers mean? Color purple = new Color(212, 0, 228); What do these numbers mean? red = Color.getHSBColor(0.0, 1.0, 0.5);

  6. Color Space You can specify any color using 3 numbers. RGB - color given by level of red, green, and blue. Usually 8-bits (0-255) per color. Some digital cameras use 12-bits per color. CMY - cyan, magenta, yellow HSB - Hue, Saturation, Brightness (Java: float 0.0-1.0) HSL - Hue, Saturation, Luminescence

  7. Calculate the Picture Size A digital camera has resolution 4 Megapixels and 8-bit color depth. 1. How much space to store a photo in TIFF format (no compression)? 2. How much space to store a photo in JPEG format with 10-to-1 compression?

  8. What is a JAR file? 1. What is the format of a JAR file? 2. How do you create a JAR file? Java reads information from a JAR file, such as the name of the "main" class, whether this JAR is a JavaBean. 3. What file contains this info?

  9. Getting a Reference • The ButtonPanel for the elevator needs to send messages to the elevator. • How can we give ButtonPanel a reference to the Elevator?

  10. Solution: inject a Reference This is dependency injection. class ButtonPanel extends JFrame { private Elevator elevator; public ButtonPanel(Elevator lift) { this.elevator = lift; // dependency ... }

  11. Dependency Injection by setter method Use a "set" method for dependency injection. class ButtonPanel extends JFrame { private Elevator elevator; public ButtonPanel( ) { ... } public void setElevator(Elevator lift) { this.elevator = lift; // dependency ... }

  12. Wrong Solution Create the Elevator in the View. class ButtonPanel extends JFrame { private Elevator elevator; public ButtonPanel( ) { elevator = new Elevator( );// wrong ... }

  13. Draw a Sequence Diagram Draw a Sequence Diagram of Dependency Injection. class Main { public void init( ) { Elevator elevator = new Elevator( ); ButtonPanel bp = new ButtonPanel( ); bp.setElevator( elevator ); elevator.addObserver( bp ); }

  14. Start Chart Diagram Floor 2 UP DOWN Floor 1 entry/openDoor exit/closeDoor

  15. 3 Fundamental O-O Characteristics 1. Encapsulation 2. Polymorphism 3. Inheritance

  16. What is Polymorphism? "Don't Ask What Type" • Define polymorphism. • Give the simplest example of polymorphism you can think of (no elevators or observers). Object o = new Coin( 5 ); System.out.println( o.toString() ); o = new Date( ); System.out.println( o.toString() );

  17. 2 ways to enable Polymorphism? • Name 2 constructs (ways of programming in Java) we can use to achieve polymorphism? 1. inheritance 2. interface • Give example of each one. 1. Coin is a subclass of Money • How can we enable polymorphism in our apps? List coins = new MyList();

  18. Notifying Observers The FloorView must update the display whenever the elevator changes state. How is this done? 1. _______________ invokes _______________ 2. _______________ invokes _______________ 3. ____________ (Observer) calls _______________ 4. _______________ calls _______________

  19. Notifying Observers The FloorView must update the display whenever the elevator changes state. How is this done? 1. Elevator invokes notifyObservers_ 2. notifyObservers() invokes observer.update( ) 3. FloorView (Observer) calls elevator.getFloor() 4. FloorView calls label.setText( "Floor "+floor )

  20. Notifying Observers The FloorView must update when the Elevator moves. How is this done? Draw and number the method calls.

  21. O-O Fundamentals How are the fundamental O-O characteristicsused in the Observer pattern? How does this benefit the program?

  22. Views and Controls Views are Observers. They get notified by Subject. Q: Do views need a reference to Subject? 2. wake up! View (Observer) AbstractSubject 3. what happened? 1. notify observers 4. update display Subject Floor 5

  23. Views and Controls Controls send messages to the Subject. Q: Do controls need a reference to the Subject? UP BUTTON AbstractSubject 1. event occurs 4. notify observers 2. handle this Subject Control 3. do something

  24. Elevator Views and Controls • Some components are both observer and control. Notification Path Control Path

  25. Remote Control problem We have a 4-button Remote Control. Design a solution to program the Remote Control. Goals:1. Don't couple Remote Control to a LightBulb or any other device. 2. Can program RemoteControl to control anything. 1 2 3 4

  26. Remote Control solution We have a 4-button Remote Control. Design a solution to program the Remote Control. 1 2 click 3 4

  27. Use Commands for RemoteControl

  28. How to Program using an Action class OnAction extends AbstractAction { private LightBulb light; public OnAction(LightBulb light) { super("Light On"); this.light = light; } public void actionPerformed( ) { light.turnOn( ); } }

  29. How to Program using an Action (2) Now you can put the action in as many components as you want. Action on = new OnAction( ); JButton onButton = new JButton( on ); MenuItem mitem = new MenuItem( on ); menu.addItem( mitem ); Components will get their name, status, icon from the action. They use OnAction as ActionListener.

  30. How to Program using an Action (3) You can disable the components by disabling the action. on.setEnabled( false ); Disable all components using this action.

  31. 3 O-O Characteristics • How does the Command Pattern (using Actions) use the 3 O-O characteristics? • What is the benefit of each one?

  32. How does Remote Control use the Pattern? Complete the table.

  33. Abstract Superclass • What is an Abstract Class? a class with one or more abstract methods (method without an implementation) class must be declared "abstract" you cannot create an instance of an abstract class • Give example of an Abstract Class is Java. AbstractAction, AbstractList, Observable • Why use an Abstract Class? see following slides

  34. Abstract Class • How would you use an Abstract Class?

  35. Abstract Class for Money • Factor out common behavior and attributes needed by subclasses

  36. Abstract Class for Money • Provide default implementations for methods if you don't like my equals then do it yourself! @Override equals(Object) { ... }

  37. MouseAdapter • Abstract class provides default implementations of methods (convenience). Most of them do nothing. • Makes it easier to implement the interface.

  38. Abstract Classes Provide Behavior Observable, AbstractList, AbstractAction Abstract superclass providing methods and attributes that implement useful behavior. Subclass can reuse this functionality or override it. AbstractAction provides useful behavior to its subclasses.

  39. Benefits of Abstract Superclass 1. 2. 3.

  40. Disadvantage of Abstract Superclass Are there any disadvantages or limitations on using an Abstract Superclass? 1. 2.

  41. Abstract Superclass or Interface? Compare Abstract Class with Interface.

  42. Patterns

  43. What Pattern Should We Use? • General Problem: some resource needs to be shared by all objects. How can we guarantee that only one instance exists? • Example: share a connection to a database. // DatabaseManager should ensure we always // use the same connection Connection connection = DatabaseManager.getConnection( );

  44. Draw a Class Diagram of Solution

  45. Changing Some Behavior • General Problem: we have a reusable class but some methods need to change their behavior (algorithm) in different situations. • Example: change the way a Container arranges Components. panel.setLayout( new GridLayout(4,3) ); panel.add( button1 ); panel.add( button2 ); ...

  46. Draw a Class Diagram Draw a class diagram of the parts of the Strategy Pattern

  47. Complete the Table How do Container and LayoutManager use this pattern? Complete this table.

  48. Give Another Example Give another example of the Strategy Pattern.

  49. What Pattern Should We Use? • General Problem: we want to modify or enhance some behavior of a class, but we don't want to modify the class. • Example: how to add scroll bars to components JTextArea textarea = new JTextArea( 8, 40); JScrollPane pane = new JScrollPane( textarea ); pane.setVerticalScrollbarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ): // add pane (not textarea) to container contentpane.add( pane );

  50. Class Diagram of Decorator • Draw a class diagram of the decorator pattern.

More Related