Purpose:
1. The system divides services according to the functional or business logic layers, and the system architecture is clear and flexible;
2. provide automatic registration of services;
3. Simple service calls. calls between services can be directly obtained through getxxxservice;
4. Unified service management;
5. service monitoring;
I. Service Interface
The service interface provides an automatic registration mechanism. Implements the registe () method to complete service registration.
public interface Service {public void registe();}
Ii. abstractservice abstract class
The abstractservice abstract class implements a registe (string, object) method and registers the service through servicemanager.
Servicemanager is responsible for saving all services. It also provides service registration and acquisition services, which is the only channel for service registration and acquisition.
The registe () method without parameters is called in abstractservice, which is defined in service and not implemented in abstractservice.
It is required to inherit the subclass Implementation of abstractservice and call the registe (string, object) method to register itself.
public abstract class AbstractService implements Service {public AbstractService() {registe();}protected void registe(String serviceName, Object service) {ServiceManager.registe(serviceName, service);}public Object getService(String serviceName) {return ServiceManager.getService(serviceName);}}
Iii. Example userservice
Userservice inherits abstractservcie. Because abstractservcie does not implement the registe () method, it must be implemented by userservice.
Userservice implements the registe () method, calls the registe (string, object) method provided by abstractservcie In the method, and passes in the servicename and this of the current service to complete registration.
The subclass that inherits abstractservcie can directly use getservice (string servicename) to obtain any required services.
public abstract class AbstractService implements Service {public AbstractService() {registe();}protected void registe(String serviceName, Object service) {ServiceManager.registe(serviceName, service);}public Object getService(String serviceName) {return ServiceManager.getService(serviceName);}}
4. servicemanager
Stores the service and provides methods for registering and obtaining the service. It also provides a service monitoring mechanism.
Servicemanager is also the facade of all service calls and is an application in the facade mode.
Public final class servicemanager {Private Static Map <string, Object> services = new hashmap <string, Object> (); Private Static servicemonitor = NULL; /*** servicemonitor can manage the services variable ** @ return */public static servicemonitor getservicemonitor () {If (servicemonitor = NULL) {servicemonitor = new servicemonitor (services );} return servicemonitor;}/*** register the service to servicemanager. ** This method is used by the abstractservice class. The subclass of abstractservice must implement the registe () * method and call the registe (string servicename, Object Service) method of abstractservice in the method. ** Register the subclass to the current servicemanager. ** @ Param servicename *, for example, servicemanager. class. getname () * @ Param Service */public static void registe (string servicename, Object Service) {services. put (servicename, service);}/*** get service by service name, which is used by the abstractservice class. ** @ Param servicename *, for example, servicemanager. class. getname () * @ return */public static object getservice (string servicename) {object service = services. get (servicename); If (service = NULL) {try {class <?> Serviceclass = Class. forname (servicename); service = serviceclass. newinstance ();} catch (classnotfoundexception e) {e. printstacktrace ();} catch (instantiationexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace () ;}} return service ;}}
5. servicemonitor
Services in servicemanager are private variables, and servicemanager is only responsible for saving, registering, and obtaining services. Therefore, servicemonitor is provided to implement (MONITOR) Services variables ).
public class ServiceMonitor {private Map<String, Object> services = null;public ServiceMonitor(Map<String, Object> monitedServices) {this.services = monitedServices;}public int getServiceCount() {return this.services.size();}}