Skip to content

Commit

Permalink
写信+1 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Q10Viking committed Mar 27, 2024
1 parent e222bf9 commit e4d8f99
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 141 additions & 0 deletions docs/designpattern/07 门面(外观)模式.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,144 @@ public class TestDemo2 {
*/
```



### 写信件

[Source Code](https://gitee.com/q10viking/design-patterns/tree/master/springboot-design-patterns/src/main/java/org/hzz/%E7%BB%93%E6%9E%84%E6%80%A7%E6%A8%A1%E5%BC%8F/SP1_%E9%97%A8%E9%9D%A2%E6%A8%A1%E5%BC%8F/%E6%A1%88%E4%BE%8B/%E5%86%99%E4%BF%A1%E4%BB%B6)

这个案例和最开始提到的喝茶类似。这里我们用现代邮局来替代了喝茶中服务员的角色,也就是门面(外观)

![image-20240327211047812](/images/designpattern/image-20240327211047812.png)

#### 写信具体步骤

```java
package org.hzz.结构性模式.SP1_门面模式.案例.写信件;

public interface ILetterProcess {
//首先要写信的内容
public void writeContext(String context);
//其次写信封
public void fillEnvelope(String address);
//把信放到信封里
public void letterInotoEnvelope();
//然后邮递
public void sendLetter();
}

```

```java
package org.hzz.结构性模式.SP1_门面模式.案例.写信件.impl;

import org.hzz.结构性模式.SP1_门面模式.案例.写信件.ILetterProcess;

public class LetterProcessImpl implements ILetterProcess {
//写信
public void writeContext(String context) {
System.out.println("填写信的内容...." + context);
}
//在信封上填写必要的信息
public void fillEnvelope(String address) {
System.out.println("填写收件人地址及姓名...." + address);
}
//把信放到信封中,并封好
public void letterInotoEnvelope() {
System.out.println("把信放到信封中....");
}
//塞到邮箱中,邮递
public void sendLetter() {
System.out.println("邮递信件...");
}
}

```

一个检查程序

```java
package org.hzz.结构性模式.SP1_门面模式.案例.写信件;

/**
* 检查信件,比如是不是恐吓信,有没有炭疽病毒,寄往某地的邮件都要检查
*/
public class Police {

//检查信件,检查完毕后警察在信封上盖个戳:此信无病毒
public void checkLetter(ILetterProcess letterProcess){
System.out.println(letterProcess+" 信件已经检查过了.....");
}
}

```



#### 门面邮局

```java
package org.hzz.结构性模式.SP1_门面模式.案例.写信件;

import org.hzz.结构性模式.SP1_门面模式.案例.写信件.impl.LetterProcessImpl;

/**
* 相当于门面
*/
public class ModenPostOffice {
private ILetterProcess letterProcess = new LetterProcessImpl();
private Police letterPolice = new Police();

//写信,封装,投递,一体化了
public void sendLetter(String context,String address){

//帮你写信
letterProcess.writeContext(context);

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

//警察要检查信件了
letterPolice.checkLetter(letterProcess);

//把信放到信封中
letterProcess.letterInotoEnvelope();

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

}
}

```



#### 测试

```java
package org.hzz.结构性模式.SP1_门面模式.案例.写信件;

public class TestDemo {
public static void main(String[] args) {
//现代化的邮局,有这项服务,邮局名称叫Hell Road
ModenPostOffice hellRoadPostOffice = new ModenPostOffice();
//你只要把信的内容和收信人地址给他,他会帮你完成一系列的工作;
//定义一个地址
String address = "广东省深圳市福田区,卡卡收";
//信的内容
String context = "桂林风景甲天下,阳朔风景甲桂林";
//一个门面搞定
hellRoadPostOffice.sendLetter(context, address);
}
}
/**
* 填写信的内容....桂林风景甲天下,阳朔风景甲桂林
* 填写收件人地址及姓名....广东省深圳市福田区,卡卡收
* org.hzz.结构性模式.SP1_门面模式.案例.写信件.impl.LetterProcessImpl@4d7e1886 信件已经检查过了.....
* 把信放到信封中....
* 邮递信件...
*/

```

0 comments on commit e4d8f99

Please sign in to comment.