current position:Home>Java Design Patterns - Facade

Java Design Patterns - Facade

2022-09-23 09:32:13java lovers milk candy


前言

外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口.这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性.这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用.


一、外观模式(Facade)

 A complex process requires high-level interfaces and concrete implementation classes,The caller doesn't care about the implementation details,Only care about the result call and the result output.Subsystems can be split,Or a separate class to construct the facade pattern.

二、使用步骤

Simulate a shopping process,to pick items,添加购物车,选择快递方式,支付等.

1.An interface that defines a shopping process

代码如下:

/** * Define a shopping process */
public interface PurchurseProcess {
    

    //挑选商品
    void pickGoods(String goodName);

    //添加购物车
    void addCart(String goodName);

    //选择快递方式
    void chooseDelivMode(String delivName);

    //支付
    void pay();

}

接口实现类:

/** * The implementation class for the shopping process */
public class PurchurseProcessImpl implements PurchurseProcess{
    
    @Override
    public void pickGoods(String goodName) {
    
        System.out.println("挑选商品:"+goodName);
    }

    @Override
    public void addCart(String goodName) {
    
        System.out.println("将商品"+goodName+"添加购物车!");
    }

    @Override
    public void chooseDelivMode(String delivName) {
    
        System.out.println("选择快递方式"+delivName);
    }

    @Override
    public void pay() {
    
        System.out.println("进行支付....");
    }
}

简单的过程,Before using Appearance Mode,We need to call each step once.

/** * Simulate the process of placing a shopping order */
public class FacadeDemo {
    
    public static void main(String[] args) {
    
        PurchurseProcessImpl process = new PurchurseProcessImpl();
        //1.挑选物品
        process.pickGoods("联想电脑");
        //2.添加购物车
        process.addCart("联想电脑");
        //3.选择快递方式
        process.chooseDelivMode("京东快递");
        //4.支付完成
        process.pay();
    }
}

If every step is like this,步骤比较繁琐.Appearance mode can help with this.

2.Appearance mode for a makeover

代码如下
The interface class for import and purchase in the facade class,Provide a unified interface for shopping.

/** * 定义门面 */
public class PurchurseFacade {
    

    private PurchurseProcess purchurseProcess = new PurchurseProcessImpl();

    /** * A method implements four steps * @param goodName 商品名字 * @param deliveryMode 快递方式 */
   public void purchurseGoods(String goodName,String deliveryMode) {
    
       //1.挑选物品
       purchurseProcess.pickGoods(goodName);
       //2.添加购物车
       purchurseProcess.addCart(goodName);

       //3.选择快递方式
       purchurseProcess.chooseDelivMode(deliveryMode);

       //4.支付完成
       purchurseProcess.pay();
   }
}

This makes our call much simpler,调用代码如下:


public class Client {
    
    public static void main(String[] args) {
    
        PurchurseFacade purchurseFacade = new PurchurseFacade();
        //购买过程
        purchurseFacade.purchurseGoods("华为mate30","圆通快递");
    }
}

3. 思考

What if you add steps,选择支付方式,Choose a courier address,We also need to do step splitting,But for the caller it doesn't care about the steps,只关心结果.
支付方式:

/** * 选择支付方式 */
public class PayModeFacde {
    
    /** * 选择支付方式 * @param payMode */
    public void selectPayMode(String payMode){
    
        System.out.println("选择支付方式:"+payMode);
    }
}

快递地址:

/** * Added select email address */
public class DeliveryAddress {
    
    /** * Select a mailing address * @param address */
    public void addAddress(String address){
    
        System.out.println("Select a mailing address:"+address);
    }

}

The original class is transformed,Add additional functional steps:
//Add selection address private DeliveryAddress deliveryAddress = new DeliveryAddress(); //选择支付方式 private PayModeFacde payModeFacde = new PayModeFacde();

/** * 定义门面 */
public class PurchurseFacade {
    

    private PurchurseProcess purchurseProcess = new PurchurseProcessImpl();

    //Add selection address
    private  DeliveryAddress deliveryAddress = new DeliveryAddress();
    //选择支付方式
    private PayModeFacde payModeFacde = new PayModeFacde();

    /** * A method implements four steps * @param goodName 商品名字 * @param deliveryMode 快递方式 */
   public void purchurseGoods(String goodName,String deliveryMode) {
    
       //1.挑选物品
       purchurseProcess.pickGoods(goodName);
       //2.添加购物车
       purchurseProcess.addCart(goodName);
       //Add a courier address
       deliveryAddress.addAddress("xx省");
       //3.选择快递方式
       purchurseProcess.chooseDelivMode(deliveryMode);
       //增加
       payModeFacde.selectPayMode("支付宝");
       //4.支付完成
       purchurseProcess.pay();
   }
}

输出结果:

挑选商品:华为mate30
Huawei productsmate30添加购物车!
Select a mailing address:xx省
Select the express method Yuantong Express
选择支付方式:支付宝
进行支付....


总结

It can be seen that the appearance mode does not conform to the open-closed principle,The original interface code needs to be modified for transformation,比如日志输出 ,We don't care how logging is implemented,Only need to introduce log related classes.

copyright notice
author[java lovers milk candy],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/266/202209230925443610.html

Random recommended