03OOP

Embed Size (px)

Citation preview

  • 8/3/2019 03OOP

    1/3

    CS193j, Stanford Handout #3Winter, 2002-03 Nick Parlante

    OOP

    Procedural vs. OOP

    Nouns and VerbsNou ns -- dataVerbs -- operations

    Procedural StructureC/ Pascal/ etc. ...

    Verb orienteddecomp osition around the verbs -- dividing the big operation into a series ofsmaller and smaller operations.

    Noun s/ Verb structure is not formalThe programmer can grou p the verbs and nouns together (ADTs), but it's just

    a convention and the compiler d oes not especially help ou t.

    OOP Structure

    ObjectsStorageObjects store state at runtime (ivars)Behavior

    Objects will also in som e sense take an active role. Each object has a set ofopera tions that it can p erform, usually on itself. (meth ods)

    ClassEvery object belongs to a class that d efines its storage and behav ior.An object always r emem bers its class (in Java)."Instan ce" is another word for object -- an "instance" of a class.

    Anth ropomorph ic -- self-containedProcedu ral variables are passive -- they just sit there. A procedu re is called

    and it changes the variable.

    Objects are anth ropomorph ic-- the object has both storage and behav ior tooperate on that state.

    String examp leCould have a couple String objects, each of which stores a sequ ence of

    characters. The objects belong to the String class. The String class defines thestorage an d operations for the String objects...

  • 8/3/2019 03OOP

    2/3

    String class

    length() {---;---;}

    reverse() {---;---;}

    hello

    String

    hi

    String

    length()

    5

    Sending the length()message to a Stringobject...

    ClassExists once -- there is one copy of the class in memory.Defines the storage and behav ior of its objectsStorage

    Define th e storage th at objects of this class w ill have."instance variables" -- the variables that each object w ill use for its own

    storage. Instance variables are u sually just called "ivars".Behavior

    Define th e behaviors tha t objects of this class can execute (method s).

    String examp leThe String class d efines the storage stru cture u sed by a ll String objects --probably an array of chars of some sort

    The String class also defines the operations tha t String objects can per form onthem selves -- length(), reverse(), ...

    Message / ReceiverSup pose w e have Studen t objects, each of which h as a current n um ber of units.

    The message getUnits() requests the u nits from a stud ent.Java syntax:

    a.getUnits()send the "getUnits()" message to the receiver "a"

    ReceiverThe "receiver" is the object receiving th e message. Typically, the operation

    uses the receiver's memory.

    Method (code)A "method " is executable code d efined in a class.The objects of a class can execute all the m ethod s their class defines.

  • 8/3/2019 03OOP

    3/3

    The String class defines the code for length () and reverse() methods. Themeth ods are ru n by sen ding the "length ()" or "reverse()" message to a Stringobject.

    Message -> Method resolutionSupp ose a message is sent to an object --- x.reverse();1. The receiver, x, is of some class -- suppose x is of the String class2. Look in that class of the receiver for a m atching rever se() method (code)3. Execute tha t code "against" the receiver-- using its m emory (instance variables)In Java this is "dyn amic" -- the message/ method resolution u ses the true, run-

    time class of the receiver.

    OOP Design - Anthropomorphic,Modular1. Objects responsible for their ow n sta te -- as m uch as p ossible, object's d o not

    reach in to read or write the state of other objects.2. Objects can sen d messages to each other -- requests3. The object/ message parad igm makes the program m ore modu lar internally.

    Each class deals with its own implementation d etails, but can be largelyindepen dent of the details of the other classes. They just exchan ge messages.

    OOP Design Rule #1 -- EncapsulationObjects "protect" their own state from d irect access by other objects --

    "encapsu lation". Other objects can send requests, but on ly the receiver actuallychanges its own state. This allows m ore reliable software -- once a class iscorrect and debu gged, pu tting it in a new context should n ot create new bugs.

    Abstraction vs. ImplementationThis is the old Abstract Data Type (ADT) style of separating the abstr action

    from the imp lementation, but structured as m essages (abstraction) vs.method s (implementation)

    OOP Design ProcessThink about the objects that m ake up an app licationThink abou t the behav iors or capabilities those objects should haveEndow th e objects with those abilities as meth odsIf a capability does not occur to you in the initial design, that's ok. Add it to the

    app ropriate class when needed -- the just needs to go in the right classCo-operation

    Objects send each other m essages to co-operateTidy style

    Exper ience show s that having each object operate on its own state is a prettyintuitive and mod ular way to organize things.