Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment...
-
Upload
terrance-knaggs -
Category
Documents
-
view
212 -
download
0
Transcript of Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment...
Lilian Blot
Announcements
Teaching Evaluation Form week 9 practical session
Formative Assessment week 10 during usual practical sessions
group 1 Friday at 11:30 group 2 Wednesday at 9:30 bring your card
Autumn 2013TPOP
1
Lilian BlotLilian Blot
OOPANOTHER PROGRAMMING
PARADIGM
Object Oriented Programming
Autumn 2013TPOP
2
Lilian Blot
Last Week: ADT implementation
see code: Linked_Queue_struct_Answer.py
Autumn 2012TPOP
3
Lilian Blot
Encapsulation as Information hiding
Data belonging to one object is hidden from other objects.
Know what an object can do, not how it does it.
Information hiding increases the level of independence.
Independence of modules is important for large systems and maintenance.
Autumn 2013TPOP
4
Lilian Blot
What are Classes?
Classes are composed from structural and behavioural constituents.
Data field members (member variables or instance variables) enable a class instance to maintain state. a.k.a properties, fields, data members, or attributes
Methods, enable the behaviour of class instances.
Classes define the type of their instances
Autumn 2013TPOP
5
Lilian Blot
Code quality
Two important concepts for quality of code:
1. Coupling
2. Cohesion
Autumn 2013TPOP
6
Lilian Blot
Coupling
Coupling refers to links between separate units of a program.
If two classes depend closely on many details of each other, we say they are tightly coupled.
We aim for loose coupling.
Autumn 2013TPOP
7
Lilian Blot
Loose coupling
Loose coupling makes it possible to:
understand one class without reading others;
change one class without affecting others.
Thus: improves maintainability.
Autumn 2013TPOP
Lilian Blot
Cohesion
Cohesion refers to the number and diversity of tasks that a single unit is responsible for.
If each unit is responsible for one single logical task, we say it has high cohesion.
Cohesion applies to classes and methods.
We aim for high cohesion.
Autumn 2013TPOP
9
Lilian Blot
High cohesion
High cohesion makes it easier to:
understand what a class or method does;
use descriptive names;
reuse classes or methods.
Autumn 2013TPOP
Lilian Blot
Cohesion of methods
A method should be responsible for one and only one well defined task.
Autumn 2013TPOP
Lilian Blot
Cohesion of classes
Classes should represent one single, well defined entity.
Autumn 2013TPOP
Lilian BlotLilian Blot
PART I I : IMPLEMENTATION
Object Oriented Programming
Autumn 2013TPOP
13
Lilian Blot
Python and OOP
ModulesClasses : class keywordAttributesMethods: __repr__(self) for exampleConstructor: __init__(self,…)
Autumn 2013TPOP
14
Lilian Blot
Towards OOP
f
Autumn 2013TPOP
15
class QueueOOP: def __init__(self): self._head = None ## Pointer to front of queue, _Node object self._tail = None ## Pointer to back of the queue , _Node object self._size = 0
def dequeue(self):…
def enqueue(self,element):… def isempty(self):…
def size(self):…
Code OOP
class LinkedQueue: def __init__(self, element): self.head = None ## Pointer to front of queue self.tail = None ## Pointer to back of the queue self.size = 0
Code Procedural
Lilian Blot
Towards OOP
Autumn 2013TPOP
16
def dequeue(queue): if queue.size == 0: raise IndexError('dequeue from empty queue') else: ## remove first element, the head element = queue.head.datum queue.head = queue.head.next if queue.head is None: queue.tail = None queue.size -= 1 return element
Code Procedural
def dequeue(self): if self.isempty(): raise IndexError('dequeue from empty queue') else: ## remove first element, the head element = self._head.datum self._head = self._head.next if self._head is None: self._tail = None self._size -= 1 return element
Code OOP Internal method call
Lilian Blot
Method calls
Internal method calls (inside class definition) self.method_name(parameters)
External method calls object.method_name(parameters)
Autumn 2013TPOP
17
queue = QueueOOP() # Constructor Call of class QueueOOP queue.enqueue(1) queue.enqueue(3) print 'dequeued element is:', queue.dequeue() print 'size is:', queue.size()
Code External Method Call
External method call
(class QueueOOP)
Lilian Blot
Code quality
Two important concepts for quality of code:
1. Low Coupling
2. High Cohesion
Autumn 2013TPOP
18
Lilian Blot
Low Coupling
f
Autumn 2013TPOP
19
class QueueOOP: def __init__(self): self._data = [] ## list object containing all element in queue
def dequeue(self):…
def enqueue(self,element):…
Code OOP second implementation
class QueueOOP : def __init__(self, element): self._head = None ## Pointer to front of queue self._tail = None ## Pointer to back of the queue self._size = 0 def dequeue(self):…
def enqueue(self,element):…
Code OOP first implementation
Lilian Blot
Low Coupling
Autumn 2013TPOP
20
def dequeue(self): if self.isempty()== 0: raise IndexError('dequeue from empty queue') else: ## remove first element, the head element = self._head.getElement() self._head = self._head.next() self._size -= 1 return element
Code OOP first implementation
def dequeue(self): if self.isempty()== 0: raise IndexError('dequeue from empty queue') else:
## remove and return first element return self._data.pop()
Code OOP second implementation
Lilian Blot
Low Coupling
Autumn 2013TPOP
21
queue = QueueOOP() # Constructor Call of class QueueOOP queue.enqueue(1) queue.enqueue(3) print 'dequeued element is:', queue.dequeue() print 'size is:', queue.size()
Code Using QueueOOP
The use of QueueOOP is independent of implementation
The change of implementation does not have an impact on the program using the ADT.
Lilian Blot
High Cohesion
Autumn 2013TPOP
22
The class QueueOOP contains only method necessary for this type of ADT. does NOT implement add_first, insert_at, remove, etc.
If the user need such methods, he/she must use a different ADT (not a queue).
Several ADTs could/should be implemented in the same module
Lilian Blot
Writing class documentation
Your own classes should be documented the same way library classes are.
Other people should be able to use your class without reading the implementation.
Make your class a 'library class'!
Autumn 2013TPOP
23
Lilian Blot
Elements of documentation
Documentation for a class should include: the class name a comment describing the overall purpose and
characteristics of the class a version number the authors’ names documentation for the constructor and each method
Autumn 2013TPOP
24
Lilian Blot
Elements of documentation
The documentation for each constructor and method should include: the name of the method the return type (if known) the parameter names and types (if needed) a description of the purpose and function of the
method a description of each parameter a description of the value returned
Autumn 2013TPOP
25
Lilian Blot
Summary
By now, you should be able to: create small scripts/program using selection and
repetition Decomposed complex problems into smaller sub-
problems Use Modularisation
Separation of concerns Semantically coherent Function Classes/OOP
Handle Abstract Data Type Write Documentation and use correct code style
(conventions)Autumn 2013TPOP
26
Lilian Blot
Exercises
Rewrite Your Queue and Deque ADT using OOP You can use practical 6 model answers
Rewrite the Library, Member and Item classes from practical 5 if you haven’t done so, you should create one class for the
library, one for members and one for items Low Coupling: separate user interface and core of the
program (e.g. adding members to library, borrowing items,…) use separate files, one containing the three classes Library,
Member, Item, and one for the User Interface (reading inputs from users)
Autumn 2013TPOP
27