1 / 24

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Phones off Signs out. Announcements. Exam 2 – 1 week away covers material from exam 1 up to & including 10/15 review on Monday 10/18 exam on Wednesday 10/20. Agenda.

aquila
Download Presentation

CSE115: Introduction to Computer Science I

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. CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu

  2. Phones off Signs out

  3. Announcements • Exam 2 – 1 week away • covers material from exam 1 up to & including 10/15 • review on Monday 10/18 • exam on Wednesday 10/20

  4. Agenda • interfaces and realization • type hierarchy

  5. Interfaces and Realization • form of interface definition • function of an interface • realization (“implements”) relationship

  6. Form of an interface • header + body • header • access control modifier • keyword ‘interface’ • name (generally an adjective, following class name conventions, but prefixed with an upper-case ‘I’) • body • method specifications (method headers followed by ‘;’, also called method declarations, as opposed to method definition)

  7. Example public interface ICollarable { public void addCollar(Collar collar); public Collar removeCollar(); }

  8. Example public class Dog implements ICollarable { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } }

  9. Example public class Dog implements ICollarable { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void addCollar(Collar collar) { _collar = collar; } public Collar removeCollar() { Collar temp = _collar; _collar = null; return temp; } }

  10. Example ICollarable x; x = new ICollarable(); NO! Cannot instantiate. x = new Dog(); OK if Dog implements ICollarable x = new Cat(); OK if Cat implements ICollarable

  11. Calling methods ICollarable x; x = new ICollarable(); NO! Cannot instantiate. x = new Dog(); OK if Dog implements ICollarable x = new Cat(); OK if Cat implements ICollarable x.addCollar(new Collar()); OK x.removeCollar(); OK x.walk(); NO! Not all ICollarable objects define this method!

  12. Demo in Eclipse

  13. State systems • State-based system: • system’s behavior based on its current state • Many systems are state-based: • Cable/Satellite TV box • Wrist-watch • Cell phone interface

  14. State Diagram push button OFF ON push button

  15. State Pattern example

  16. Polymorphism • Behavior determined by method of object, because… • it’s the object that responds: • the type of the object determines response • the type of the reference to object doesn’t

  17. Polymorphism declared type of variable a supertype of actual type of object

  18. Proxy Pattern Polymorphism

  19. Polymorphism(in proxy pattern) context delegates method call to concrete tool

  20. State Pattern delegation

  21. Delegation in State code public class DeskLamp { private IState _state; public DeskLamp() { _state = new Off(); } public void pushButton() { _state.pushButton(this); } public void setState(IState s) { _state = s; } }

  22. Proxy Pattern delegation

  23. Delegation in Proxy code public class Proxy implements ISomething { private ISomthing _target; public Proxy(ISomething t) { _target = t; } public void methodA(X x, Y y, Z z) { _target.methodA(x, y, z); } public void methodB(R r, S s) { _target.methodB(r, s); } // and so on }

More Related