concept inheritance in category design
appears as: inheritce

This is an excerpt from Manning's book Object Design Style Guide.
Using inheritance you can override the implementation of certain methods of a parent class. An interface can declare methods but leave their implementations entirely to a class that implements the interface.
What is wrong with the following code? class Preferences { private string preferencesFilePath; public function __construct(string preferencesFilePath) { this.preferencesFilePath = preferencesFilePath; } public function getPreference( string preference, bool defaultValue ): bool { preferences = this.loadPreferences(); if (isset(preferences[preference])) { return preferences[preference]; } return defaultValue; } protected function loadPreferences(): array { return json_decode( file_get_contents(preferencesFilePath) ); } } final class DatabaseTablePreferences extends Preferences { private Connection connection; public function __construct(Connection connection) { this.connection = connection; } protected function loadPreferences(): array { return this.connection.executeQuery( 'SELECT * FROM preferences' ).fetchAll(); } }
- DatabaseTablePreferences uses inheritance to change the behavior of a service.
- Preferences should extend DatabaseTablePreferences instead.
- Preferences should dispatch events to allow loading preferences from a different location.
- Loading the preferences should be delegated to a dedicated service with its own interface.
This is better, but it’s still not optimal. We may not have the downsides of inheritance anymore, but we don’t have the endless possibilities of using composition either.