Object-Oriented
Programming
WBCS028-05
Think about
AI or Imitation Game?
Engineers must produce products that are ‘fit to use’
use AI when you have the time and capacity to assess it
Future: Systems to do things that humans cannot do
Recap
Lecture 1: Encapsulation / class + object /
access modifier
Lecture 2: Association / Inheritance /
Lecture 2: Association / Inheritance /
abstract class / interface
Polymorphism
Design Patterns
set of tested approaches for specifying the relations between classes or objects to
solve a certain design problem.
Decorator Pattern
enables adding functionality without modifying existing code
Concept
Human can eat, work
Child is a human
Child can eat, work
Intent
Add extra functionality to individual objects, not to classes.
Inheritance adds functionality to ALL instances and will produce duplication
Solution
enclose the component in another object (decorator) that adds this extra
functionality.
•The decorator conforms to the interface of the component it decorates. This way,
its presence is transparent to the component's clients
•The decorator forwards requests to the component and may perform additional
actions before or after forwarding.
•Nested decorators allow an unlimited number of added responsibilities.
Scenario: Book cost
Book: 20 euros Book: 20 euros
Wrapping paper: 1 euro Wrapping paper: 1 euro
Card: 1 euro VAT: 21%
VAT: 21%
Total cost: 20+1+1+4.2=26.2 Total cost: 20+1+4.2=25.2
Connected through interface
ProductPrice (Interface)
implements
Product (abstract) DecoratorPrice (abstract)
extends
Book Pot WrappingPrice CardPrice
Solution
enclose the component in another object (decorator) that adds this extra
functionality.
•The decorator conforms to the interface of the component it decorates. This way,
its presence is transparent to the component's clients
•The decorator forwards requests to the component and may perform additional
actions before or after forwarding.
•Nested decorators allow an unlimited number of added responsibilities.
Decorator Pattern vs. Inheritance
Inheritance applies to all objects of a class
Decorative pattern enables changing individual objects
Lecture 7: Observer Pattern + Exam