Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.

Similar presentations


Presentation on theme: "Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday."— Presentation transcript:

1 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 2013 TPOP 1

2 Lilian Blot OOP ANOTHER PROGRAMMING PARADIGM Object Oriented Programming Autumn 2013 TPOP 2

3 Lilian Blot Last Week: ADT implementation see code: Linked_Queue_struct_Answer.py Autumn 2012 TPOP 3

4 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 2013 TPOP 4

5 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 2013 TPOP 5

6 Lilian Blot Code quality Two important concepts for quality of code: 1. Coupling 2. Cohesion Autumn 2013 TPOP 6

7 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 2013 TPOP 7

8 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 2013 TPOP

9 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 2013 TPOP 9

10 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 2013 TPOP

11 Lilian Blot Cohesion of methods A method should be responsible for one and only one well defined task. Autumn 2013 TPOP

12 Lilian Blot Cohesion of classes Classes should represent one single, well defined entity. Autumn 2013 TPOP

13 Lilian Blot PART II: IMPLEMENTATION Object Oriented Programming Autumn 2013 TPOP 13

14 Lilian Blot Python and OOP Modules Classes : class keyword Attributes Methods: __repr__(self) for example Constructor: __init__(self,…) Autumn 2013 TPOP 14

15 Lilian Blot Towards OOP f Autumn 2013 TPOP 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

16 Lilian Blot Towards OOP Autumn 2013 TPOP 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

17 Lilian Blot Method calls Internal method calls (inside class definition) self.method_name(parameters) External method calls object.method_name(parameters) Autumn 2013 TPOP 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)

18 Lilian Blot Code quality Two important concepts for quality of code: 1. Low Coupling 2. High Cohesion Autumn 2013 TPOP 18

19 Lilian Blot Low Coupling f Autumn 2013 TPOP 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

20 Lilian Blot Low Coupling Autumn 2013 TPOP 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

21 Lilian Blot Low Coupling Autumn 2013 TPOP 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.

22 Lilian Blot High Cohesion Autumn 2013 TPOP 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

23 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 2013 TPOP 23

24 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 2013 TPOP 24

25 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 2013 TPOP 25

26 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 2013 TPOP 26

27 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 havent 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 2013 TPOP 27


Download ppt "Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday."

Similar presentations


Ads by Google