swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: copy method


From: Scott Christley
Subject: Re: copy method
Date: Thu, 13 Feb 1997 11:04:13 -0800

At 10:53 AM 2/13/97 -0700, glen e. p. ropella wrote:
>Now, how can I use protocols for this?  I'd like the universe object
>to accept a "- (id) copy: (id) bug;" message.  But, from the
>programming perspective I'd like the bug to do the actual copy.  So
>the bug might accept a message specified as "- (id) copy;".  Can a
>protocol do this kind of mapping?  Or would it be necessary to create
>a method in the universe object that took the former message and
>reformulated it to send it to the bug argument?

You need to percieve @protocol as being very similar to @interface; meaning
a protocol just describes a set of methods that *may* be implemented by a class.

So in your case, no, a protocol by itself is not going to do this kind of
mapping; so you need to do the work that you mention at the end of the
paragraph.  You may want to create two protocols

@protocol (Copying)
- copy;
@end

@protocol (DelegateCopying)
- copyObject:(id)anObj;
@end

The "bug" (I'm not a swarm user so I'm just playing along) would implement
the Copying protocol; while the "universe" would implement DelegateCopying
protocol, so the user may

id bug;      /* this already exists */
id universe; /* this already exists */

// Do everything needed to create and handle a copy of the bug
[universe copyObject: bug];

The "univerese" implementation may look something like this:

- copyObject:(id)anObj
{
   id newObj = nil;

   // copy the object
   if ([anObj conformsToProtocol:@protocol(Copying)])
      newObj = [anObj copy];

   if (!newObj)
      return nil;
   else
      {
        // Do stuff to put new object into universe
      }
}

I'm guessing this is what you require; meaning when somebody creates a copy
of a "bug", you want to make sure that whatever is required in the
"universe" is done properly.

Of course, you can also define it the other way...

@protocol (Copying)
- copyWithDelegate:(id)anObj
@end

@protocol (DelegateCopying)
- (void)didPerformCopy:(id)anObj
@end

In this case you call the "bug" directly to do the copying, but pass the
universe along as the delegate.

id bug;      /* this already exists */
id universe; /* this already exists */

// Do everything needed to create and handle a copy of the bug
[bug copyWithDelegate: universe];

Then the "bug" implementation actually informs the delegate

- copyWithDelegate:(id)anObj
{
   id newObj;

   // do the copy
            
   // inform the delegate
   if ([anObj conformsToProtocol:@protocol(DelegateCopying)])
      [anObj didPerformCopy: newObj];
}

Then the "universe" can do whatever is necessary in its didPerformCopy:
method with the new object.

I prefer the first technique better because then each individual "bug"
doesn't need to know that it must inform a delegate; somebody may implement
a "bug" and forgot to add that code.  Better that you always call the
"universe" to do the copying so that it make sure the objects are setup
properly.

I hope I've explained this clearly enough.

Scott



reply via email to

[Prev in Thread] Current Thread [Next in Thread]