迭代器、中介者和工厂模式实验课
一.验证第11章迭代器模式,模拟点钞机应用
1.实验内容和源程序
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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package exe3;
import java.util.*; public class Application { public static void main(String args[]){ int n = 20; int sum = 0; Collection<RenMinMony> set = new HashSet<RenMinMony>(); for(int i=1;i<=n;i++){ if(i == n/2 || i == n/5 || i == n/6) set.add(new RenMinMony(100,false)); else set.add(new RenMinMony(100,true)); } Iterator<RenMinMony>iterator = set.iterator(); int jia = 1,zhen = 1; System.out.println("保险箱共有"+set.size()+"张人民币"); int k = 0; while(iterator.hasNext()){ RenMinMony money = iterator.next(); k++; if(money.getIsTrue()==false){ System.out.println("第"+k+"张是假币,被销毁"); iterator.remove(); } } System.out.println("保险箱现有真人民币"+set.size()+"张,总价值是:"); iterator = set.iterator(); while(iterator.hasNext()){ RenMinMony money = iterator.next(); sum = sum+money.getValue(); } System.out.println(sum+"元"); } }
class RenMinMony{ int value; private boolean isTrue; RenMinMony(int value,boolean b){ this.value = value; isTrue = b; } public boolean getIsTrue(){ return isTrue; } public int getValue(){ return value; } }
|
2.实验执行结果
3.本次收获体会
认识了迭代器模式,并掌握了经典例题的方法和执行过程以及具体设计框架
二.验证第12章中介者模式,模拟三国交战的停战协议
1.实验内容和源程序
Colleague.java
1 2 3 4 5 6
| public interface Colleague { public void giveMess(String [] mess); public void receiverMess(String mess); public void setName(String mess); public String getName(); }
|
ConcreteMediator.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 25 26 27 28 29 30 31 32 33 34
| public class ConcreteMediator { ColleagueA colleagueA; ColleagueB colleagueB; ColleagueC colleagueC; public void registerColleagueA(ColleagueA colleagueA){ this.colleagueA=colleagueA; } public void registerColleagueB(ColleagueB colleagueB){ this.colleagueB=colleagueB; } public void registerColleagueC(ColleagueC colleagueC){ this.colleagueC=colleagueC; } public void deliverMess(Colleague colleague,String [] mess){ if(colleague==colleagueA){ if(mess.length>=2){ colleagueB.receiverMess(colleague.getName()+mess[0]); colleagueC.receiverMess(colleague.getName()+mess[1]); } } else if(colleague==colleagueB){ if(mess.length>=2){ colleagueA.receiverMess(colleague.getName()+mess[0]); colleagueC.receiverMess(colleague.getName()+mess[1]); } } else if(colleague==colleagueC){ if(mess.length>=2){ colleagueA.receiverMess(colleague.getName()+mess[0]); colleagueB.receiverMess(colleague.getName()+mess[1]); } } } }
|
ColleagueA.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class ColleagueA implements Colleague{ ConcreteMediator mediator; String name; ColleagueA(ConcreteMediator mediator){ this.mediator=mediator; mediator.registerColleagueA(this); } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void giveMess(String [] mess){ mediator.deliverMess(this, mess); } public void receiverMess(String mess){ System.out.println(name+"收到的消息:"); System.out.println("\t"+mess); } }
|
ColleagueB.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class ColleagueB implements Colleague{ ConcreteMediator mediator; String name; ColleagueB(ConcreteMediator mediator){ this.mediator=mediator; mediator.registerColleagueB(this); } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void giveMess(String [] mess){ mediator.deliverMess(this, mess); } public void receiverMess(String mess){ System.out.println(name+"收到的消息:"); System.out.println("\t"+mess); } }
|
ColleagueC.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class ColleagueC implements Colleague{ ConcreteMediator mediator; String name; ColleagueC(ConcreteMediator mediator){ this.mediator=mediator; mediator.registerColleagueC(this); } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void giveMess(String [] mess){ mediator.deliverMess(this, mess); } public void receiverMess(String mess){ System.out.println(name+"收到的消息:"); System.out.println("\t"+mess); } }
|
Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Application { public static void main(String args[]){ ConcreteMediator mediator = new ConcreteMediator(); ColleagueA colleagueA = new ColleagueA(mediator); ColleagueB colleagueB = new ColleagueB(mediator); ColleagueC colleagueC = new ColleagueC(mediator); colleagueA.setName("A国"); colleagueB.setName("B国"); colleagueC.setName("C国"); String [] messA = {"要求归还曾抢夺的100斤土豆","要求归还曾抢夺的20头牛"}; colleagueA.giveMess(messA); String [] messB = {"要求归还曾抢夺的10只公鸡","要求归还曾抢夺的15匹马"}; colleagueB.giveMess(messB); String [] messC = {"要求归还曾抢夺的300斤小麦","要求归还曾抢夺的50头驴"}; colleagueC.giveMess(messC); } }
|
2.实验执行结果
3.本次收获体会
掌握了中介者模式的设计模式,和其实现的方法
三.验证第13章工厂方法模式,模拟生产笔芯的例子
1.实验内容和源程序
PenCore.java
1 2 3 4
| public abstract class PenCore { String color; public abstract void writeWord(String s); }
|
RedPenCore.java
1 2 3 4 5 6 7 8
| public class RedPenCore extends PenCore{ RedPenCore(){ color = "红色"; } public void writeWord(String s){ System.out.println("写出"+color+"的字:"+s); } }
|
BluePenCore.java
1 2 3 4 5 6 7 8
| public class BluePenCore extends PenCore{ BluePenCore(){ color = "蓝色"; } public void writeWord(String s){ System.out.println("写出"+color+"的字:"+s); } }
|
BlackPenCore.java
1 2 3 4 5 6 7 8
| public class BlackPenCore extends PenCore{ BlackPenCore(){ color = "黑色"; } public void writeWord(String s){ System.out.println("写出"+color+"的字:"+s); } }
|
BallPen.java
1 2 3 4 5 6
| public abstract class BallPen { BallPen(){ System.out.println("生产了一只装有"+getPenCore().color+"笔芯的圆珠笔"); } public abstract PenCore getPenCore(); }
|
RedBallPen.java
1 2 3 4 5
| public class RedBallPen extends BallPen{ public PenCore getPenCore(){ return new RedPenCore(); } }
|
BlueBallPen.java
1 2 3 4 5
| public class BlueBallPen extends BallPen{ public PenCore getPenCore(){ return new BluePenCore(); } }
|
BlackBallPen.java
1 2 3 4 5
| public class BlackBallPen extends BallPen{ public PenCore getPenCore(){ return new BlackPenCore(); } }
|
Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Application { public static void main(String args[]){ PenCore penCore; BallPen ballPen = new BlueBallPen(); penCore = ballPen.getPenCore(); penCore.writeWord("你好,很高兴认识你");
ballPen = new RedBallPen(); penCore = ballPen.getPenCore(); penCore.writeWord("How are you");
ballPen = new BlackBallPen(); penCore = ballPen.getPenCore(); penCore.writeWord("Nice to meet you");
|
2.实验执行结果
3.本次收获体会
了解和认识了基本的工厂方法模式,对模式的架构和流程有了基本的认识。
本文使用 CC BY-NC-SA 3.0 中国大陆 协议许可
具体请参见 知识共享协议
本文链接:https://zyhang8.github.io/2019/11/10/design-exp5/