设计模式-门面模式

设计模式-门面模式

参考文档

设计模式之禅

流程图

设计模式-门面模式

类图

image-20220619140934207

举例生活中举例的案例

写信步骤

1.写信的内容

2.写信封

3.把信放到信封

4.邮递

定义写信接口

LetterProcess.java

public interface LetterProcess {
    //首先要写信的内容
    public void writeContext(String context);

    //其次写信封
    public void fillEnvelope(String address);

    //把信放到信封里
    public void letterInotoEnvelope();

    //然后邮递
    public void sendLetter();
}

具体实现过程

LetterProcessImpl.java

public class LetterProcessImpl implements LetterProcess{
    @Override
    public void writeContext(String context) {
        System.out.println("填写信息的类容 "+context);
    }

    @Override
    public void fillEnvelope(String address) {
        System.out.println("填写地址 "+address);
    }

    @Override
    public void letterInotoEnvelope() {
        System.out.println("把信放到邮箱");
    }

    @Override
    public void sendLetter() {
        System.out.println("寄送信 ... ...");
    }
}

自己实现

LetterClient.java

public class LetterClient {
    public static void main(String[] args) {
        LetterProcess letterProcess = new LetterProcessImpl();
        //开始写信
        letterProcess.writeContext("Hello,It's me,do you know who I am? I'm your  I'd like to...."); 

        //开始写信封
        letterProcess.fillEnvelope("Happy Road No. 666,God Province,Heaven");

        //把信放到信封里,并封装好
        letterProcess.letterInotoEnvelope();

        //跑到邮局把信塞到邮箱,投递
        letterProcess.sendLetter();

    }
}

邮局

ModenPostOffice.java

public class ModenPostOffice {
    private LetterProcess letterProcess = new LetterProcessImpl();
    
    public void sendLetter(String context , String address){
        //帮你写信
        letterProcess.writeContext(context);

        //写好信封
        letterProcess.fillEnvelope(address);

        // .... 可以增加别的方法
        // 比如检查邮件 
        // 核对 写信人信息
        // 核对 收信人信息
        
        //把信放到信封中
        letterProcess.letterInotoEnvelope();

        //邮递信件
        letterProcess.sendLetter();
    }
}

LetterClient.java

public class LetterClient {
    public static void main(String[] args) {
        
       
        //*****邮局替我们发送信件
        ModenPostOffice hellRoadPostOffice = new ModenPostOffice();

        //你只要把信的内容和收信人地址给他,他会帮你完成一系列的工作;
        String address = "Happy Road No. 666,God Province,Heaven"; //定义一个地址
        String context = "Hello,It's me,do you know who I am? I'm your old lover. I'd like to...."; 
        hellRoadPostOffice.sendLetter(context, address);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值