home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ACTORTUT.ZIP / TUTR1.TXT next >
Text File  |  1990-10-19  |  19KB  |  400 lines

  1. Introduction to Object-Oriented Programming
  2. by Bruce Newburger, Zack Urlocker
  3. Copyright(c) 1989 The Whitewater Group, Inc.
  4.  
  5. Overview
  6. --------
  7. Object-oriented programming (OOP) represents a new, more
  8. productive approach to solving problems.  Rather than working
  9. with traditional procedures and separate data structures, we
  10. create objects which encompass both the data and the
  11. operations.  Object-oriented programming languages reduce the
  12. amount of code that needs to be written and maintained by
  13. enabling the programmer to create classes of reusable objects
  14. which encapsulate behavior and ensure data abstraction.
  15.  
  16. Object-oriented programming is easy to learn since it is based
  17. on a few simple yet powerful concepts.  Many programmers find
  18. object-oriented programming to be a natural extension to how
  19. they think.
  20.  
  21. We will define objects, classes, messages and inheritance using
  22. understandable non-programming examples.  We will often refer
  23. to the Actor(R) language for programming examples of object-oriented
  24. concepts.  Actor is an interactive object-oriented language for the
  25. development of Microsoft Windows(tm) applications.
  26.  
  27. History of OOP
  28. --------------
  29. Object-oriented programming was first introduced in the Simula
  30. language in 1967.  Smalltalk, developed at Xerox Palo Alto
  31. Research Center (PARC), later refined the concepts and became
  32. the first pure object-oriented language.  Since then, many
  33. other object-oriented languages have emerged including Actor,
  34. C preprocessors such as C++ and Objective-C, and extensions to
  35. Lisp such as Flavors and Scheme.
  36.  
  37. In order for a language to be considered object-oriented it
  38. must meet three criteria:
  39.  
  40. 1] encapsulation of data and instructions into units of
  41.    functionality called objects
  42. 2] inheritance of functionality through a class hierarchy
  43. 3] dynamic runtime binding of messages sent to objects
  44.  
  45. Languages such as Ada and Modula-2, which are sometimes called
  46. object-oriented, meet the first criteria but fail the second
  47. and third.
  48.  
  49. Pure object-oriented languages such as Actor meet all three
  50. criteria and have the advantage of a consistent world in which
  51. everything is an object and all operations take place by
  52. sending messages to objects.  This makes learning the language
  53. and debugging programs easier.
  54.  
  55. Hybrid languages such as C++ add objects to standard C,
  56. providing the mixed blessing of being able to freely mix
  57. object-oriented C with traditional C.  This compromises some of
  58. the power of object-oriented programming.  For example, classes
  59. are not objects and dynamic binding is limited due to the
  60. constraints of C.
  61.  
  62. When selecting an object-oriented language you should consider
  63. factors such as:
  64.  
  65. 1] ease of learning
  66. 2] predefined classes
  67. 3] programming environment and tools
  68. 4] ease of debugging
  69. 5] ability to rapidly prototype and test code
  70. 6] execution speed
  71. 7] memory management
  72. 8] access to existing libraries
  73. 9] ability to create a standalone, distributable application
  74.  
  75. One of the goals of developing Actor was to provide a rich
  76. class library and interactive environment that improved
  77. programmer productivity without compromising efficiency.
  78.  
  79.  
  80. Object-oriented Programming
  81. ---------------------------
  82. The basis of object-oriented programming is the creation and
  83. management of objects.  An object is a programming entity that
  84. is designed to closely resemble real-life objects.  An object
  85. has attributes and responds to instructions.  For example, in a
  86. check-writing program, a check object has these attributes:
  87. amount, check number, date, to whom, and comment.  It responds
  88. to these instructions: write, cash, sign, record.
  89.  
  90. The advantage of breaking down a problem in terms of objects is
  91. the way in which we represent data.  In the check-writing
  92. problem above, the data is the amount, the check number and so
  93. on.  The rest of the program can access the data only through
  94. the instructions like write and cash.  In effect, we have
  95. encapsulated access to the data since a check object is both
  96. the data and the instructions that act upon it.
  97.  
  98. In Actor, many entities that you would normally think of as
  99. data structures are actually objects.  For example, integers,
  100. characters, arrays, strings and files are all objects.  There
  101. are also objects corresponding to more sophisticated data
  102. structures such as stacks, queues, look up tables, windows,
  103. dialog boxes and so on.
  104.  
  105. A non-programming example of an object as a functional entity
  106. is a car.  A car object has attributes such as the
  107. transmission, year and color.  In addition, the car object
  108. responds to certain instructions, such as go (step on the gas),
  109. stop (step on the brake), left (turn the wheel left) and right
  110. (turn the wheel right).  When you step on the gas, you need not
  111. be concerned with the internal workings of the engine.  All
  112. cars are driven the same way, regardless of the particular make
  113. or model.  The ability to manipulate an object's data without
  114. knowledge of the data's internal format is called data
  115. abstraction.
  116.  
  117. Objects can be made up of primitive data, indexed data or named
  118. data.  Primitive data is embedded directly in the structure of
  119. objects such as numbers, characters and symbols so that access
  120. is as efficient as possible.  Indexed data is used in objects
  121. such as arrays or strings where there are several items that
  122. make up an object.  We can refer to indexed data in an object x
  123. via the familiar array notation x[i].  Objects can also be made
  124. up of named instance variables, which refer to other objects.
  125. For example a car object myCar will have instance variables
  126. paint, year and transmission.   We can refer to the instance
  127. variables via dot notation such as myCar.year, although
  128. generally we will want to encapsulate access to private data.
  129.  
  130. To review, an object is the basic building block of an object-
  131. oriented programming system.  Objects provide a simple means to
  132. represent the entities of a real world problem in terms of a
  133. programming entity.  An object has instance variables that
  134. describe its data, and instructions that define operations to
  135. be performed on the object.  In general, objects may have named
  136. instance variables, indexed variables, both or neither,
  137. depending on what is required.  The instructions are part of
  138. the object and are tailored to the object's internal
  139. representation.
  140.  
  141.  
  142. Objects are Grouped in Classes
  143. ------------------------------
  144. In object-oriented programming, we call a type a class.  Each
  145. object belongs to one and only one class.  We say that an
  146. object is an instance of the class it belongs to.  There can be
  147. many different classes; Actor includes over a hundred
  148. predefined classes that you can use in your programs.  Think of
  149. a class as an object template, or an object factory.  Every
  150. object of a given class has the same data format and responds
  151. to the same instructions.  In the car example, we might have a
  152. class called Ford.  Ford is a class of car, and all Ford cars
  153. belong to the class.
  154.  
  155. There is no limit to the number of objects you can create of a
  156. given class.  The instructions that an object can respond to
  157. are managed by the class.  The data associated with a
  158. particular object is managed by the object itself.  For
  159. example, you might have a red Ford Cortina and a blue Ford
  160. Mustang.
  161.  
  162. Thus, the objects you use in your programs come from classes.
  163. You can use any of the predefined classes that are loaded in
  164. the system.  You can also create your own classes.
  165.  
  166.  
  167. Objects Respond to Messages
  168. ---------------------------
  169. Objects perform operations in response to messages.  For
  170. example, when we press on the brake we send a stop message to a
  171. car object.  The car object's brake system is authorized to
  172. handle the stop message because it consists of specialized
  173. parts like brake pads and hydraulics.  By following a set of
  174. conventions, or protocol, the Actor programmer is protected
  175. from unauthorized data manipulation.  Too often, procedural
  176. programmers put their hands right to the wheel and get burned.
  177.  
  178. A message is different than a subroutine call in that different
  179. objects can respond to the same message in different ways.  For
  180. example, cars, motorcycles and bicycles will all respond to a
  181. stop message, but the operations they perform are object-
  182. specific.
  183.  
  184. Examples of messages in Actor are shown below.
  185.  
  186.    printLine(x);         /* whatever x is, tell it to print */
  187.    draw(item, hDC);      /* tell the item to draw itself */
  188.    close(device);        /* send a close message to device */
  189.    y := 5*7              /* tell 7 to multiply by 5 */
  190.  
  191. In the examples above the receiver of the message is the first
  192. parameter in parenthesese; arguments may follow the receiver.
  193. Note that the message makes no assumptions about the class of
  194. the receiver or of the arguments; they are simply objects.  It
  195. is the receiver's responsibility to respond to a message in an
  196. appropriate manner.  This gives us a great deal of flexibility
  197. since different objects can respond to the same message
  198. differently.  This is known as polymorphism, literally meaning
  199. "many behaviors."
  200.  
  201. Polymorphism allows us to write generic reusable code more
  202. easily, since we can specify general instructions and delegate
  203. the implementation details to the objects that are involved.
  204. Since there are no assumptions about the classes of objects
  205. involved, there are fewer dependencies in the code and
  206. maintenance is easier.
  207.  
  208. Objects respond to messages according to methods that have been
  209. defined in their class.  For example if a Cube object receives
  210. a draw message, then the class Cube (or one of its ancestors)
  211. must define a draw method.  A method definition corresponds to
  212. a procedure definition in other languages.  A sample method
  213. definition in Actor is shown below.
  214.  
  215.    /* This method defines how a Cube object will respond to
  216.       a draw message.  The argument hDC is a handle to a
  217.       Windows display context where the drawing takes place.
  218.  
  219.       A cube object is made up of two rectangles called front
  220.       and rear.  A cube is drawn by drawing the front and rear
  221.       faces and then connecting them.
  222.  
  223.       self refers to the cube that received the draw message.
  224.    */
  225.    Def draw(self, hDC)
  226.    {
  227.     draw(front, hDC);/* tell the front to draw */
  228.     draw(rear, hDC); /* tell the rear to draw */
  229.     drawConnections(self, hDC);  /* connect the faces */
  230.    }
  231.  
  232.  
  233. Class Hierarchy
  234. ---------------
  235. An object-oriented system has the responsibility of organizing
  236. and managing the one-hundred or more classes that your programs
  237. will use.  It does this by organizing classes into a hierarchy.
  238. At the top of the hierarchy is the most general classes and at
  239. the bottom are the most specific classes.
  240.  
  241. For example, in the car example, Ford is a class that defines
  242. what Ford car objects are.  But there are classes of Ford car
  243. objects that are more specialized Fords, such as Taurus,
  244. Escort, and Thunderbird.  These classes define Fords in a much
  245. more specialized manner than does the Ford class itself.  So we
  246. say that the classes Taurus, Escort and Thunderbird descend
  247. from class Ford and the Ford class is their ancestor class.  In
  248. some object-oriented languages the terms superclass and
  249. subclass or base class and derived class are used instead of
  250. ancestor and descendant.
  251.  
  252. But there can be classes more general than Ford.  For example,
  253. Ford might descend from class Car, which would, in turn,
  254. descend from class Vehicle, and so on.  Class Car defines how a
  255. car behaves, class Ford defines how Ford cars behave in
  256. addition to cars in general, and class Escort defines how
  257. Escorts behave in addition to Ford cars in general.
  258.  
  259. Of course, if all we wanted was a Ford Escort object, we would
  260. write only one class, called Escort.  This class would define
  261. exactly how a Ford Escort car operates.  This methodology is
  262. limiting because if we decide later to create a Ford Taurus
  263. object we will have to duplicate most of the code which
  264. describes first how a vehicle behaves, then how a car behaves,
  265. and then how a Ford behaves.  This is what is done in a
  266. procedural language.  An object-oriented language eliminates
  267. duplicated effort by allowing classes to share behaviors.
  268.  
  269. You might find it strange to define a Car class.  After all,
  270. what is an instance of the Car class.  We have no such thing as
  271. a generic car; all cars must be of some make and model.  In the
  272. same way, we have no instances of the Ford class.  All Fords
  273. must belong to one of the subclasses Escort, Taurus or
  274. Thunderbird.  We call classes like Car and Ford formal classes.
  275. We never create instances of formal classes.  Formal classes
  276. are used to share common behavior among classes.
  277.  
  278. A real Actor example of formal classes is the part of the Actor
  279. class tree that defines various collections including arrays,
  280. sets, strings, dictionaries and so on.  The formal class
  281. Collection defines the common behavior among the classes and
  282. has descendant classes Set, Bag, IndexedCollection and
  283. KeyedCollection.  IndexedCollection is a formal class that
  284. defines the common behavior among collections that use integer
  285. indices.  It has descendant classes such as Array, String,
  286. Struct and OrderedCollection.  KeyedCollection is a formal
  287. class that allows arbitrary keyed access and has descendant
  288. classes such as Dictionary, Frame and Slot.
  289.  
  290.  
  291. Inheritance
  292. -----------
  293. Inheritance is a mechanism for sharing behaviors between
  294. classes.  The behavior of a class's instances is defined in
  295. that classes methods.  But a class also inherits the behavior
  296. of all of its ancestors.
  297.  
  298. For example, the class Car defines how cars in general behave.
  299. The class Ford inherits the general car behavior from class
  300. Car, and adds behavior that is specific to Ford cars.  But the
  301. Ford class does not have to redefine the car behavior.  Next,
  302. the Escort class inherits behavior of cars from class Car, and
  303. behavior of Fords from class Ford.  It adds the behavior
  304. specific only to Escorts.
  305.  
  306. For example, assume that all Fords use the same braking system.
  307. Then the method brake would be defined in class Ford.  When we
  308. step on the brake pedal of an Escort, a stop message is sent.
  309. However, Escort does not define a stop method, and the search
  310. continues in class Escort's direct ancestor, the class Ford.
  311. The stop method of class Ford is then invoked.
  312.  
  313. In a similar way, Escort can inherit behaviors from the Car and
  314. Vehicle classes.  The behaviors of any given class is really an
  315. amalgamation of the behaviors of all of its ancestors.  This
  316. straightforward process of inheritance prevents you from having
  317. to re-invent the wheel, or the brakes, for that matter.
  318.  
  319. Let's say that most Ford cars use the same braking system, but
  320. the Thunderbird has its own, anti-lock braking system.  In this
  321. case, the Thunderbird class would redefine the stop method.
  322.  
  323. Thus the brake method of the Ford class would never be invoked
  324. by Thunderbirds.  However, its existence higher up in the class
  325. hierarchy does not cause a conflict.  Other Ford cars would
  326. continue to use the standard braking system.
  327.  
  328.  
  329. Single Inheritance vs. Multiple Inheritance
  330. -------------------------------------------
  331. Actor, like most object-oriented languages, operates according
  332. to single inheritance.  That means that the system searches for
  333. methods directly up the class hierarchy, first in the class of
  334. the instance, then in that class's direct ancestor, and so on
  335. up the line.  Each class directly descends from only one
  336. ancestor class.
  337.  
  338. In a scheme called multiple inheritance, used in some object-
  339. oriented versions of Lisp, a class can descend from many
  340. unrelated classes.  Multiple inheritance can make it easy to
  341. combine behaviors of several classes at the expense of having
  342. to specify how conflicts are resolved.
  343.  
  344. Although there are some limitations to single inheritance, you
  345. can often achieve the benefits of combined behavior by adding
  346. instance variables of other classes.  This is not as general a
  347. solution as multiple inheritance, but it avoids some of the
  348. complexity of conflict resolution.
  349.  
  350.  
  351. Encapsulation
  352. -------------
  353. Encapsulation is a design goal of object-oriented programming.
  354. It is achieved by allowing access to data only through its own
  355. methods.  This ensures that instructions are operating on the
  356. proper data.  No other parts of the program can operate
  357. directly on another object's data.  An object's internal format
  358. is insulated from other objects.  An important factor in
  359. achieving encapsulation is designing different classes of
  360. objects that operate using a common protocol.  That means many
  361. objects will respond to the same messages but will implement
  362. their own methods.  That way, your program can send a generic
  363. message and leave the implementation up to the receiving
  364. object.  This reduces interdependencies, and increases
  365. interchangability and reusability.
  366.  
  367. If we bring to mind the car example, we can think of a car's
  368. engine as being encapsulated.  Although engines may differ in
  369. implementation, they all interface with the driver through a
  370. common protocol: step on the gas for more power, and let up on
  371. the gas for less power.  Since all drivers know this protocol,
  372. all drivers know how to drive all cars.  It doesn't matter what
  373. engine is in the car, that detail is insulated from the rest of
  374. the car and the driver.  This makes cars more easily
  375. interchangeable and maintainable.
  376.  
  377. Data abstraction is the result of good object-oriented code
  378. that takes advantage of encapsulation and polymorphism.  Data
  379. is abstracted when it is shielded by a full set of methods.
  380. Only a class's methods utilize the details of the class's data
  381. format or contents.
  382.  
  383.  
  384. Summary
  385. -------
  386. Object-oriented programming is a significant departure from
  387. procedural programming.  Rather than treat data and procedures
  388. separately, they are closely linked into what are known as
  389. objects.  The main advantage of object-oriented programming is
  390. the ability to reuse code and develop more maintainable systems
  391. in a shorter amount of time.  This is achieved by enabling the
  392. programmer to create reusable classes that inherit the behavior
  393. of ancestor classes.  Good object-oriented programming makes
  394. use of encapsulation and polymorphism to provide abstract data
  395. types.
  396.  
  397. Actor is a registered trademark and The Whitewater Group is a registered
  398. service mark of The Whitewater Group, Inc.  Other product names may be
  399. trademarks of their respective owners.