Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are...

25
Object Oriented Programming CS115

Transcript of Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are...

Page 1: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Object Oriented Programming

CS115

Page 2: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Object Oriented Design

• Most modern computer applications are designed using a data-centered view of computing called object-oriented design (OOD).

• The essence of OOD is describing a system in terms of magical black boxes and their interfaces.

Page 3: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

The Process of OOD

• Each component provides a service or set of services through its interface.

• Other components are users or clients of the services.

• A client only needs to understand the interface of a service – implementation details are not important, they may be changed and shouldn’t affect the client at all!

Page 4: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

The Process of OOD

• The component providing the service shouldn’t have to consider how the service is used – it just needs to provide the service “as advertised” via the interface.

• This separation of concerns makes the design of complex systems possible.

Page 5: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

The Process of OOD

• Breaking a large problem into a set of cooperating classes reduces the complexity that must be considered to understand any given part of the program. Each class stands on its own!

• OOD is the process of finding and defining a useful set of classes for a given problem.

• Like design, it’s part art and part science. The more you design, the better you’ll get.

Page 6: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

OO Concepts

• The OO approach helps us to produce complex software that is more reliable and cost-effective.

• OO is comprised of three principles:– Encapsulation– Polymorphism– Inheritance

Page 7: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Encapsulation

• As you’ll recall, objects know stuff and do stuff, combining data and operations.

• This packaging of data with a set of operations that can be performed on the data is called encapsulation.

• Encapsulation provides a convenient way to compose complex problems that corresponds to our intuitive view of how the world works.

Page 8: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Encapsulation

• From a design standpoint, encapsulation separates the concerns of “what” vs. “how”. The implementation of an object is independent of its use.

• The implementation can change, but as long as the interface is preserved, the object will not break.

• Encapsulation allows us to isolate major design decisions, especially ones subject to change.

Page 9: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Encapsulation

• Another advantage is that it promotes code reuse. It allows us to package up general components that can be used from one program to the next..

• Encapsulation alone makes a system object-based. To be object-oriented, we must also have the properties of polymorphism and inheritance.

Page 10: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Polymorphism

• Literally, polymorphism means “many forms.”

• When used in object-oriented literature, this refers to the fact that what an object does in response to a message (a method call) depends on the type or class of the object.

Page 11: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Polymorphism

• With polymorphism, a given line in a program may invoke a completely different method from one moment to the next.

• Suppose you had a list of graphics objects to draw on the screen – a mixture of Circle, Rectangle, Polygon, etc.

Page 12: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Polymorphism

• You could draw all the items with this simple code:for obj in objects: obj.draw(win)

• What operation does this loop really execute?

• When obj is a circle, it executes the draw method from the circle class, etc.

Page 13: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Polymorphism

• Polymorphism gives object-oriented systems the flexibility for each object to perform an action just the way that it should be performed for that object.

Page 14: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Inheritance

• The idea behind inheritance is that a new class can be defined to borrow behavior from another class.

• The new class (the one doing the borrowing) is called a subclass, and the other (the one being borrowed from) is called a superclass.

• This is an idea our examples have not included.

Page 15: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Inheritance

• Say we’re building an employee management system.

• We might have a class called Employee that contains general information common to all employees. There might be a method called homeAddress that returns an employee’s home address.

Page 16: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Inheritance

• Within the class of employees, we might distinguish between salaried and hourly employees with SalariedEmployee and HourlyEmployee, respectively.

• Each of these two classes would be a subclass of Employee, and would share the homeAddress method.

Page 17: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Inheritance

• Each subclass could have its own monthlyPay function, since pay is computed differently for each class of employee.

• Inheritance has two benefits:– We can structure the classes of a system to avoid

duplication of operations, e.g. there is one homeAddress method for HourlyEmployee and SalariedEmployee.

– New classes can be based on existing classes, promoting code reuse.

Page 18: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Namespaces• At the simplest level, classes are simply namespaces

• class myfunctions:• def exp():• return 0

• >>> math.exp(1)• 2.71828...• >>> myfunctions.exp(1)• 0

• It can sometimes be useful to put groups of functions in their own namespace to differentiate these functions from other similarly named ones.

Page 19: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Python Classes

• Classes define objects• Objects are instances of classes

class atom:def

__init__(self,atno,x,y,z):self.atno =

atnoself.position =

(x,y,z)

__init__ is the default constructor

self refers to the object itself,like this in Java.

Page 20: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

example

class atom:def __init__(self,atno,x,y,z):

self.atno = atnoself.position = (x,y,z)

def __repr__(self): # overloads printingreturn '%d %10.4f %10.4f %10.4f' %

(self.atno, self.position[0],

self.position[1],self.position[2])

>>> at = atom(6,0.0,1.0,2.0)>>> print(at)6 0.0000 1.0000 2.0000

Page 21: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Discussion

• Overloaded the default constructor• Defined class variables (atno,position) that are

persistent and local to the atom object• Good way to manage shared memory:– instead of passing long lists of arguments,

encapsulate some of this data into an object, and pass the object.

– much cleaner programs result• Overloaded the print operator

Page 22: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

© 2000 Richard P. Muller

Classes that look like arrays

• Overload __getitem__(self,index) to make a class act like an array

Page 23: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Classes that look like functions• Overload __call__(self,arg) to make a class

behave like a functionclass gaussian:

def __init__(self,exponent):self.exponent = exponent

def __call__(self,arg):return math.exp(-

self.exponent*arg*arg)

>>> func = gaussian(1.)>>> func(3.)0.0001234

Page 24: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Other things to overload• __setitem__(self,index,value)

– Another function for making a class look like an array/dictionary– a[index] = value

• __add__(self,other)– Overload the "+" operator– molecule = molecule + atom

• __mul__(self,number)– Overload the "*" operator– zeros = 3*[0]

• __getattr__(self,name)– Overload attribute calls– We could have done atom.symbol() this way

Page 25: Object Oriented Programming CS115. Object Oriented Design Most modern computer applications are designed using a data-centered view of computing called.

Other things to overload, cont.• __del__(self)

– Overload the default destructor– del temp_atom

• __len__(self)– Overload the len() command– natoms = len(mol)

• __getslice__(self,low,high)– Overload slicing– glycine = protein[0:9]

• __cmp__(self,other):– On comparisons (<, ==, etc.) returns -1, 0, or 1, like C's strcmp