#5: The Tragedy of Objectsby Richard Bartle Inheritance as a software tool gained its popularity as part of the Object-Oriented Programming (OOP) drive of the 1980s. OOP is great for large projects involving many programmers, because it allows whoever codes a class to define its functionality separate from that of the rest of the system, presenting only a set of interface routines for other classes to use for access. This stops programmers from stomping all over each others code, and makes modifying existing code easy. Well thats the theory. In practice, well, Ill talk about that shortly... OOP is a methodology in which data and code fragments are attached to objects that are instances of classes. Each object comes with a set of data members and member functions (or methods). A principle of good practice known as encapsulation states that access to the data members should only occur through the member functions, so therefore to all intents and purposes we can consider methods as the Big Thing as far as OOP is concerned. Methods are pieces of code defined for individual classes. They can be inherited by sub-classes, and will be attached to specific instances when their classes are created as objects in the program. How methods are implemented is unimportant; what matters is how they are called. In the Microsoft Foundation Class library, for example, to stop a particular window from being overlapped by any other a programmer could invoke Thats the good news, now the bad... FragilityOOP is design-critical. If you set about programming a system and your object tree is specified wrongly, correcting it can be a horrendous task. For example, a model of the human body using organs as objects, each with its own specific functionality, might do 95% of what you wanted it to do. Thats still only 95%, though. A model based on the central nervous system might do 100% of what you wanted. If you didnt find this out until late, however, then reprogramming all the organs code youve written as methods for nerve pathway objects would be... well lets just say youd be clenching your teeth awhile. OK, so back to MUDs. Because it comes with inheritance, you want to write a MUD using an object-oriented approach. Thats fine by me, theres nothing wrong with that at all. So what are your objects? Most MUD designers dont give this a moments thought. Why, the objects in the game, of course! It has treasure and swords and doors and boxes and rooms and mobiles and players those are all objects, its obvious. Well they are "objects", yes, but they dont have to be OOP objects. They get you 95% of what you can have, but not 100%. The fact that people call both concepts "objects" is the cause of a major dead-end in MUD programming. Alternative ObjectsLets say we want to implement a HIT command. If you hit a creature (i.e. a player or mobile) then a message is generated and a fight starts. That would be a simple method, invoked by Now what if hitting something other than a creature had a different effect? HIT HARDOBJECT (e.g. an anvil) might hurt you, HIT SOFTOBJECT (e.g. a cake) might damage what you hit, HIT DOOR might transmit a knocking sound to the other side and HIT BAG might generate some suggestion of whats inside it. OK, well we can write special methods for Then we have to add The problem is that although game objects are OOP objects, their methods arent. Were back to the old enumeration days again, when I was writing the same line of code over and over again saying that when the goat came across something it wanted to eat it would eat it. What we need here is to be able to quantify over methods, i.e. define a class HIT of which PUNCH, SLAP, THUMP etc. are instances. We cant, though, because functions arent OOP objects. Making them be OOP objects would entail a complete rewrite. Thats why Im telling you this: so you know the score before you start. CommandsThe real OOP objects in a MUD are the commands. It isnt a property of objects of class CREATURE that when you hit them a message is generated and a fight begins. Neither, incidentally, is it a property of objects of class HIT that if a CREATURE is the victim then that happens. Rather, it is a property of the command { HIT CREATURE } that theres a message and a fight. Its { HIT CREATURE } that has the method not CREATURE, not HIT. This simple observation completely opens up a MUDs design. You can use it for all kinds of things. You dont have to philosophize whether OPEN DOOR WITH KEY means Unfortunately, Im all out of space right now, so I cant give examples of how versatile this formalism is. That comes in the article to follow. Youre on the edge of your seat with expectation, I can tell... |