APT Part2 01
APT Part2 01
Learning Objectives:
• Overall OOP-concept
• Classes
• Object-oriented programs are made up of objects. An object packages data and methods that operate on
that data.
• The difficulty of object-oriented software design is to decompose systems into objects. Factors like
dependency, granularity, reusability and performance influence the software design, often in conflicting and
contradicting ways
• Objects in object-oriented designs often do not have a real world counterpart to make design flexible and
useful for future changes of the software.
Gamma E., Helm R., Johnson R., and Vlissides J., Design Patterns: Elements of Reusable Object-Oriented Software, 1995
Prof. Dr. Phillipp Torkler Advanced Programming Techniques 1
Classes
Class
Defines an object’s interface and implementation. Classes consist of attributes and methods to bundle data and
specifying the object’s internal data, representation and operations the object can perform. A concrete class has
no abstract definitions and can be instantiated.
1 class Student :
2 institue = " DIT "
3
4 def __init__ ( self , sid , firstname ) :
5 self . sid = sid
6 self . firstname = firstname
7
8 def say_hello ( self ) :
9 print ( f " { self . firstname }: I am a student at the { self . institue } and my id is { self . sid }. " )
Objects are created by instantiating a class. It is said that the object is an instance of the class.
Class
Defines an object’s interface and implementation. Classes consist of attributes and methods to bundle data and
specifying the object’s internal data, representation and operations the object can perform. A concrete class has
no abstract definitions and can be instantiated.
1 class Student :
2 institue = " DIT " # Class attribute : shared by all objects
3
4 def __init__ ( self , sid , firstname ) :
5 # Instance attributes : unique per object
6 self . sid = sid
7 self . firstname = firstname
8
9 def say_hello ( self ) :
10 print ( f " { self . firstname }: I am a student at the { self . institue } and my id is { self . sid }. " )
Objects are created by instantiating a class. It is said that the object is an instance of the class.
Class
Defines an object’s interface and implementation. Classes consist of attributes and methods to bundle data and
specifying the object’s internal data, representation and operations the object can perform. A concrete class has
no abstract definitions and can be instantiated.
1 class Student :
2 institue = " DIT " # Class attribute : shared by all objects
3
4 def __init__ ( self , sid , firstname ) :
5 # Instance attributes : unique per object
6 self . sid = sid
7 self . firstname = firstname
8
9 def say_hello ( self ) :
10 print ( f " { self . firstname }: I am a student at the { self . institue } and my id is { self . sid }. " )
Encapsulation
Hiding representations and implementations in an object. Representations are not visible and cannot be modified
directly from outside the object. Only methods are provided to access or modify representations of an object.
1 class Student :
2 def __init__ ( self , sid , firstname ) :
3 self . sid = sid
4 self . firstname = firstname
5 self . _email = " " # ’ private ’ attribute .
6
7 def get_email ( self ) :
8 return self . _email
9
10 def set_email ( self , email ) :
11 if self . verifyEmail ( email ) :
12 self . _email = email
13 # TODO : raise an error if no valid email address is provided
14
15 def verifyEmail ( self , email ) :
16 # TODO : check if a string is a valid email address .
17 return True
Note: a true encapsulation does not exist in Python! By convention, method and attribute names starting with an
underscore are considered being private in Python, but technically they can be accessed and modified.
Prof. Dr. Phillipp Torkler Advanced Programming Techniques 4
Abstraction
Abstraction
The concept of abstraction in computer science is to hide/ignore complex details of a system and focus on the
high-level operation of a system. Complexity is reduced by hiding details.
Abstraction
The concept of abstraction in computer science is to hide/ignore complex details of a system and focus on the
high-level operation of a system. Complexity is reduced by hiding details.
1 class BankManager :
2 def __init__ ( self ) :
3 self . finance_data = None
4 def _connect ( self ) :
5 print ( " Establish connection to your bank account . " )
6 def _ g e t _ f i n a n c e _ d a t a ( self ) :
7 print ( " Retrieve financial data . " )
8 def _ tr an s fo rm _d a ta ( self ) :
9 print ( " Rearrange financial data into a proper format . " )
10 def _disconnect ( self ) :
11 print ( " Close connection to the bank account . " )
12
13 # Keep all ’ complicated ’ internals private and only expose
14 # desired functions to other objects .
15 def get_finances ( self ) :
16 self . _connect ()
17 self . _ g e t _ f i n a n c e _ d a t a ()
18 self . _t ra n sf or m_ d at a ()
19 self . _disconnect ()
20 return self . finance_data
Abstraction
The concept of abstraction in computer science is to hide/ignore complex details of a system and focus on the
high-level operation of a system. Complexity is reduced by hiding details.
• By hiding implementation details, the implementation can be changed without a↵ecting other parts of an
application.
• Maintainability is increased since implementation details are kept at a single point and are not scattered
throughout an application.
Signature
A signature defines a method’s name, parameters (and types) and return values.
Interface
An interface is the set of all signatures of an object. An interface describes the set of all requests an object can
respond to.
Type
A type is a name of a particular interface.
Abstract class
An abstract class defines an interface for its subclasses. Abstract classes cannot be instantiated.
Implementations of abstract classes are deferred to subclasses that inherit from the abstract class.
Abstract class
An abstract class defines an interface for its subclasses. Abstract classes cannot be instantiated.
Implementations of abstract classes are deferred to subclasses that inherit from the abstract class.
1 import abc
2
3 class UIControl ( abc . ABC ) :
4 @abc . abs tractme thod
5 def display () :
6 pass
7
8class Button ( UIControl ) :
9 def display ( self ) :
10 print ( f " Display a button . " )
11
12 class CheckBox ( UIControl ) :
13 def display ( self ) :
14 print ( f " Display a checkbox . " )
Abstract class
An abstract class defines an interface for its subclasses. Abstract classes cannot be instantiated.
Implementations of abstract classes are deferred to subclasses that inherit from the abstract class.
Interface
An interface is the set of all (method) signatures of an object. An interface describes the set of all requests an
object can respond to.
1 class Nonsense :
2 pass
Polymorphism
”Poly” means ”many” and ”morphism” means shape or form. Objects with matching interfaces can be
substituted at run-time.
Interface
An interface is the set of all (method) signatures of an object. An interface describes the set of all requests an
object can respond to.
>>> displayUI(Button())
Display a button.
>>> displayUI(CheckBox())
Display a checkbox.
Objects of type Button and CheckBox are subclasses of the abstract class UIControl. UIControl can take many
di↵erent forms at run-time. Since an abstract class defines the interface of an object, it is guaranteed that Button
and CheckBox will have an implementation of the display() function.
Prof. Dr. Phillipp Torkler Advanced Programming Techniques 9
Interfaces, Abstract Classes and Polymorphism
• The ability to define behavior that can be implemented by a group of unrelated classes without forcing them
to share a common class hierarchy.
• A class can be exposed through its interface, e↵ectively hiding its implementation details by forcing all
communication to be through its public interface methods
• Existing systems are easily modified to provide new functionality within their current class hierarchy.
All classes derived from an abstract class will share its interface. ”There are two benefits to manipulating objects
in terms of the interface defined by abstract classs:
1. Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the
interface that clients expect.
2. Clients remain unaware of the classes that implement these objects. Clients only know about the abstract
class(es) defining the interfaces.
This reduces implementation dependencies between subsystems that it leads to the following principle of
object-oriented design:
1 Gamma E., Helm R., Johnson R., and Vlissides J., Design Patterns: Elements of Reusable Object-Oriented Software, 1995
Learning Objectives:
• Introduction to design patterns
• Definition
• Motivation
• ”Gang of Four”
Design Patterns: Definitions
Design Patterns
A design pattern systematically names, motivates and explains a general design that addresses a recurring design
problem in object-oriented systems. It describes the problem, the solution, when to apply the solution and the
consequences.
Why should I have a look at design patterns? Designing OOP software is hard, and designing flexible and reusable
OOP software is even harder. Design patterns...
Adapter Pattern
Convert the interface of a class into another interface clients expect. The adapter pattern lets classes work
together that couldn’t otherwise because of incompatible interfaces.
Participants:
• Target
Target • defines the interface that Client uses.
C lient
request() • Client
• works with objects conforming to the Target interface.
• Adaptee
• an existing interface that does not match to the
Adapter
Adaptee Target interface and needs adapting.
adaptee
nonm atching_request()
• Adapter
request()
• adaptes the interface of Adaptee to the Target
interface.
Adapter Pattern
Convert the interface of a class into another interface clients expect. The adapter pattern lets classes work
together that couldn’t otherwise because of incompatible interfaces.
Applicability:
• Use the adapter pattern when you want to (re)use an existing class, and its interface does not match the one
you need.
• Use the adapter pattern when you want to create a reusable class that cooperates with unrelated or
unforeseen classes, that is, classes that don’t necessarily have compatible interfaces.
• Use the adapter pattern when existing classes cannot be modified or are too complex to modify.
Composite Pattern
Compose objects into trees to represent part-whole hierarchies. Composite lets clients treat individual objects
and compositions of objects uniformly by using a common interface.
Participants:
• Component:
• specifies the interface for objects that are identical for
Component
C lient simple and complex objects in the composition.
operation() • Composite
• defines behavior for components having children.
• stores child components.
• delegates work to child elements.
Com posite • Leaf
Leaf
• has no children and defines behavior for primitive
operation()
operation() add() objects in the composition.
remove() • typically performs the most work since requests are
delegated to a leaf.
• Client
• stores and manipulates the composition.
Prof. Dr. Phillipp Torkler Advanced Programming Techniques 18
Composite Pattern
Composite Pattern
Compose objects into trees to represent part-whole hierarchies. Composite lets clients treat individual objects
and compositions of objects uniformly by using a common interface.
Applicability:
Use the Composite pattern when:
• Open-closed-principle. New types can be added to a tree without a↵ecting any previous objects.
• Depending on the elements it might be difficult to find an interface common to all elements.
Related Patterns: