外观模式
本质: 封装交互,简化调用
何时使用外观模式
相关模式
外观模式经典代码:
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: ServiceA* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:05* @version: 1.0*/
public interface ServiceA {void methodA();
}
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: ServiceAImpl* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:06* @version: 1.0*/
public class ServiceAImpl implements ServiceA{@Overridepublic void methodA() {System.out.println("这是服务A");}
}
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: ServiceB* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:05* @version: 1.0*/
public interface ServiceB {void methodB();
}
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: ServiceBImpl* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:06* @version: 1.0*/
public class ServiceBImpl implements ServiceB{@Overridepublic void methodB() {System.out.println("这是服务B");}
}
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: ServiceC* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:05* @version: 1.0*/
public interface ServiceC {void methodC();
}
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: ServiceCImpl* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:06* @version: 1.0*/
public class ServiceCImpl implements ServiceC{@Overridepublic void methodC() {System.out.println("这是服务C");}
}
门面类:将交互封装起来,简化使用者的调用
package com.tao.design.facade;/*** @projectName: DesignMode* @package: com.tao.design.facade* @className: Facade* @author: TT_Hugo* @description: TODO* @date: 2022/12/26 22:08* @version: 1.0*/
public 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();}
}
测试类
package com.tao.design.facade;/*** @Author Mi_Tao* @Date 2023/7/23* @Description* @Version 1.0**/
public class Client {public static void main(String[] args) {new Facade().methodA();}
}