Difference between revisions of "Object Oriented Programming in Script/Object Oriented Programming"

From SCI Wiki
Jump to navigationJump to search
 
Line 18: Line 18:
 
 
 
 
  
xxxxxxx body xxxxxxxx
+
Object oriented programming (OOP) is a relatively new paradigm in programming.  The Smalltalk language, developed at Xerox PARC, was the first language to fully use the concept of OOP.  Since then, many languages (such as C++, Scheme, Objective C, and Actor) have incorporated the concepts to different degrees.  Because of the power of OOP for things such as simulations (and after all, what are adventure games?), Script was designed with the intention that much of the programming would use this concept. Those of us using the language over the last several months have found ourselves moving more and more away from its procedural aspects and toward the OOP aspects -- rooms, dialogs, menus, everything is now an object of one sort or another.
 +
 +
OOP languages basically turn the data/procedure relationship in conventional programming inside out: instead of sending data to a procedure for the procedure to act upon, we send a message to data (an object) telling it what to do.  This allows the sender of the message to obtain a service
 +
from the receiver object with no knowledge of how this service is performed, something which is not possible in procedural languages.
 +
 
 +
As an example, consider displaying the 'value' of data elements in a program in a procedural language and then an OOP language.  In this example, we'll assume that the procedural language is untyped (as opposed to the current direction of strong typing in C, Ada, Pascal, etc.).  Strong typing would make the follow exercise even harder.  To make the example more concrete, we'll imagine that we want to be able to display one of two data element types: a list of strings or an array of integers.
 +
 
 +
Procedurally, we'll use a C-like language (with lots of extensions which don't exist in C) and write
 +
 
 +
<blockquote>
 +
<div class="CodeBlockHeader">Code:</div>
 +
<syntaxhighlight lang="sci">
 +
Show(data)
 +
/* Here's one place C would have a problem -- data could either be
 +
* a list of strings or an array of integers.  What type does one
 +
* declare?
 +
*/
 +
{
 +
    switch (typeof(data)) {      /* how is typeof() implemented? */
 +
          case stringList:
 +
              for (str = ListStart(data) ;
 +
                    str != NULL ;
 +
                    str = ListNext(data)
 +
                  )
 +
                        printf(str);
 +
              break;
 +
          case integerArray:
 +
              for (i = 0 ; i < sizeof(data) ; ++i)
 +
                    printf("%d", data[i]);
 +
              break;
 +
          }
 +
}
 +
</syntaxhighlight>
 +
</blockquote>
 +
 
 +
Note that one could not easily write this in C because of strong data typing and the difficulty of implementing both typeof() and this particular use of sizeof().  What usually ends up happening in a procedural language is that instead of one general-purpose Show() procedure, one writes a lot of special purpose ShowStringList() and ShowIntegerArray() procedures and whoever wants to display something has to be aware of the type of data being displayed and call the appropriate procedure.
 +
 
 +
Contrast this with object-oriented programming.  Either type of data can be displayed by simply sending the data object the show message:
 +
 
 +
<blockquote>
 +
<div class="CodeBlockHeader">Code:</div>
 +
<syntaxhighlight lang="sci">
 +
(data show:)
 +
</syntaxhighlight>
 +
</blockquote>
 +
 
 +
Since both Arrays and Lists are sub-types of the Collection type they implement this in the same way (they actually share the code):
 +
 
 +
<blockquote>
 +
<div class="CodeBlockHeader">Code:</div>
 +
<syntaxhighlight lang="sci">
 +
(method (show)
 +
    (self eachElementDo: #show:)
 +
)
 +
</syntaxhighlight>
 +
</blockquote>
 +
 
 +
Both the list and the array simple send each of their elements the message requesting the element to display itself. The elements, strings and integers, implement the show method in different manners.  The show method for a string might be
 +
 
 +
<blockquote>
 +
<div class="CodeBlockHeader">Code:</div>
 +
<syntaxhighlight lang="sci">
 +
 
 +
(method (show)
 +
    (Print str)
 +
)
 +
</syntaxhighlight>
 +
</blockquote>
 +
 
 +
whereas that for the integer might be
 +
 
 +
<blockquote>
 +
<div class="CodeBlockHeader">Code:</div>
 +
<syntaxhighlight lang="sci">
 +
(method (show)
 +
    (Print "%d" number)
 +
)
 +
</syntaxhighlight>
 +
</blockquote>
 +
 
 +
Note how much simpler the OOP code is than the procedural code.  This is because the responsibility for displaying something is delegated to the object being displayed, freeing the calling code from knowing anything about it.  In fact, because of this passing on of responsibility, the above code will also handle lists which contain elements of several data types -- (data show:) would also work if data were a list which contained a string, an integer, a list of integers, an array of strings, and a list of arrays of lists of strings.  No change in code is necessary.
 +
 
 +
Another benefit of the OOP code is that adding a new data type is as easy as writing the show method for the data type.  In the procedural code, the Show() procedure would need re-writing, as would the typeof() and sizeof() procedures -- the addition of the new type propagates throughout the code rather than remaining isolated in the new type.
 +
 
 +
 
 +
Rather than continuing to give examples in an unspecified language, we will first define the terms to be used in discussions of OOP languages and then give the syntax and commands which Script uses.  Following that will be more examples of the use of OOP in adventure game coding.
  
 
&nbsp;
 
&nbsp;

Latest revision as of 00:56, 4 December 2015

Official SCI Documentation

Chapter: 1 | 2 | 3 | 4 | 5 | 6 | Index


Object Oriented Programming

Author: Jeff Stephenson

Date: 4 April 1988

 

Object oriented programming (OOP) is a relatively new paradigm in programming. The Smalltalk language, developed at Xerox PARC, was the first language to fully use the concept of OOP. Since then, many languages (such as C++, Scheme, Objective C, and Actor) have incorporated the concepts to different degrees. Because of the power of OOP for things such as simulations (and after all, what are adventure games?), Script was designed with the intention that much of the programming would use this concept. Those of us using the language over the last several months have found ourselves moving more and more away from its procedural aspects and toward the OOP aspects -- rooms, dialogs, menus, everything is now an object of one sort or another.

OOP languages basically turn the data/procedure relationship in conventional programming inside out: instead of sending data to a procedure for the procedure to act upon, we send a message to data (an object) telling it what to do. This allows the sender of the message to obtain a service from the receiver object with no knowledge of how this service is performed, something which is not possible in procedural languages.

As an example, consider displaying the 'value' of data elements in a program in a procedural language and then an OOP language. In this example, we'll assume that the procedural language is untyped (as opposed to the current direction of strong typing in C, Ada, Pascal, etc.). Strong typing would make the follow exercise even harder. To make the example more concrete, we'll imagine that we want to be able to display one of two data element types: a list of strings or an array of integers.

Procedurally, we'll use a C-like language (with lots of extensions which don't exist in C) and write

Code:
Show(data) 
/* Here's one place C would have a problem -- data could either be 
 * a list of strings or an array of integers.  What type does one 
 * declare? 
 */
{ 
     switch (typeof(data)) {       /* how is typeof() implemented? */ 
          case stringList: 
               for (str = ListStart(data) ; 
                    str != NULL ; 
                    str = ListNext(data) 
                   ) 
                         printf(str); 
               break; 
          case integerArray: 
               for (i = 0 ; i < sizeof(data) ; ++i) 
                    printf("%d", data[i]); 
               break; 
          }
}

Note that one could not easily write this in C because of strong data typing and the difficulty of implementing both typeof() and this particular use of sizeof(). What usually ends up happening in a procedural language is that instead of one general-purpose Show() procedure, one writes a lot of special purpose ShowStringList() and ShowIntegerArray() procedures and whoever wants to display something has to be aware of the type of data being displayed and call the appropriate procedure.

Contrast this with object-oriented programming. Either type of data can be displayed by simply sending the data object the show message:

Code:
(data show:)

Since both Arrays and Lists are sub-types of the Collection type they implement this in the same way (they actually share the code):

Code:
(method (show)
     (self eachElementDo: #show:)
)

Both the list and the array simple send each of their elements the message requesting the element to display itself. The elements, strings and integers, implement the show method in different manners. The show method for a string might be

Code:
(method (show)
     (Print str)
)

whereas that for the integer might be

Code:
(method (show)
     (Print "%d" number)
)

Note how much simpler the OOP code is than the procedural code. This is because the responsibility for displaying something is delegated to the object being displayed, freeing the calling code from knowing anything about it. In fact, because of this passing on of responsibility, the above code will also handle lists which contain elements of several data types -- (data show:) would also work if data were a list which contained a string, an integer, a list of integers, an array of strings, and a list of arrays of lists of strings. No change in code is necessary.

Another benefit of the OOP code is that adding a new data type is as easy as writing the show method for the data type. In the procedural code, the Show() procedure would need re-writing, as would the typeof() and sizeof() procedures -- the addition of the new type propagates throughout the code rather than remaining isolated in the new type.


Rather than continuing to give examples in an unspecified language, we will first define the terms to be used in discussions of OOP languages and then give the syntax and commands which Script uses. Following that will be more examples of the use of OOP in adventure game coding.

 

Notes


 

Table of Contents

 

< Previous: Table of Contents Next: Object Oriented Terminology >