1、实例

1.1、定义一个Service接口

public interface Service {

    void init(Configuration config);
    void start();
    void stop();
}

1.2、定义一个AbstractService抽象类

public abstract class AbstractService implements Service {

    private volatile Configuration config;

    @Override
    public void init(Configuration conf) {
        serviceInit(config);
    }

    @Override
    public void start() {
        serviceStart();
    }

    @Override
    public void stop() {
        serviceStop();
    }

    protected void serviceInit(Configuration conf) {
        if (conf != config) {
            setConfig(conf);
        }
    }

    protected void serviceStart() {

    }

    protected void serviceStop() {

    }

    protected void setConfig(Configuration conf) {
        this.config = conf;
    }
}

1.3、定义一个CompositeService组合Service

public class CompositeService extends AbstractService{

    private final List<Service> serviceList = new ArrayList<>();


    protected void serviceInit(Configuration conf) {
        List<Service> services = getServices();

        for (Service service : services) {
            service.init(conf);
        }
        super.serviceInit(conf);
    }

    protected void serviceStart() {
        List<Service> services = getServices();

        for (Service service : services) {
            service.start();
        }
        super.serviceStart();
    }

    protected void serviceStop() {
        super.serviceStop();
    }


    protected boolean addIfService(Object object) {
        if (object instanceof Service) {
            addService((Service) object);
            return true;
        } else {
            return false;
        }
    }

    public List<Service> getServices() {
        synchronized (serviceList) {
            return new ArrayList<Service>(serviceList);
        }
    }

    protected void addService(Service service) {
        synchronized (serviceList) {
            serviceList.add(service);
        }
    }
}

1.4、定义一个AsyncDispatcher组件

public class AsyncDispatcher extends AbstractService  {

    @Override
    protected void serviceInit(Configuration conf) {
        super.serviceInit(conf);
        System.out.println("AsyncDispatcher init...");
    }

    @Override
    protected void serviceStart() {
        super.serviceStart();
        System.out.println("AsyncDispatcher start...");
    }

    protected void dispatch(String event) {
        // 业务逻辑
    }
}

1.5、定义一个ResourceManager组件

public class ResourceManager extends CompositeService {

    public static void main(String[] args) {

        Configuration conf = new Configuration();

        ResourceManager resourceManager =  new ResourceManager();
        resourceManager.init(conf);
        resourceManager.start();
    }


    @Override
    protected void serviceInit(Configuration conf) {
        System.out.println("ResourceManager服务初始化...");

        AsyncDispatcher dispatcher = new AsyncDispatcher();
        addIfService(dispatcher);

        super.serviceInit(conf);
    }

    @Override
    protected void serviceStart() {
        System.out.println("ResourceManager服务启动...");
        super.serviceStart();
    }
}

1.6、运行结果如下 :

ResourceManager服务初始化...
AsyncDispatcher init...
ResourceManager服务启动...
AsyncDispatcher start...

2、图示

Service组合模式.png

3、总结

两个方向来说,就是说组件是一个普通的服务组件,还是组合服务组件。如果是普通的服务组件,比如说
AsyncDispatcher就是一个普通的服务组件,它继承了AbstractService,那其实就是直接重新自己的serviceInit和serviceStart方法即可,然后调用它的init和start方法就可以完成初始化和启动
当时针对ResourceManager这种组件,它里面是有其他子组件的,比如说AsyncDispatcher、RMApplicationHistoryWriter(当然,这个组件又是一个组件Service组件,那就说明它里面还有子service)。这种情况下其实就是在调用init、start的时候当然会调用本类重写的serviceInit或serviceStart方法,但是记住,本类重写的serviceInit还会调用super.serviceInit或super.serviceStart方法,这就走到CompositeService中对应的serviceInit、serviceStart。从而调用子组件的init、start方法


journey
32 声望20 粉丝

« 上一篇
YARN & MapReduce