object-oriented design chapter 16. real objects and software objects “real” objects are things...

40
Object-oriented Design Chapter 16

Upload: lynne-kelley

Post on 01-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Object-oriented Design

Chapter 16

Page 2: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Real Objects and Software Objects

• “Real” objects are things that can be manipulated and used for different tasks. So, too, are software objects.

• Real objects fit into general classes with the same general purpose and means of use: cars, televisions, microwaves, lawnmowers, dryers, etc.

• In addition, there are very broad classes that include general classes. For instance, “appliances” would include microwaves, televisions and dryers.

Page 3: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

What is a Software 'Object'?

• Software objects are similar to real-world objects in that they can have general uses, and fit into classes.

• There is also the capability to have “classes of classes” just like “appliances” for real world objects.

Page 4: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

The Object Paradigm

• With software objects, however, we don’t interact ‘directly’ with them (like turning on a television.) Instead, we ‘send the object a message’.

• A software object can accept and understand any ‘message’ or messages we design it to accept.

• The model (or ‘paradigm’) is that we ‘talk’ to objects and send it instructions about what to do. In turn, the object may send messages to other objects to help it with it’s task.

Page 5: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Object-oriented requirements

• Object-oriented requirements definition lends itself to definition of ‘analog’ systems: systems designed on the basis of existing physical or manual systems (such as accounting ledgers, invoices, receipts, etc.)

• There are techniques for extracting object requirements from unstructured requirements definitions (such as rambling prose) that work quite well!

Page 6: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Modeling the physical world

• Since a major strength of the object-oriented approach is the object ‘paradigm’, it’s best to model the physical world when building object oriented systems.

• For instance, since we can ‘post’ an entry to a ledger in an accounting system, it would perhaps be wise to create an object class called ‘ledger’ that understands a message named ‘post’ that contains a ledger entry in the message.

Page 7: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

The role of requirements

• In the instance of object-oriented systems, the requirements give the designer instructions about what objects should be constructed, and what messages they should understand.

• In addition, the requirement must give the designer an idea of how the objects can be organized, and what other objects they must ‘collaborate’ with.

Page 8: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Booch method

• One popular method, the Booch method, models objects on several levels: a logical levell, physical model, dynamic model and static model.

• The Booch method is perhaps one of the most popular methods, and merits careful study by the serious object-oriented designer.

Page 9: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Shlaer-Mellor method

• The Shlaer-Mellor method uses a slightly different approach from the Booch model in that it models three basic aspects of the system: Object information, state and processing.

• There are many different ways to model objects, but we shall examine a popular ‘pencil-and-paper’ approach.

Page 10: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Class-Responsibilites-Collaboration (CRC) Method

• The CRC method uses `CRC’ cards that are essentially 3 by 5 index cards (or whatever is handy).

• The approach is to `decompose’ the requirements to identify classes and patterns, then look at their responsibilities.

• After the classes and responsibilities are identified, the next step is to look at how they need to work together: their ‘collaborations’ in other words.

Page 11: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Identification of classes and patterns

• When looking for classes and patterns, scan the specifications for nouns and noun phrases (nouns or adjective/noun combinations).

• These are ‘candidate classes’, and we create a CRC class to represent these classes.

• We may, at a later time, change the name of the class, or discard it altogether. Remember, at this time the classes are merely ‘candidates!’

Page 12: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Identification of Responsibilities

• Once we have these candidate class CRC cards, we look at the verb phrases (verbs or verb/adverb combinations) to identify possible responsibilities.

• These candidate responsibilities are also entered on the respective CRC cards. Again, these responsibilities may later be modified or discarded!

• We can begin to see ‘action on object’ potentials from the associated class names and verb/noun combinations (‘post and entry’ to a ‘ledger’).

Page 13: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Identification of collaborators

• In looking for object collaborations, we need to look for possible relationships, such as:– Object A is like Object B– Object A knows about Object B– Object A shares information with Object B

• These relationships are entered on the CRC card as candidate collaborations.

Page 14: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Polymorphism

Designers may find that there are general classes of objects, such as ledgers and journals, who share similar messages (such as ‘post entry’) but have other unique messages that make them different. These objects are candidates for inheritance: a superclass can be created that accepts their common messages, and the ‘subclasses’ can `inherit’ these methods and add their own.

Page 15: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Polymorphism

• Designers may likewise find that there are several different ‘forms’ of messages or objects that perform the identical functions, using different message values.

• Object oriented programming languages provide features that permit these different forms (polymorphism.)

Page 16: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Checking an Object Design for Implementation

• Object role playing is one of the best methods for checking.

• Each object is assigned to an engineer to ‘play the part’ of that object.

• The objects are then `walked through’ a cases in the system, and each object player checks to make sure that the information is available for proper execution of the object’s responsibilities.

Page 17: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Object-oriented languages

• There are many object oriented languages today. Many of these are modern languages with extensions to support the object-oriented concepts: Classes, Methods, Inheritance, and Polymorphism.

• Still, thee of the major object oriented languages are still: Smalltalk, C++ and Java.

Page 18: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Smalltalk

• Smalltalk was advanced as a language originally by Alan Kay, who was working at the Xerox Palo Alto Research Center (Xerox PARC).

• Smalltalk was used as the fundamental language for programming the Xerox Star system, one of the early Object-oriented workstations.

Page 19: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

C++

• In the mid-1980s C++ was developed as the object-oriented extension to the C programming language.

• Bjarne Stroustrop was the major proponent of the C++ programming language.

• Another major contender at the time was ‘Objective C’ which was used by the “Next” system built by Steve Jobs company.

Page 20: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Java

• Java emerged from the “Oak” programming language at Sun Microsystems.

• Java was well suited to providing small programs that could be downloaded quickly (‘applets’). This proved to be very useful for internet applications, and Java quickly became the language of choice for Web browser applet programming.

Page 21: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Java

An exceedingly brief introduction

Page 22: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

It's like

• C++

• Smalltalk

Page 23: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

How is Java different?

• No preprocessor– No #define– No #ifdef

• Strongly typed

• No multiple inheritance

• No operator overloading

Page 24: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

How is Java different? (cont’d)

• Interpreted (Virtual Machine)

• Run-time error checking

• Garbage collection

• Standard class libraries

• No pointers, only references

• Scoping

Page 25: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Terminology

Classes vs. Objects

• A "Class" is a definition.

• An "Object" is the instance.

Methods

• In C++, called “member functions"

Page 26: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Inheritance

• Parent class defines common elements (data, methods)

• Child class defines ONLY elements unique to that specific class.

• Rule: Promote methods and data to the HIGHEST level of abstraction.

Page 27: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Public, Protected, and Private

• private is visible only to the class.

• protected is visible only to the class and child classes.

• "super" allows reference to parent elements, providing overrides.

Page 28: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Defining Objects

class Myclass {

<object content>

}

Page 29: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Data

class Myclass { public int mypublicdata;

private int myprivatedata;

protected int myprotecteddata;

}

Page 30: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Methods

class Myclass { public int mypublicdata;

private int myprivatedata;

protected int myprotecteddata;

public int get_myprivatedata { return myprivatedata;

};

}

Page 31: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Constructorsclass Myclass {

public int mypublicdata; private int myprivatedata; protected int myprotecteddata;

public int get_myprivatedata { return myprivatedata; };

Myclass(int myarg) { mypublicdata = myarg++; myprivatedata = myarg++; myprotecteddata = myarg;

}

}

Page 32: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Instatiating Objects

Myclass Myobject = new Myclass(arglist);

System.out.println

("My public data = " + Myobject.mypublicdata);

System.out.println

("My private data = " + Myobject.myprivatedata() );

Page 33: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Notation

Dot notation• Instance.item• Instance.item()

this and super• "this" insures self-reference.

• "super" insures parent reference.

Page 34: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Built-in types

• boolean (true or false)

• char (16-bit Unicode)

• byte (8-bits)

• short, int, long (integers)

• float, double (floating point)

Page 35: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Built-in objects and methods

Arrays• length()

Strings• Not mutable! (StringBuffer is!)• length(), indexOf(), lastindex()...

• Concatenation ("+")Exceptions• throw• catch

Page 36: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Libraries

java.lang - core language classes java.io - io package java.util - general utility java.text - date, I18N, L12N, sorting java.awt - abstract windows java.applet - applets java.beans - user composable code java.rmi - remote method invocation (distributed

processing)

Page 37: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Libraries (cont’d)

java.net - networking

java.math - math routines

java.sql - SQL (database)

java.security - encryption, authentication, signatures

Page 38: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Control

if-else switch (to define a “constant”:

public static final int SOMECONST = 1; )

while for while (and do-while) break (and labeled break: break somelabel ) continue return

Page 39: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

Inheritance

class Otherclass extends Myclass {public int get_myprivatedata() { return myprivatedata-1;}

}…Otherclass Oc = new Otherclass(4);System.out.println(“Otherclass private data = “ + Oc.get_myprivatedata();

}

Page 40: Object-oriented Design Chapter 16. Real Objects and Software Objects “Real” objects are things that can be manipulated and used for different tasks. So,

But consider…

class Otherclass extends Myclass {public int get_myprivatedata() { return super.get_myprivatedata()-1;}

}…Otherclass Oc = new Otherclass(4);System.out.println(“Otherclass private data = “ + Oc.get_myprivatedata();

}