1 / 46

Object-Oriented Programming: Advanced Concepts

This lecture covers advanced concepts in object-oriented programming, including object behavior, abstraction, encapsulation, inheritance, polymorphism, and dependency injection. It also explores the use of different object types and the ability to treat them as others while maintaining their own behavior.

rude
Télécharger la présentation

Object-Oriented Programming: Advanced Concepts

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. WARNING • These slides are not optimized for printing or exam preparation. These are for lecture delivery only. • These slides are made for PowerPoint 2010. They may not show up well on other PowerPoint versions. You can download PowerPoint 2010 viewer from here. • These slides contain a lot of animations. For optimal results, watch in slideshow mode.

  2. How to structure? Object rules Collaborating objects Object structure Follow a paradigm 1 2 3 4 OOP Object behavior 1. Abstraction 2. Encapsulation 3. Inheritance 4. ……………..

  3. OOP: Advanced Concepts CS2103/T, Lecture 5, Part 2, [Sep 9, 2016]

  4. I don’t teach SE differently based on… 2nd, 3rd, 4th year 2nd, 3rd, 4th year 2nd, 3rd, 4th year Male vs Female ISE vs CS vs CEG 2nd, 3rd, 4th year

  5. Example 1 I want to register Students Here you go! UGStudent Admin NGStudent PGStudent

  6. Example 1 Gaaah….! UGStudent Admin NGStudent PGStudent

  7. Example 1 foreachStudent s: s.register(); … Can? Cannot! UGStudent Admin NGStudent PGStudent

  8. Example 2 Storage s; … s.load(); … Logic Storage

  9. Example 2 TestDriver Logic Storage StorageStub

  10. Example 2 TestDriver Logic Storage StorageStub Dependency Injection

  11. Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection

  12. Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection

  13. Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection

  14. Example 2 TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; }

  15. Treat one type as another and still get the behavior of the actual object Example 2 Example 1 TestDriver Logic Storage foreachStudent s: s.register(); … StorageStub setStorage(Storage) UGStudent Storage s; … void setStorage(Storage s){ this.s = s; } Admin NGStudent PGStudent

  16. Treat one type as another and still get the behavior of the actual object Example 2 Example 1 TestDriver Logic Storage foreachStudent s: s.register(); … StorageStub setStorage(Storage) UGStudent Storage s; … void setStorage(Storage s){ this.s = s; } Admin NGStudent Polymorphism PGStudent

  17. Polymorphism

  18. Different game, different behavior Same hardware/ software Polymorphism

  19. = ability to take many forms Polymorphism

  20. ? Polymorphism

  21. Ability to treat different types as same type and yet get different behavior Ability to be accepted as a different type but maintain own behavior Polymorphism

  22. foreachStudent s: s.register(); … UGStudent * Admin Student NGStudent register() PGStudent

  23. foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent register() Inheritence PGStudent

  24. foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent register() PGStudent

  25. foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent register() PGStudent

  26. If expecting super class, can accept sub class foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent Substitutability PGStudent

  27. Storage s; … void setStorage(Storage s){ this.s = s; } Example 2 Storage TestDriver Logic setStorage(Storage) load() StorageStub Logic logic; … logic .setStorage(new StorageStub() );

  28. Storage s; … void setStorage(Storage s){ this.s = s; } FileStorage Storage TestDriver Logic DBStorage setStorage(Storage) load() StorageStub Logic logic; … logic .setStorage(new StorageStub() ); All ‘load’ the same way?

  29. FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() Overriding StorageStub load()

  30. Storage s; … s.load(); At compile time… FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub load()

  31. At run-time… Storage s; … void setStorage(Storage s){ this.s = s; } :Logic s:StorageStub Storage s; … s.load(); FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub Logic logic; … logic .setStorage(new StorageStub()); load()

  32. At run-time… :Logic s:StorageStub Storage s; … s.load(); FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub Logic logic; … logic .setStorage(new StorageStub()); load()

  33. At run-time… :Logic s:??????? Storage s; … s.load(); FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() Dynamic binding StorageStub load()

  34. At run-time… :Logic s:??????? Storage s; … s.load(); Cannot! FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() void load (){ //do nothing } StorageStub Can remove? load()

  35. FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() All Storagechild classes should support a loadmethod, but implementation is up to the child classes StorageStub load()

  36. FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub abstract void load () ; load()

  37. Incomplete! Cannot create objects! FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() {abstract} load() StorageStub load()

  38. FileStorage {abstract} Storage load() TestDriver Logic DBStorage setStorage(Storage) load() {abstract} load() Abstract method/class StorageStub load()

  39. FileStorage {abstract} Storage load() TestDriver Logic {abstract} DBStorage setStorage(Storage) load() {abstract} init() store() Abstract method/class StorageStub A sub class must implement all inherited abstract methods, or it will become an abstract class itself load()

  40. Account BankSystem Throws exception if not 0 < i< 100 getAccount(inti) Which one of these subclasses are not substitutable? i is a parameter from BankSystem to Account AccountTypeB AccountTypeC AccountTypeA getAccount(inti) getAccount(inti) getAccount(inti) … if not0 < i < 1000 … if not0 < i < 100 … if not0 < i < 10

  41. Account BankSystem Throws exception if 0 < i< 100 getAccount(inti) i is a parameter from BankSystem to Account AccountTypeB AccountTypeC AccountTypeA getAccount(inti) getAccount(inti) getAccount(inti) … if0 < i < 1000 … if0 < i < 100 … if0 < i < 10 To preserve substitutability, sub classes should not impose conditions that are more restrictive than the super class.

  42. by Barbara Liskov Liscov Substitution Principle To preserve substitutability, sub classes should not impose conditions that are more restrictive than the super class.

  43. Commandcmd= createCommand(commandString); cmd.execute(); history.add(cmd); … cmd.undo(); Add Command execute() undo() execute() undo() Delete History execute() undo() add(Command) Edit execute() undo()

  44. Command cmd = createCommand(commandString); cmd.execute(); history.add(cmd); … cmd.undo(); Add {abstract} Command execute() undo() * execute() {abstract} undo() {abstract} Delete History execute() undo() add(Command) Edit execute() undo()

  45. Command cmd = createCommand(commandString); cmd.execute(); history.add(cmd); … cmd.undo(); Add <<interface>> Command execute() undo() * execute() undo() Delete History execute() undo() add(Command) Edit execute() undo()

  46. How to structure? Object rules Collaborating objects Object structure Follow a paradigm 1 2 3 4 OOP Object behavior 1. Abstraction 2. Encapsulation 3. Inheritance 4. Polymorphism

More Related