Inserting classes between other classes when loading packages

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Inserting classes between other classes when loading packages

Mikael Svane
I have developed a package which (among other classes) contains the
following hierarchy:

Model
    - MyModelSubclass
        - FirstClass
        - SecondClass
        - ThirdClass

Then I want to add extra behaviour to among other things the classes above.
This I would like to do in another package, since the first package can be
used without the extra behaviour. The behaviour should be added to
MyModelSubclass in the hierarchy above and it requires both new methods and
new instance variables. The most convenient way would be to insert a new
class (belonging to the second package) between Model and MyModelSubclass
with the new behaviour, but is this possible? If it isn't, is there another
solution except making the changes in MyModelSubclass and keeping everything
in a single package?

Regards,

Mikael Svane
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Inserting classes between other classes when loading packages

Ian Bartholomew-4
Mikael,

> new instance variables. The most convenient way would be to insert a new
> class (belonging to the second package) between Model and MyModelSubclass
> with the new behaviour, but is this possible? If it isn't, is there
> another solution except making the changes in MyModelSubclass and keeping
> everything in a single package?

The following appears to work but you should test it properly (in an
unwanted image) before using it as a proper solution.

Define a new class that fits between the Model and MyModelSubclass classes,
call it MyNewModelSubclass, and save it in it's own package.
Add the following line to the package's post install script.

MyModelSubclass setSuperclass: MyNewModelSubclass

After the package has been installed it will change the existing class
hierarchy to -

Model
    -- MyNewModelSubclass
        -- MyModelSubclass
            -- etc

NB
- An open class browser doesn't reflect the new hierarchy. You have to close
and open a new one.
- You can't uninstall, even if you put the classes back round the right way,
as the package prerequisites seem to get confused (may be fixable?)
- Using a MyNewModelSubclass class>>initialize method might be preferable to
using the package script.
- #setSuperclass: is private.

A better solution, IMHO, would be to have MyNewModelSubclass in the
hierarchy all the time. The version of MyNewModelSubclass included in the
basic package would just be a placeholder with no methods or instVars. The
working version of the class would be in a separate *.cls file which, when
filed in, would overwrite the dummy version of the class that was loaded
from the package.

Ian