in reply to Class or Object composition ??

Inheritance is simpler and quicker but breaks pretty quickly when you want a "plugin" style application. Composition/delegation, where each piece can be implemented by a separate class if necessary and the implementation to use can be read from a config file or something, gives you a much more scalable plugin API. It requires some thinking up front though, and is overkill if you will not actually need to support plugins.

Middle ground? Not really. You could start with inheritance and split things out when you need to support plugins, I suppose. It depends on the way the app will be developed in the future, e.g. if you want CPAN authors to be able to add functionality without bugging you, a composition approach is much better.

Replies are listed 'Best First'.
Re^2: Class or Object composition ??
by bsb (Priest) on Oct 03, 2006 at 23:21 UTC
    Which modules on CPAN are good examples of the plugin style architecture?
    Template? DBIx::Class?
      There are not many examples of composition on CPAN because CPAN is mostly smaller pieces. DBIx::Class uses inheritance, modified by Class::C3. Template Toolkit has an excellent plugin API, but that's kind of separate from composition.

      However, if you look at the TT code, it does use composition internally. For example, a Template::Provider is a service for finding templates which the main Template object points to. You can implement the API and create a Template object that points to yours instead of the default one. It's a very good system for replacing bits and pieces.

      Another plugin system that worked out really well is Crypt::CBC, but again it's not so much composition as a specific plugin API.