0% found this document useful (0 votes)
14 views20 pages

Decorator Pattern

The document discusses Object-Oriented Programming concepts, focusing on design patterns such as the Decorator Pattern, which allows adding functionality to individual objects without modifying existing code. It contrasts this with inheritance, which applies changes to all instances of a class. Additionally, it touches on the importance of AI in engineering and outlines topics covered in lectures, including encapsulation, association, and polymorphism.

Uploaded by

hygge.opp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

Decorator Pattern

The document discusses Object-Oriented Programming concepts, focusing on design patterns such as the Decorator Pattern, which allows adding functionality to individual objects without modifying existing code. It contrasts this with inheritance, which applies changes to all instances of a class. Additionally, it touches on the importance of AI in engineering and outlines topics covered in lectures, including encapsulation, association, and polymorphism.

Uploaded by

hygge.opp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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

You might also like