🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.objective-c
Article #201

Re: method inheritance in NeXT ObjC classes

#201
From: Andy Belk
Date: Mon, 12 Oct 1992 10:56
97 lines
2848 bytes
Hi all.
        Just as an aside, this (List->Stack) is a fairly good example of
when subclassing is not necessarily what is really desired.

A Stack usually has push and pop operations, and in this case also a display
method, but one can argue that doing List operations on a stack may cause
problems. For example, one can do things that mess up the LIFO idea of a
stack:

Stack s;
Integer i = 5;
[s init];
[s push: i--];
[s push: i--];
[s push: i--];
[s push: i--];
[s push: i--];
[s removeObjectAt: 5];   // Ok, you can do it, but should you be able to ?
[[s pop] display];

etc.


Here's my version of Stack.h and Stack.m (yes, I did the ObjectiveC example
too, and I don't have Queue or Stack classes either - but I do have access to
a NeXT and its List class ...):

--------------------------- Stack.m -------------------------------

#import <objc/Object.h>
#import <objc/List.h>

@interface Stack : Object
{
  List *sList;
}

+ new;              // Probably should use init and alloc now
- free;
- get;              // returns last object on stack (and removes it)
- put: anObject;    // Store this object on the top of the stack
- (unsigned)size;   // current stack size

@end;

--------------------------- Stack.m -------------------------------

// Use a list with index to make a stack and a queue
// Modified 30th September to use a List rather than inheriting from one -
// Stacks should not look like lists, IMHO. This is an example of delegation
// as compared to inheritance.

#import <stdio.h>
#import "Stack.h"

@implementation Stack: Object

+ new
{
  self = [super new];  // It's VITALLY important to create an object
  sList = [List new];  // before playing with its instance variables !
  [sList init];
  return self;         // MUST return self here or instance will be wrong class
}

// The "-" in front of report means that it's an instance method,
// i.e., how a particular object should respond.

- free
{
  [sList free];         // Note the List object gets freed by me
  return [self free];
}

- put: anObject
{
  [sList addObject: anObject];
  return self;
}

- get
{
  id anObject = [sList lastObject];
  [sList removeLastObject];
  return anObject;
}

- (unsigned)size
{
  return [sList count];
}

@end

------------------------------------------------------------------------
-- 
					    ...   Anyd   ...
Technical Details
Message-ID: <1992Oct12.105651.17220@dxcern.cern.ch>
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!dxcern!belk
References: <7493@blue.cis.pitt.edu.UUCP> <DEMARCO.92Oct11123711@fsd.cpsc.ucalgary.ca>

Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.objective-c
Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!dxcern!belk
From: belk@dxcern.cern.ch (Andy Belk)
Subject: Re: method inheritance in NeXT ObjC classes
Message-ID: <1992Oct12.105651.17220@dxcern.cern.ch>
Organization: CERN European Lab for Particle Physics
References: <7493@blue.cis.pitt.edu.UUCP> <DEMARCO.92Oct11123711@fsd.cpsc.ucalgary.ca>
Date: Mon, 12 Oct 1992 10:56:51 GMT
Lines: 97