CIT 590 Intro to Programming Lecture 10 (object oriented programming)

advertisement
CIT 590
Intro to Programming
Lecture 10 (object oriented programming)
Agenda
• Objects
• MVC
Objects/Services
• A program written in an object oriented paradigm will have
• Classes
• Each class providing service methods that are utilized by other
classes
• Metaphorically speaking we are passing messages to
service providers
A stack object
• When you instantiate a stack object you will get access to
methods like
• Push – put an element onto the top of the stack
• Pop – pop the first element out of the stack
Distinction between class and instance
• Class is the template
• An instance is a specific example of that template
• An example with stacks
Creating a calculator using stacks
• Reverse polish notation
• Clearly we need a class that implements stack
functionality
• Given that we have implemented stacks, we can write
what we generically call calc code
• Module dependencies (we’ve probably seen this before
by now?)
Object oriented programming
• The stack object was not concerned with the fact that it
was going to eventually be used to solve reverse polish
notation problems
• The reverse polish calculator engine is not concerned with
the fact that the user is going to be entering operators and
operands split by space
• Advantages
• Understanding the program is easier
• Testing!
• General purpose classes like stacks can be reused
Model view separation
• Separate the actual calculation implementation from the
user interface
• Many views can exist for the same model
• GUI, text based inputs, different formats …
Infix calculator
• Building a wrapper class on top of the RPNCalculator
• Note how both the RPNCalculator and the Stack class are
being used in this case
Discovering objects
• Usage of CRC – class responsibility collaborator pattern
• Write the nouns and verbs that are part of the application
• Each noun defines an entity
• Verbs matched up with a who for a class
Duck Typing
• Class implementations are implementing the abstract
concept of a type
• Any object that implements the members and the
functions that are required will be considered to be of the
appropriate type
Encapsulation and properties
• Usage of getters and setters instead of directly accessing
the properties on an object
• Nothing in the python language will really prevent you
from doing the direct access though
• The underscore convention is used for data that should
rarely (if ever) be directly accessed
• You can remove the function call syntax by using property
syntax
property(getter function name, setter function name)
Use None if you want to ensure that there is no getter or no
setter
Summary
• Recursion
• Throwing exceptions
• IDLE debugger
Download