首页 > 代码库 > 设计模式11-外观模式

设计模式11-外观模式

1. 概念

       为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

 

 

 2 .案例

  

/********************************************************************** * <pre> * FILE : Demo01.java * CLASS : Demo01 * * AUTHOR : Liaokailin * * FUNCTION : TODO * * *====================================================================== * CHANGE HISTORY LOG *---------------------------------------------------------------------- * MOD. NO.|   DATE   |   NAME  | REASON  | CHANGE REQ. *---------------------------------------------------------------------- *             |2014-3-6|Liaokailin| Created | * DESCRIPTION: * </pre> ***********************************************************************/package org.demo.facade.demo01;import java.text.SimpleDateFormat;import java.util.Date;interface ServiceA{    void methodA() ;}interface ServiceB{    void methodB() ;}interface ServiceC{    void methodC() ;}class ServiceAImpl implements ServiceA{    public void methodA() {        System.out.println("serviceA :methodA");    }}class ServiceBImpl implements ServiceB{    public void methodB() {        System.out.println("serviceB :methodB");    }}class ServiceCImpl implements ServiceC{    public void methodC() {        System.out.println("serviceC :methodC");    }}class Facade{    ServiceA sa ;    ServiceB sb ;    ServiceC sc ;    public Facade(){        sa = new ServiceAImpl() ;        sb = new ServiceBImpl() ;        sc = new ServiceCImpl() ;    }        public void methodA(){        sa.methodA() ;        sb.methodB() ;    }        public void methodB(){        sb.methodB() ;        sc.methodC() ;    }        public void methodC(){        sc.methodC() ;        sa.methodA() ;    }}public class Demo01 {     public static void main(String[] args) {        ServiceA sa = new ServiceAImpl() ;        ServiceB sb = new ServiceBImpl() ;        sa.methodA() ;        sb.methodB() ;        System.out.println("----");        System.out.println("facade");        Facade facade = new Facade()  ;        facade.methodA() ;        facade.methodB() ;        SimpleDateFormat s = new SimpleDateFormat() ;        s.format(new Date()) ;    }}