Download presentation
Presentation is loading. Please wait.
Published byMorris Butler Modified over 9 years ago
2
Lilian Blot Announcements The TPOP problem class this afternoon: group 1 should come at 3.30pm and group 2 at 4pm. Teaching Evaluation Form week 9 practical session good bad Formative Assessment week 10 during usual practical sessions Tuesday at 16:30 & Wednesday at 11:30 bring your card Be on time, no entry accepted after the start of the assessment Autumn 2014 TPOP 1
3
Lilian Blot OOP ANOTHER PROGRAMMING PARADIGM Object Oriented Programming Autumn 2014 TPOP 2
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 2014 TPOP 3
5
Lilian Blot Information Hiding f All Attributes are public Autumn 2014 TPOP 4 class Member: def __init__(self, firstname, surname, postcode, uid): self.firstname = firstname self.surname = surname self.postcode = postcode self.uid = uid self.borrowed = [] ## a list of items uid def __repr__(self): return ('<Member: uid = ' + str(self.uid) + ', ' + self.surname + ', ' + str(self.borrowed) + '>') Code No Information Hiding
6
Lilian Blot Private vs Protected vs Public Attributes Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world. There is no completely private attributes in Python Private attributes can be simulated using two underscores for prefix __attributename Protected attributes have a single underscore for prefix _attributename No underscore prefix for public attributes Spring 2014 TPOP 5
7
Lilian Blot Private vs Protected vs Public Attributes If using private/protected attribute, you should provide adequate Accessors method (read/get value) Mutators method (change/set value) Which attribute must be public/protected/private is a design decision Which mutator/accessor to provide is also a design decision Spring 2014 TPOP 6
8
Lilian Blot Information Hiding f All Attributes are protected Autumn 2014 TPOP 7 class Member: def __init__(self, firstname, surname, postcode, uid): self._firstname = firstname self._surname = surname self._postcode = postcode self._uid = uid self._borrowed = [] ## a list of items uid def __repr__(self): return ('<Member: uid = ' + str(self._uid) + ', ' + self._surname + ', ' + str(self._borrowed) + '>') Code How to access Information?
9
Lilian Blot Defining Class Behaviour Methods, enable the behaviour of class instances. def methodName (self, ): Accessors are methods to read/access the values of some attributes Autumn 2014 TPOP 8 def getSurname(self): return self._surname def getUID(self): return self._uid Code Accessor
10
Lilian Blot Defining Class Behaviour Mutators are methods to set/modify the values of some attributes. We may want some attributes not to be modified by an external source (e.g. another class), so no mutator should be provided. For example UID should never be changed (design decision) Autumn 2014 TPOP 9 def setSurname(self, name): self._surname = name Code Mutator
11
Lilian Blot Defining Class Behaviour How about modifying _borrowed attribute? What are the drawbacks of this design? Use another design Autumn 2014 TPOP 10 def setBorrowed(self, borrowed): self._ borrowed = borrowed Code Mutator def addBorrowed(self, borrowed_item): if borrowed_item in self._borrowed: raise Exception(‘Already in borrowed’) else: self._ borrowed.append(borrowed_item) Def removeBorrowed(self, borrowed_item):... Code
12
Lilian Blot Method calls Internal method calls (inside class definition) self.method_name(parameters) External method calls object.method_name(parameters) Autumn 2014 TPOP 11 # Constructor Call of class QueueOOP lilian = Member(‘blot’,’lilian’,’yox xgh’, ‘01’) print ‘Surname is:', lilian.getSurname() Code External Method Call External method call (class Member)
13
Lilian Blot THE LIBRARY CLASS Class Design Autumn 2014 TPOP 12
14
Lilian Blot Code quality Two important concepts for quality of code: 1. Coupling 2. Cohesion Autumn 2014 TPOP 13
15
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 2014 TPOP 14
16
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 2014 TPOP
17
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 2014 TPOP 16
18
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 2014 TPOP
19
Lilian Blot Cohesion of methods A method should be responsible for one and only one well defined task. Autumn 2014 TPOP
20
Lilian Blot Cohesion of classes Classes should represent one single, well defined entity. Autumn 2014 TPOP
21
Lilian Blot Last Week Practical Design Two dictionaries, one handling members and one handling items Both dictionaries strongly coupled, however they are not part of the same data structure Autumn 2014 TPOP 20 def add_member(firstname, surname, postcode, uid, listMembers): if uid in listMembers: ## uid already existing so must not add item return None else: member = Member(firstname, surname, postcode, uid) listMembers[uid] = member return member members = {} # keys are members UID, values are Member objects print 'Add member :', add_member('lilian','blot','xxx', '007', members) Code
22
Lilian Blot Design a Library Class Encapsulate the Items and Members into a third class Library What are the attributes? Should the attributes be public, protected, or private? What are the Accessors? What are the Mutators? To help in designing the class, think about a real world library: what actions that can be done in a library? Autumn 2014 TPOP 21
23
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 Write Documentation and use correct code style (conventions) Autumn 2014 TPOP 22
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.