词条 | Servant (design pattern) |
释义 |
}} In software engineering, the servant pattern defines an object used to offer some functionality to a group of classes without defining that functionality in each of them. A Servant is a class whose instance (or even just class) provides methods that take care of a desired service, while objects for which (or with whom) the servant does something, are taken as parameters. Description and simple exampleServant is used for providing some behavior to a group of classes. Instead of defining that behavior in each class - or when we cannot factor out this behavior in the common parent class - it is defined once in the Servant. For example: we have a few classes representing geometric objects (rectangle, ellipse, and triangle). We can draw these objects on some canvas. When we need to provide a “move” method for these objects we could implement this method in each class, or we can define an interface they implement and then offer the “move” functionality in a servant. An interface is defined to ensure that serviced classes have methods, that servant needs to provide desired behavior. If we continue in our example, we define an Interface “Movable” specifying that every class implementing this interface needs to implement method “getPosition” and “setPosition”. The first method gets the position of an object on a canvas and second one sets the position of an object and draws it on a canvas. Then we define a servant class “MoveServant”, which has two methods “moveTo(Movable movedObject, Position where)” and moveBy(Movable movedObject, int dx, int dy). The Servant class can now be used to move every object which implements the Movable. Thus the “moving” code appears in only one class which respects the “Separation of Concerns” rule. Two ways of implementationThere are two ways to implement this design pattern.
How to implement Servant
ExampleThis simple example shows the situation described above. This example is only illustrative and will not offer any actual drawing of geometric objects, nor specification of what they look like. Similar design pattern: CommandDesign patterns Command and Servant are very similar and implementations of them are often virtually the same. The difference between them is the approach to the problem.
Even though design patterns Command and Servant are similar it doesn’t mean it’s always like that. There are a number of situations where use of design pattern Command doesn’t relate to the design pattern Servant. In these situations we usually need to pass to called methods just a reference to another method, which it will need in accomplishing its goal. Since we can’t pass references to methods in many languages, we have to pass an object implementing an interface which declares the signature of passed method. See also
Resources{{cite conference| url = http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf | title = Let’s Modify the Objects First Approach into Design Patterns First | first = Rudolf | last = Pecinovský | author2=Jarmila Pavlíčková |author3=Luboš Pavlíček | date=June 2006 | conference = Eleventh Annual Conference on Innovation and Technology in Computer Science Education, University of Bologna | conferenceurl = http://www.iticse06.cs.unibo.it/ }}{{Design Patterns Patterns}} 2 : Software design patterns|Articles with example Java code |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。