460 likes | 470 Views
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.
E N D
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.
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. ……………..
OOP: Advanced Concepts CS2103/T, Lecture 5, Part 2, [Sep 9, 2016]
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
Example 1 I want to register Students Here you go! UGStudent Admin NGStudent PGStudent
Example 1 Gaaah….! UGStudent Admin NGStudent PGStudent
Example 1 foreachStudent s: s.register(); … Can? Cannot! UGStudent Admin NGStudent PGStudent
Example 2 Storage s; … s.load(); … Logic Storage
Example 2 TestDriver Logic Storage StorageStub
Example 2 TestDriver Logic Storage StorageStub Dependency Injection
Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection
Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection
Example 2 Storage s; … s.load(); … TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; } Dependency Injection
Example 2 TestDriver Logic Storage StorageStub setStorage(Storage) Storage s; … void setStorage(Storage s){ this.s = s; }
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
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
Different game, different behavior Same hardware/ software Polymorphism
= ability to take many forms Polymorphism
? Polymorphism
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
foreachStudent s: s.register(); … UGStudent * Admin Student NGStudent register() PGStudent
foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent register() Inheritence PGStudent
foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent register() PGStudent
foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent register() PGStudent
If expecting super class, can accept sub class foreachStudent s: s.register(); … :UGStudent UGStudent * Admin Student NGStudent Substitutability PGStudent
Storage s; … void setStorage(Storage s){ this.s = s; } Example 2 Storage TestDriver Logic setStorage(Storage) load() StorageStub Logic logic; … logic .setStorage(new StorageStub() );
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?
FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() Overriding StorageStub load()
Storage s; … s.load(); At compile time… FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub load()
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()
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()
At run-time… :Logic s:??????? Storage s; … s.load(); FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() Dynamic binding StorageStub load()
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()
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()
FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() load() StorageStub abstract void load () ; load()
Incomplete! Cannot create objects! FileStorage Storage load() TestDriver Logic DBStorage setStorage(Storage) load() {abstract} load() StorageStub load()
FileStorage {abstract} Storage load() TestDriver Logic DBStorage setStorage(Storage) load() {abstract} load() Abstract method/class StorageStub load()
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()
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
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.
by Barbara Liskov Liscov Substitution Principle To preserve substitutability, sub classes should not impose conditions that are more restrictive than the super class.
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()
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()
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()
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