Owner of method (owner of design)
The technique of the owner of method ( Template method pattern ) is a Patron of design (design pattern) behavioral used in software Génie.
An owner of method defines the skeleton of a algorithm using abstract operations whose concrete behavior will be in the Sous-classe S, which will implement these operations.
This technique, very widespread in the abstract classes, allows:
- To clearly fix standard behaviors which should be divided by all the subclasses, even when the detail of the under-operations differs.
- Factoriser code which would be redundant if it were repeated in each subclass.
The technique of the owner of method has this of private individual that it is the method of the class relative which calls operations existing only in the subclasses. It is a current practice in the abstract classes, whereas usually in a hierarchy of concrete classes it is the opposite: they are rather the methods of the subclasses which call the methods of the super-class like piece of their own behavior.
The implementation of an owner of method is sometimes called method base because it firmly anchors a behavior which applies then to all the hierarchy of classes per heritage. To make sure that this behavior will not be redefined arbitrarily in the subclasses, one states the method final base in nonvirtual Java, or in C++.
Methods being used as " bricks of comportement" with the method base should be declared abstract in Java, or virtual pure in C++.
Example in Java
One can now derive this class to establish various plays:
The technique of the owner of method fixes a framework for all the subclasses. That implies certain restrictions: in the example above, one cannot make inherit a class JeuDuTarot the abstract class JeuDeSociété, because in part of Tarot, the order of the players is not linear: it depends on the player who has just collected the fold.
One can decide not to declare the method final base like in Java (or to decide to declare it virtual in C++), in order to make it more flexible. Thus the class JeuDuTarot could inherit the class JeuDeSociété perfectly, on the condition of redefining the method jouerUnePartie () to take account of the rules of the Tarot. But this practice is criticable. It is important to raise the question as of the writing of the super-class: will the subclasses have the right to redefine the fundamental behaviors coded in the super-class? . The advantage is of course an increased flexibility. The disadvantage can be the loss of the internal coherence of the object, if the overload of the methods bases is badly designed. To take again the preceding example, one could set up a method which turns over the next player, who would be implemented differently in the JeuDuTarot class and a class of a play where each player plays successively.
See too
Associated owners
| Random links: | The Mussy-on-Seine | Gnowee | Joseph Campanella | Oleg Luzhny | Occult forces | Aphasiology |