说明
要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。最核心的目的:简化子系统,简化客户使用,屏蔽多个子系统
源码中的应用
flowable
ProcessEngineImpl
/** * 门面 */ public class ProcessEngineImpl implements ProcessEngine { private static final Logger LOGGER = LoggerFactory.getLogger(ProcessEngineImpl.class); protected String name; protected RepositoryService repositoryService; protected RuntimeService runtimeService; protected HistoryService historicDataService; protected IdentityService identityService; protected TaskService taskService; protected FormService formService; protected ManagementService managementService; protected DynamicBpmnService dynamicBpmnService; protected AsyncExecutor asyncExecutor; protected AsyncExecutor asyncHistoryExecutor; protected CommandExecutor commandExecutor; protected Map<Class<?>, SessionFactory> sessionFactories; protected TransactionContextFactory transactionContextFactory; protected ProcessEngineConfigurationImpl processEngineConfiguration; public ProcessEngineImpl(ProcessEngineConfigurationImpl processEngineConfiguration) { this.processEngineConfiguration = processEngineConfiguration; this.name = processEngineConfiguration.getEngineName(); this.repositoryService = processEngineConfiguration.getRepositoryService(); this.runtimeService = processEngineConfiguration.getRuntimeService(); this.historicDataService = processEngineConfiguration.getHistoryService(); this.identityService = processEngineConfiguration.getIdentityService(); this.taskService = processEngineConfiguration.getTaskService(); this.formService = processEngineConfiguration.getFormService(); this.managementService = processEngineConfiguration.getManagementService(); this.dynamicBpmnService = processEngineConfiguration.getDynamicBpmnService(); this.asyncExecutor = processEngineConfiguration.getAsyncExecutor(); this.asyncHistoryExecutor = processEngineConfiguration.getAsyncHistoryExecutor(); this.commandExecutor = processEngineConfiguration.getCommandExecutor(); this.sessionFactories = processEngineConfiguration.getSessionFactories(); this.transactionContextFactory = processEngineConfiguration.getTransactionContextFactory(); if (processEngineConfiguration.isUsingRelationalDatabase() && processEngineConfiguration.getDatabaseSchemaUpdate() != null) { this.commandExecutor.execute(processEngineConfiguration.getSchemaCommandConfig(), new SchemaOperationsProcessEngineBuild()); } if (this.name == null) { LOGGER.info("default ProcessEngine created"); } else { LOGGER.info("ProcessEngine {} created", this.name); } ProcessEngines.registerProcessEngine(this); if (processEngineConfiguration.getProcessEngineLifecycleListener() != null) { processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineBuilt(this); } processEngineConfiguration.getEventDispatcher().dispatchEvent(FlowableEventBuilder.createGlobalEvent(FlowableEngineEventType.ENGINE_CREATED)); if (this.asyncExecutor != null && this.asyncExecutor.isAutoActivate()) { this.asyncExecutor.start(); } if (this.asyncHistoryExecutor != null && this.asyncHistoryExecutor.isAutoActivate()) { this.asyncHistoryExecutor.start(); } } public void close() { ProcessEngines.unregister(this); if (this.asyncExecutor != null && this.asyncExecutor.isActive()) { this.asyncExecutor.shutdown(); } if (this.asyncHistoryExecutor != null && this.asyncHistoryExecutor.isActive()) { this.asyncHistoryExecutor.shutdown(); } Runnable closeRunnable = this.processEngineConfiguration.getProcessEngineCloseRunnable(); if (closeRunnable != null) { closeRunnable.run(); } if (this.processEngineConfiguration.getProcessEngineLifecycleListener() != null) { this.processEngineConfiguration.getProcessEngineLifecycleListener().onProcessEngineClosed(this); } this.processEngineConfiguration.getEventDispatcher().dispatchEvent(FlowableEventBuilder.createGlobalEvent(FlowableEngineEventType.ENGINE_CLOSED)); } public String getName() { return this.name; } public IdentityService getIdentityService() { return this.identityService; } public ManagementService getManagementService() { return this.managementService; } public TaskService getTaskService() { return this.taskService; } public HistoryService getHistoryService() { return this.historicDataService; } public RuntimeService getRuntimeService() { return this.runtimeService; } public RepositoryService getRepositoryService() { return this.repositoryService; } public FormService getFormService() { return this.formService; } public DynamicBpmnService getDynamicBpmnService() { return this.dynamicBpmnService; } public ProcessEngineConfigurationImpl getProcessEngineConfiguration() { return this.processEngineConfiguration; } }
例子
public class ModuleA { //示意方法 public void testA(){ System.out.println("调用ModuleA中的testA方法"); } } public class ModuleB { //示意方法 public void testB(){ System.out.println("调用ModuleB中的testB方法"); } } public class ModuleC { //示意方法 public void testC(){ System.out.println("调用ModuleC中的testC方法"); } }
public class Facade { //示意方法,满足客户端需要的功能 public void test(){ ModuleA a = new ModuleA(); a.testA(); ModuleB b = new ModuleB(); b.testB(); ModuleC c = new ModuleC(); c.testC(); } }
public class Facade { //示意方法,满足客户端需要的功能 public void test(){ ModuleA a = new ModuleA(); a.testA(); ModuleB b = new ModuleB(); b.testB(); ModuleC c = new ModuleC(); c.testC(); } }
public class Facade { //示意方法,满足客户端需要的功能 public void test(){ ModuleA a = new ModuleA(); a.testA(); ModuleB b = new ModuleB(); b.testB(); ModuleC c = new ModuleC(); c.testC(); } }