Defining a Singleton Object Type in Interface Style
Defining a singleton object type is a special case of defining an object type. The difference is that a singleton object type does not require a provider. Alternatively, we might say that its unique instance is its own provider.
Because a singleton is its own provider, the SingletonTeaObject interface subsumes the other provider methods (namely, getTypeName, getTypeDescription and getConcept).
Because it is a singleton, it needs no getInstance method.
- Procedure
- Define a class that models the managed object type. Your class must implement the interface
SingletonTeaObject.
For example, when modeling a car, we could define a singleton class to represent the engine.
public class CarEngine implements SingletonTeaObject { public String getName() { // Return the name of the engine instance; return "Engine"; } public String getDescription() { // Return the description of the engine instance; return "Car Engine"; } public String getKey() { // Return the key that corresponds to the engine instance; // for example, "Engine" } // These methods give the singleton functionality // that would otherwise be in the provider. public String getTypeName() { // Return the name of the engine object type. return "Engine"; } public String getTypeDescription() { // Return the description of the engine object type. return "Car Engine"; } public TeaConcept getConcept() { // Return the concept type of the driver. // In our model, it's a process that steers the car. return TeaConcept.RESOURCE; } } - Register the singleton instance of the class.
CarAgentServer server = ... // Register other instances, including the top-level object type. CarEngine myCarEngine = new CarEngine;server.registerInstance(myCarEngine); ... server.start();
Notice that instead of registering the provider, we register the unique instance of the engine class.