责任链模式和外观模式
一.责任链模式,模拟身份证查询
1.实验内容和源程序
Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Application{ private Handler beijing,shanghai,tianjin; public void createChain(){ beijing=new Beijing(); shanghai=new Shanghai(); tianjin=new Tianjin(); beijing.setNextHandler(shanghai); shanghai.setNextHandler(tianjin); } public void reponseClient(String number){ beijing.handleRequest(number); } public static void main(String args[]){ Application application=new Application(); application.createChain(); application.reponseClient("30029812340930034");; } }
|
Beijing.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.*; public class Beijing implements Handler{ private Handler handler; private ArrayList<String> numberList; Beijing(){ numberList=new ArrayList<String>(); numberList.add("11129812340930034"); numberList.add("10120810340930632"); numberList.add("22029812340930034"); numberList.add("32620810340930632"); } public void handleRequest(String number){ if(numberList.contains(number)) System.out.println("该人在北京居住"); else{ System.out.println("该人不在北京居住"); if(handler!=null) handler.handleRequest(number); } } public void setNextHandler(Handler handler){ this.handler=handler; } }
|
Handler.java
1 2 3 4
| public interface Handler{ public abstract void handleRequest(String number); public abstract void setNextHandler(Handler handler); }
|
Shanghai.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.*; public class Shanghai implements Handler{ private Handler handler; private ArrayList<String> numberList; Shanghai(){ numberList=new ArrayList<String>(); numberList.add("34529812340930034"); numberList.add("98720810340430632"); numberList.add("36529812340930034"); numberList.add("77720810340930632"); } public void handleRequest(String number){ if(numberList.contains(number)) System.out.println("该人在上海居住"); else{ System.out.println("该人不在上海居住"); if(handler!=null) handler.handleRequest(number); } } public void setNextHandler(Handler handler){ this.handler=handler; } }
|
Tianjin.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.*; public class Tianjin implements Handler{ private Handler handler; private ArrayList<String> numberList; Tianjin(){ numberList=new ArrayList<String>(); numberList.add("10029812340930034"); numberList.add("20020810340430632"); numberList.add("30029812340930034"); numberList.add("50020810340930632"); } public void handleRequest(String number){ if(numberList.contains(number)) System.out.println("该人在天津居住"); else{ System.out.println("该人不在天津居住"); if(handler!=null) handler.handleRequest(number); } } public void setNextHandler(Handler handler){ this.handler=handler; } }
|
2.实验执行结果
3.本次收获体会
责任链模式可以使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系, 将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理他为止
二.责任链模式上,设计循环责任链模式,查询起点可以任意设定
1.实验内容和源程序
循环模式只是更改了Application.java里面的内容,其余与第一题一样
Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.Scanner; public class Application{ private Handler beijing,shanghai,tianjin; public void createChain(){ beijing=new Beijing(); shanghai=new Shanghai(); tianjin=new Tianjin(); beijing.setNextHandler(shanghai); shanghai.setNextHandler(tianjin); } public void reponseClient(String number){ beijing.handleRequest(number); } public static void main(String args[]){ Application application=new Application(); application.createChain(); while (true) { Scanner input = new Scanner(System.in); System.out.println("请输入身份证号:"); String name = input.next(); application.reponseClient(name); } } }
|
2.实验执行结果
三.外观模式,模拟广告社排版
1.实验内容和源程序
Application.java
1 2 3 4 5 6 7 8
| public class Application{ public static void main(String args[]){ ClientServerFacade clientFacade; String clientAdvertisement="月光电脑,价格6356元,联系电话:1234567"; clientFacade=new ClientServerFacade(clientAdvertisement); clientFacade.doAdvertisement(); } }
|
Charge.java
1 2 3 4 5 6 7 8 9 10 11
| public class Charge{ public final int basicCharge=12; CheckWord checkWord; Charge(CheckWord checkWord){ this.checkWord=checkWord; } public void giveCharge(){ int charge=checkWord.getAmount()*basicCharge; System.out.println("广告费用:"+charge+"元"); } }
|
CheckWord.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class CheckWord{ public final int basicAmount=85; String advertisement; int amount; public CheckWord(String advertisement){ this.advertisement=advertisement; } public void setChargeAmount(){ amount=advertisement.length()+basicAmount; } public int getAmount(){ return amount; } }
|
ClientServerFacade.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class ClientServerFacade{ private CheckWord checkWord; private Charge charge; private TypeSeting typeSeting; String advertisement; public ClientServerFacade(String advertisement){ this.advertisement=advertisement; checkWord=new CheckWord(advertisement); charge=new Charge(checkWord); typeSeting=new TypeSeting(advertisement); } public void doAdvertisement(){ checkWord.setChargeAmount(); charge.giveCharge(); typeSeting.typeSeting(); } }
|
SafetyCheck.java
1 2 3 4 5 6 7 8 9 10 11 12
| public class SafetyCheck{ String clientPacket; public SafetyCheck(String clientPacket){ this.clientPacket=clientPacket; } public boolean isSave(){ if(clientPacket.contains("汽油")) return false; else return true; } }
|
TypeSeting.java
1 2 3 4 5 6 7 8 9 10 11 12
| public class TypeSeting{ String advertisement; public TypeSeting(String advertisement){ this.advertisement=advertisement; } public void typeSeting(){ System.out.println("广告排版格式:"); System.out.println("********"); System.out.println(advertisement); System.out.println("********"); } }
|
2.实验执行结果
3.本次收获体会
外观模式(Facade),他隐藏了系统的复杂性,并向客户端提供了一个可以访问系统的接口。这种类型的设计模式属于结构性模式。为子系统中的一组接口提供了一个统一的访问接口,这个接口使得子系统更容易被访问或者使用。
本文使用 CC BY-NC-SA 3.0 中国大陆 协议许可
具体请参见 知识共享协议
本文链接:https://zyhang8.github.io/2019/11/09/design-exp4/