行为型模式:
第16章 12模板方法模式(Template)
16.1 豆浆制作问题
16.2 模板方法模式基本介绍
16.3 模板方法模式原理类图
16.4 模板方法模式解决豆浆制作问题
package com.atguigu.template;//抽象类,表示豆浆
public abstract class SoyaMilk {//模板方法, make , 模板方法可以做成final , 不让子类去覆盖.final void make() {select(); addCondiments();soak();beat();}//选材料void select() {System.out.println("第一步:选择好的新鲜黄豆 ");}//添加不同的配料, 抽象方法, 子类具体实现abstract void addCondiments();//浸泡void soak() {System.out.println("第三步, 黄豆和配料开始浸泡, 需要3小时 ");}void beat() {System.out.println("第四步:黄豆和配料放到豆浆机去打碎 ");}
}
package com.atguigu.template;public class RedBeanSoyaMilk extends SoyaMilk {@Overridevoid addCondiments() {System.out.println(" 加入上好的红豆 ");}}
package com.atguigu.template;public class PeanutSoyaMilk extends SoyaMilk {@Overridevoid addCondiments() {System.out.println(" 加入上好的花生 ");}}
客户端
package com.atguigu.template;public class Client {public static void main(String[] args) {//制作红豆豆浆System.out.println("----制作红豆豆浆----");SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk();redBeanSoyaMilk.make();System.out.println("----制作花生豆浆----");SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk();peanutSoyaMilk.make();}}
16.5 模板方法模式的钩子方法
package com.atguigu.template.improve;//抽象类,表示豆浆
public abstract class SoyaMilk {//模板方法, make , 模板方法可以做成final , 不让子类去覆盖.final void make() {select(); if(customerWantCondiments()) {addCondiments();}soak();beat();}//选材料void select() {System.out.println("第一步:选择好的新鲜黄豆 ");}//添加不同的配料, 抽象方法, 子类具体实现abstract void addCondiments();//浸泡void soak() {System.out.println("第三步, 黄豆和配料开始浸泡, 需要3小时 ");}void beat() {System.out.println("第四步:黄豆和配料放到豆浆机去打碎 ");}//钩子方法,决定是否需要添加配料boolean customerWantCondiments() {return true;}
}
package com.atguigu.template.improve;public class RedBeanSoyaMilk extends SoyaMilk {@Overridevoid addCondiments() {System.out.println(" 加入上好的红豆 ");}}
package com.atguigu.template.improve;public class PeanutSoyaMilk extends SoyaMilk {@Overridevoid addCondiments() {System.out.println(" 加入上好的花生 ");}}
package com.atguigu.template.improve;public class PureSoyaMilk extends SoyaMilk{@Overridevoid addCondiments() {//空实现}@Overrideboolean customerWantCondiments() {return false;}}
客户端
package com.atguigu.template.improve;public class Client {public static void main(String[] args) {//制作红豆豆浆System.out.println("----制作红豆豆浆----");SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk();redBeanSoyaMilk.make();System.out.println("----制作花生豆浆----");SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk();peanutSoyaMilk.make();System.out.println("----制作纯豆浆----");SoyaMilk pureSoyaMilk = new PureSoyaMilk();pureSoyaMilk.make();}}
16.6 模板方法模式在Spring框架应用的源码分析★
16.7 模板方法模式的注意事项和细节
第17章 13命令模式(Command)
17.1 智能生活项目需求
17.2 命令模式基本介绍
17.3 命令模式的原理类图
17.4 命令模式解决智能生活项目
package com.atguigu.command;//创建命令接口
public interface Command {//执行动作(操作)public void execute();//撤销动作(操作)public void undo();
}
package com.atguigu.command;public class LightReceiver {public void on() {System.out.println(" 电灯打开了.. ");}public void off() {System.out.println(" 电灯关闭了.. ");}
}
package com.atguigu.command;public class LightOnCommand implements Command {//聚合LightReceiverLightReceiver light;//构造器public LightOnCommand(LightReceiver light) {super();this.light = light;}@Overridepublic void execute() {//调用接收者的方法light.on();}@Overridepublic void undo() {//调用接收者的方法light.off();}}
package com.atguigu.command;public class LightOffCommand implements Command {// 聚合LightReceiverLightReceiver light;// 构造器public LightOffCommand(LightReceiver light) {super();this.light = light;}@Overridepublic void execute() {// 调用接收者的方法light.off();}@Overridepublic void undo() {// 调用接收者的方法light.on();}
}
package com.atguigu.command;/*** 没有任何命令,即空执行: 用于初始化每个按钮, 当调用空命令时,对象什么都不做* 其实,这样是一种设计模式, 可以省掉对空判断* @author Administrator**/
public class NoCommand implements Command {@Overridepublic void execute() {}@Overridepublic void undo() {}}
package com.atguigu.command;public class RemoteController {// 开 按钮的命令数组Command[] onCommands;Command[] offCommands;// 执行撤销的命令Command undoCommand;// 构造器,完成对按钮初始化public RemoteController() {onCommands = new Command[5];offCommands = new Command[5];for (int i = 0; i < 5; i++) {onCommands[i] = new NoCommand();offCommands[i] = new NoCommand();}}// 给我们的按钮设置你需要的命令public void setCommand(int no, Command onCommand, Command offCommand) {onCommands[no] = onCommand;offCommands[no] = offCommand;}// 按下开按钮public void onButtonWasPushed(int no) { // no 0// 找到你按下的开的按钮, 并调用对应方法onCommands[no].execute();// 记录这次的操作,用于撤销undoCommand = onCommands[no];}// 按下关按钮public void offButtonWasPushed(int no) { // no 0// 找到你按下的关的按钮, 并调用对应方法offCommands[no].execute();// 记录这次的操作,用于撤销undoCommand = offCommands[no];}// 按下撤销按钮public void undoButtonWasPushed() {undoCommand.undo();}}
扩展一个TV按钮
package com.atguigu.command;public class TVReceiver {public void on() {System.out.println(" 电视机打开了.. ");}public void off() {System.out.println(" 电视机关闭了.. ");}
}
package com.atguigu.command;public class TVOnCommand implements Command {// 聚合TVReceiverTVReceiver tv;// 构造器public TVOnCommand(TVReceiver tv) {super();this.tv = tv;}@Overridepublic void execute() {// 调用接收者的方法tv.on();}@Overridepublic void undo() {// 调用接收者的方法tv.off();}
}
package com.atguigu.command;public class TVOffCommand implements Command {// 聚合TVReceiverTVReceiver tv;// 构造器public TVOffCommand(TVReceiver tv) {super();this.tv = tv;}@Overridepublic void execute() {// 调用接收者的方法tv.off();}@Overridepublic void undo() {// 调用接收者的方法tv.on();}
}
客户端
package com.atguigu.command;public class Client {public static void main(String[] args) {//使用命令设计模式,完成通过遥控器,对电灯的操作//创建电灯的对象(接受者)LightReceiver lightReceiver = new LightReceiver();//创建电灯相关的开关命令LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);//需要一个遥控器RemoteController remoteController = new RemoteController();//给我们的遥控器设置命令, 比如 no = 0 是电灯的开和关的操作remoteController.setCommand(0, lightOnCommand, lightOffCommand);System.out.println("--------按下灯的开按钮-----------");remoteController.onButtonWasPushed(0);System.out.println("--------按下灯的关按钮-----------");remoteController.offButtonWasPushed(0);System.out.println("--------按下撤销按钮-----------");remoteController.undoButtonWasPushed();System.out.println("=========使用遥控器操作电视机==========");TVReceiver tvReceiver = new TVReceiver();TVOffCommand tvOffCommand = new TVOffCommand(tvReceiver);TVOnCommand tvOnCommand = new TVOnCommand(tvReceiver);//给我们的遥控器设置命令, 比如 no = 1 是电视机的开和关的操作remoteController.setCommand(1, tvOnCommand, tvOffCommand);System.out.println("--------按下电视机的开按钮-----------");remoteController.onButtonWasPushed(1);System.out.println("--------按下电视机的关按钮-----------");remoteController.offButtonWasPushed(1);System.out.println("--------按下撤销按钮-----------");remoteController.undoButtonWasPushed();}}
17.5 命令模式在Spring框架JdbcTemplate应用的源码分析★
17.6 命令模式的注意事项和细节
第18章 14访问者模式(Visitor)
18.1 测评系统的需求
18.2 传统方式的问题分析
18.3 访问者模式基本介绍
18.4 访问者模式的原理类图
18.5 访问者模式应用实例
package com.atguigu.visitor;public abstract class Action {//得到男性 的测评public abstract void getManResult(Man man);//得到女的 测评public abstract void getWomanResult(Woman woman);
}
package com.atguigu.visitor;public abstract class Person {//提供一个方法,让访问者可以访问public abstract void accept(Action action);
}
package com.atguigu.visitor;public class Man extends Person {@Overridepublic void accept(Action action) {action.getManResult(this);}}
package com.atguigu.visitor;//说明
//1. 这里我们使用到了双分派, 即首先在客户端程序中,将具体状态作为参数传递Woman中(第一次分派)
//2. 然后Woman 类调用作为参数的 "具体方法" 中方法getWomanResult, 同时将自己(this)作为参数
// 传入,完成第二次的分派
public class Woman extends Person{@Overridepublic void accept(Action action) {action.getWomanResult(this);}}
package com.atguigu.visitor;public class Success extends Action {@Overridepublic void getManResult(Man man) {System.out.println(" 男人给的评价该歌手很成功 !");}@Overridepublic void getWomanResult(Woman woman) {System.out.println(" 女人给的评价该歌手很成功 !");}}
package com.atguigu.visitor;public class Fail extends Action {@Overridepublic void getManResult(Man man) {System.out.println(" 男人给的评价该歌手失败 !");}@Overridepublic void getWomanResult(Woman woman) {System.out.println(" 女人给的评价该歌手失败 !");}}
package com.atguigu.visitor;public class Wait extends Action {@Overridepublic void getManResult(Man man) {System.out.println(" 男人给的评价是该歌手待定 ..");}@Overridepublic void getWomanResult(Woman woman) {System.out.println(" 女人给的评价是该歌手待定 ..");}}
package com.atguigu.visitor;import java.util.LinkedList;
import java.util.List;//数据结构,管理很多人(Man , Woman)
public class ObjectStructure {//维护了一个集合private List<Person> persons = new LinkedList<>();//增加到listpublic void attach(Person p) {persons.add(p);}//移除public void detach(Person p) {persons.remove(p);}//显示测评情况public void display(Action action) {for(Person p: persons) {p.accept(action);}}
}
客户端
package com.atguigu.visitor;public class Client {public static void main(String[] args) {//创建ObjectStructureObjectStructure objectStructure = new ObjectStructure();objectStructure.attach(new Man());objectStructure.attach(new Woman());//成功Success success = new Success();objectStructure.display(success);System.out.println("===============");Fail fail = new Fail();objectStructure.display(fail);System.out.println("=======给的是待定的测评========");Wait wait = new Wait();objectStructure.display(wait);}}
18.6 访问者模式的注意事项和细节
第19章 15迭代器模式(Iterator)
19.1 看一个具体的需求
19.2 传统的设计方案(类图)
19.3 传统的方式的问题分析
19.4 迭代器模式基本介绍
19.5 迭代器模式的原理类图
19.6 迭代器模式应用实例
package com.atguigu.iterator;//ϵ
public class Department {private String name;private String desc;public Department(String name, String desc) {super();this.name = name;this.desc = desc;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}}
package com.atguigu.iterator;import java.util.Iterator;public class ComputerCollegeIterator implements Iterator {//这里我们需要知道Department 是以怎样的方式存放=>数组Department[] departments;int position = 0; //遍历的位置public ComputerCollegeIterator(Department[] departments) {this.departments = departments;}//判断是否还有下一个元素@Overridepublic boolean hasNext() {if(position >= departments.length || departments[position] == null) {return false;}else {return true;}}@Overridepublic Object next() {Department department = departments[position];position += 1;return department;}//删除的方法,默认空实现public void remove() {}}
package com.atguigu.iterator;import java.util.Iterator;
import java.util.List;public class InfoColleageIterator implements Iterator {List<Department> departmentList; // 信息工程学院是以List方式存放系int index = -1;//索引public InfoColleageIterator(List<Department> departmentList) {this.departmentList = departmentList;}//判断list中还有没有下一个元素@Overridepublic boolean hasNext() {if(index >= departmentList.size() - 1) {return false;} else {index += 1;return true;}}@Overridepublic Object next() {return departmentList.get(index);}//空实现removepublic void remove() {}}
package com.atguigu.iterator;import java.util.Iterator;public interface College {public String getName();//增加系的方法public void addDepartment(String name, String desc);//返回一个迭代器,遍历public Iterator createIterator();
}
package com.atguigu.iterator;import java.util.Iterator;public class ComputerCollege implements College {Department[] departments;int numOfDepartment = 0 ;// 保存当前数组的对象个数public ComputerCollege() {departments = new Department[5];addDepartment("Java专业", " Java专业 ");addDepartment("PHP专业", " PHP专业 ");addDepartment("大数据专业", " 大数据专业 ");}@Overridepublic String getName() {return "计算机学院";}@Overridepublic void addDepartment(String name, String desc) {Department department = new Department(name, desc);departments[numOfDepartment] = department;numOfDepartment += 1;}@Overridepublic Iterator createIterator() {return new ComputerCollegeIterator(departments);}}
package com.atguigu.iterator;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class InfoCollege implements College {List<Department> departmentList;public InfoCollege() {departmentList = new ArrayList<Department>();addDepartment("信息安全专业", " 信息安全专业 ");addDepartment("网络安全专业", " 网络安全专业 ");addDepartment("服务器安全专业", " 服务器安全专业 ");}@Overridepublic String getName() {return "信息工程学院";}@Overridepublic void addDepartment(String name, String desc) {Department department = new Department(name, desc);departmentList.add(department);}@Overridepublic Iterator createIterator() {return new InfoColleageIterator(departmentList);}}
package com.atguigu.iterator;import java.util.Iterator;
import java.util.List;public class OutPutImpl {//学院集合List<College> collegeList;public OutPutImpl(List<College> collegeList) {this.collegeList = collegeList;}//遍历所有学院,然后调用printDepartment 输出各个学院的系public void printCollege() {//从collegeList 取出所有学院, Java 中的 List 已经实现IteratorIterator<College> iterator = collegeList.iterator();while(iterator.hasNext()) {//取出一个学院College college = iterator.next();System.out.println("=== "+college.getName() +"=====" );printDepartment(college.createIterator()); //得到对应迭代器}}//输出 学院输出 系public void printDepartment(Iterator iterator) {while(iterator.hasNext()) {Department d = (Department)iterator.next();System.out.println(d.getName());}}}
客户端
package com.atguigu.iterator;import java.util.ArrayList;
import java.util.List;public class Client {public static void main(String[] args) {//创建学院List<College> collegeList = new ArrayList<College>();ComputerCollege computerCollege = new ComputerCollege();InfoCollege infoCollege = new InfoCollege();collegeList.add(computerCollege);//collegeList.add(infoCollege);OutPutImpl outPutImpl = new OutPutImpl(collegeList);outPutImpl.printCollege();}}
19.7 迭代器模式在JDK-ArrayList集合应用的源码分析★
package com.atguigu.jdk;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class IteratorDemo {public static void main(String[] args) {List<String> a = new ArrayList<>();a.add("jack");// ..// 获取到迭代器Iterator Itr = a.iterator();while (Itr.hasNext()) {System.out.println(Itr.next());}}
}
ArrayList内部类Itr实现Iterator接口
ArrayList内部类Itr直接使用Object[] elementData
19.8 迭代器模式的注意事项和细节
第20章 16观察者模式(Observer)★
20.1 天气预报项目需求,具体要求如下
20.2 天气预报设计方案1-普通方案
package com.atguigu.observer;/*** 显示当前天气情况(可以理解成是气象站自己的网站)* @author Administrator**/
public class CurrentConditions {// 温度,气压,湿度private float temperature;private float pressure;private float humidity;//更新 天气情况,是由 WeatherData 来调用,我使用推送模式public void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}//显示public void display() {System.out.println("***Today mTemperature: " + temperature + "***");System.out.println("***Today mPressure: " + pressure + "***");System.out.println("***Today mHumidity: " + humidity + "***");}
}
package com.atguigu.observer;/*** 类是核心* 1. 包含最新的天气情况信息 * 2. 含有 CurrentConditions 对象* 3. 当数据有更新时,就主动的调用 CurrentConditions对象update方法(含 display), 这样他们(接入方)就看到最新的信息* @author Administrator**/
public class WeatherData {private float temperatrue;private float pressure;private float humidity;private CurrentConditions currentConditions;//加入新的第三方public WeatherData(CurrentConditions currentConditions) {this.currentConditions = currentConditions;}public float getTemperature() {return temperatrue;}public float getPressure() {return pressure;}public float getHumidity() {return humidity;}public void dataChange() {//调用 接入方的 updatecurrentConditions.update(getTemperature(), getPressure(), getHumidity());}//当数据有更新时,就调用 setDatapublic void setData(float temperature, float pressure, float humidity) {this.temperatrue = temperature;this.pressure = pressure;this.humidity = humidity;//调用dataChange, 将最新的信息 推送给 接入方 currentConditionsdataChange();}
}
客户端
package com.atguigu.observer;public class Client {public static void main(String[] args) {//创建接入方 currentConditionsCurrentConditions currentConditions = new CurrentConditions();//创建 WeatherData 并将 接入方 currentConditions 传递到 WeatherData中WeatherData weatherData = new WeatherData(currentConditions);//更新天气情况weatherData.setData(30, 150, 40);//天气情况变化System.out.println("============天气情况变化=============");weatherData.setData(40, 160, 20);}
}
20.3 观察者模式原理
20.4 观察者模式解决天气预报需求
20.4.1 类图说明
20.4.2 代码实现
package com.atguigu.observer.improve;//接口, 让WeatherData 来实现
public interface Subject {public void registerObserver(Observer o);public void removeObserver(Observer o);public void notifyObservers();
}
package com.atguigu.observer.improve;//观察者接口,有观察者来实现
public interface Observer {public void update(float temperature, float pressure, float humidity);
}
package com.atguigu.observer.improve;public class CurrentConditions implements Observer {// 温度,气压,湿度private float temperature;private float pressure;private float humidity;// 更新 天气情况,是由 WeatherData 来调用,我使用推送模式public void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}// 显示public void display() {System.out.println("***Today mTemperature: " + temperature + "***");System.out.println("***Today mPressure: " + pressure + "***");System.out.println("***Today mHumidity: " + humidity + "***");}
}
package com.atguigu.observer.improve;public class BaiduSite implements Observer {// 温度,气压,湿度private float temperature;private float pressure;private float humidity;// 更新 天气情况,是由 WeatherData 来调用,我使用推送模式public void update(float temperature, float pressure, float humidity) {this.temperature = temperature;this.pressure = pressure;this.humidity = humidity;display();}// 显示public void display() {System.out.println("===百度网站====");System.out.println("***百度网站 气温 : " + temperature + "***");System.out.println("***百度网站 气压: " + pressure + "***");System.out.println("***百度网站 湿度: " + humidity + "***");}}
package com.atguigu.observer.improve;import java.util.ArrayList;/*** 类是核心* 1. 包含最新的天气情况信息 * 2. 含有 观察者集合,使用ArrayList管理* 3. 当数据有更新时,就主动的调用 ArrayList, 通知所有的(接入方)就看到最新的信息* @author Administrator**/
public class WeatherData implements Subject {private float temperatrue;private float pressure;private float humidity;//观察者集合private ArrayList<Observer> observers;//加入新的第三方public WeatherData() {observers = new ArrayList<Observer>();}public float getTemperature() {return temperatrue;}public float getPressure() {return pressure;}public float getHumidity() {return humidity;}public void dataChange() {//调用 接入方的 updatenotifyObservers();}//当数据有更新时,就调用 setDatapublic void setData(float temperature, float pressure, float humidity) {this.temperatrue = temperature;this.pressure = pressure;this.humidity = humidity;//调用dataChange, 将最新的信息 推送给 接入方 currentConditionsdataChange();}//注册一个观察者@Overridepublic void registerObserver(Observer o) {observers.add(o);}//移除一个观察者@Overridepublic void removeObserver(Observer o) {if(observers.contains(o)) {observers.remove(o);}}//遍历所有的观察者,并通知@Overridepublic void notifyObservers() {for(int i = 0; i < observers.size(); i++) {observers.get(i).update(this.temperatrue, this.pressure, this.humidity);}}
}
客户端
package com.atguigu.observer.improve;public class Client {public static void main(String[] args) {//创建一个WeatherDataWeatherData weatherData = new WeatherData();//创建观察者CurrentConditions currentConditions = new CurrentConditions();BaiduSite baiduSite = new BaiduSite();//注册到weatherDataweatherData.registerObserver(currentConditions);weatherData.registerObserver(baiduSite);//测试System.out.println("通知各个注册的观察者, 看看信息");weatherData.setData(10f, 100f, 30.3f);weatherData.removeObserver(currentConditions);//测试System.out.println();System.out.println("通知各个注册的观察者, 看看信息");weatherData.setData(10f, 100f, 30.3f);}}
20.4.3 观察者模式的好处
20.5 观察者模式在jdk应用的源码分析★
第21章 17中介者模式(Mediator)
21.1 智能家庭项目
21.2 传统方案解决智能家庭管理问题
21.3 传统方式的问题分析
21.4 中介者模式基本介绍
21.5 中介者模式的原理类图
21.6 中介者模式应用实例-智能家庭管理
package com.atguigu.mediator.smarthouse;//同事抽象类
public abstract class Colleague {private Mediator mediator;public String name;public Colleague(Mediator mediator, String name) {this.mediator = mediator;this.name = name;}public Mediator GetMediator() {return this.mediator;}public abstract void SendMessage(int stateChange);
}
package com.atguigu.mediator.smarthouse;//具体的同事类
public class Alarm extends Colleague {//构造器public Alarm(Mediator mediator, String name) {super(mediator, name);//在创建Alarm 同事对象时,将自己放入到ConcreteMediator 对象中[集合]mediator.Register(name, this);}public void SendAlarm(int stateChange) {SendMessage(stateChange);}@Overridepublic void SendMessage(int stateChange) {//调用的中介者对象的getMessagethis.GetMediator().GetMessage(stateChange, this.name);}}
package com.atguigu.mediator.smarthouse;public class Curtains extends Colleague {public Curtains(Mediator mediator, String name) {super(mediator, name);mediator.Register(name, this);}@Overridepublic void SendMessage(int stateChange) {this.GetMediator().GetMessage(stateChange, this.name);}public void UpCurtains() {System.out.println("I am holding Up Curtains!");}}
package com.atguigu.mediator.smarthouse;public class CoffeeMachine extends Colleague {public CoffeeMachine(Mediator mediator, String name) {super(mediator, name);mediator.Register(name, this);}@Overridepublic void SendMessage(int stateChange) {this.GetMediator().GetMessage(stateChange, this.name);}public void StartCoffee() {System.out.println("It's time to startcoffee!");}public void FinishCoffee() {System.out.println("After 5 minutes!");System.out.println("Coffee is ok!");SendMessage(0);}
}
package com.atguigu.mediator.smarthouse;public class TV extends Colleague {public TV(Mediator mediator, String name) {super(mediator, name);mediator.Register(name, this);}@Overridepublic void SendMessage(int stateChange) {this.GetMediator().GetMessage(stateChange, this.name);}public void StartTv() {System.out.println("It's time to StartTv!");}public void StopTv() {System.out.println("StopTv!");}
}
中介者
package com.atguigu.mediator.smarthouse;public abstract class Mediator {//将给中介者对象,加入到集合中public abstract void Register(String colleagueName, Colleague colleague);//接收消息, 具体的同事对象发出public abstract void GetMessage(int stateChange, String colleagueName);public abstract void SendMessage();
}
package com.atguigu.mediator.smarthouse;import java.util.HashMap;//具体的中介者类
public class ConcreteMediator extends Mediator {//集合,放入所有的同事对象private HashMap<String, Colleague> colleagueMap;private HashMap<String, String> interMap;public ConcreteMediator() {colleagueMap = new HashMap<String, Colleague>();interMap = new HashMap<String, String>();}@Overridepublic void Register(String colleagueName, Colleague colleague) {colleagueMap.put(colleagueName, colleague);if (colleague instanceof Alarm) {interMap.put("Alarm", colleagueName);} else if (colleague instanceof CoffeeMachine) {interMap.put("CoffeeMachine", colleagueName);} else if (colleague instanceof TV) {interMap.put("TV", colleagueName);} else if (colleague instanceof Curtains) {interMap.put("Curtains", colleagueName);}}//具体中介者的核心方法//1. 根据得到消息,完成对应任务//2. 中介者在这个方法,协调各个具体的同事对象,完成任务@Overridepublic void GetMessage(int stateChange, String colleagueName) {//处理闹钟发出的消息if (colleagueMap.get(colleagueName) instanceof Alarm) {if (stateChange == 0) {((CoffeeMachine) (colleagueMap.get(interMap.get("CoffeeMachine")))).StartCoffee();((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();} else if (stateChange == 1) {((TV) (colleagueMap.get(interMap.get("TV")))).StopTv();}} else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {((Curtains) (colleagueMap.get(interMap.get("Curtains")))).UpCurtains();} else if (colleagueMap.get(colleagueName) instanceof TV) {//如果TV发现消息} else if (colleagueMap.get(colleagueName) instanceof Curtains) {//如果是以窗帘发出的消息,这里处理...}}@Overridepublic void SendMessage() {}}
客户端
package com.atguigu.mediator.smarthouse;public class ClientTest {public static void main(String[] args) {//创建一个中介者对象Mediator mediator = new ConcreteMediator();//创建Alarm 并且加入到 ConcreteMediator 对象的HashMapAlarm alarm = new Alarm(mediator, "alarm");//创建了CoffeeMachine 对象,并 且加入到 ConcreteMediator 对象的HashMapCoffeeMachine coffeeMachine = new CoffeeMachine(mediator,"coffeeMachine");//创建 Curtains , 并 且加入到 ConcreteMediator 对象的HashMapCurtains curtains = new Curtains(mediator, "curtains");TV tV = new TV(mediator, "TV");//让闹钟发出消息alarm.SendAlarm(0);coffeeMachine.FinishCoffee();alarm.SendAlarm(1);}}
21.7 中介者模式的注意事项和细节
第22章 18备忘录模式(Memento)
22.1 游戏角色状态恢复问题
22.2 传统方案解决游戏角色恢复
22.3 传统方式的问题分析
22.4 备忘录模式基本介绍
22.5 备忘录模式的原理类图
package com.atguigu.memento.theory;public class Originator {private String state;//状态信息public String getState() {return state;}public void setState(String state) {this.state = state;}//编写一个方法,可以保存一个状态对象 Memento//因此编写一个方法,返回 Mementopublic Memento saveStateMemento() {return new Memento(state);}//通过备忘录对象,恢复状态public void getStateFromMemento(Memento memento) {state = memento.getState();}
}
package com.atguigu.memento.theory;public class Memento {private String state;//构造器public Memento(String state) {super();this.state = state;}public String getState() {return state;}}
package com.atguigu.memento.theory;import java.util.ArrayList;
import java.util.List;public class Caretaker {//在List 集合中会有很多的备忘录对象private List<Memento> mementoList = new ArrayList<Memento>();public void add(Memento memento) {mementoList.add(memento);}//获取到第index个Originator 的 备忘录对象(即保存状态)public Memento get(int index) {return mementoList.get(index);}
}
客户端
package com.atguigu.memento.theory;import java.util.ArrayList;
import java.util.HashMap;public class Client {public static void main(String[] args) {Originator originator = new Originator();Caretaker caretaker = new Caretaker();originator.setState(" 状态#1 攻击力 100 ");//保存了当前的状态caretaker.add(originator.saveStateMemento());originator.setState(" 状态#2 攻击力 80 ");caretaker.add(originator.saveStateMemento());originator.setState(" 状态#3 攻击力 50 ");caretaker.add(originator.saveStateMemento());System.out.println("当前的状态是 =" + originator.getState());//希望得到状态 1, 将 originator 恢复到状态1originator.getStateFromMemento(caretaker.get(0));System.out.println("恢复到状态1 , 当前的状态是");System.out.println("当前的状态是 =" + originator.getState());}}
22.6 游戏角色恢复状态实例
package com.atguigu.memento.game;@Data
public class Memento {//攻击力private int vit;//防御力private int def;public Memento(int vit, int def) {super();this.vit = vit;this.def = def;}}
package com.atguigu.memento.game;import java.util.ArrayList;
import java.util.HashMap;//守护者对象, 保存游戏角色的状态
public class Caretaker {//如果只保存一次状态private Memento memento;//对GameRole 保存多次状态//private ArrayList<Memento> mementos;//对多个游戏角色保存多个状态//private HashMap<String, ArrayList<Memento>> rolesMementos;public Memento getMemento() {return memento;}public void setMemento(Memento memento) {this.memento = memento;}}
package com.atguigu.memento.game;@Data
public class GameRole {private int vit;private int def;//创建Memento ,即根据当前的状态得到Mementopublic Memento createMemento() {return new Memento(vit, def);}//从备忘录对象,恢复GameRole的状态public void recoverGameRoleFromMemento(Memento memento) {this.vit = memento.getVit();this.def = memento.getDef();}//显示当前游戏角色的状态public void display() {System.out.println("游戏角色当前的攻击力:" + this.vit + " 防御力: " + this.def);}}
客户端
package com.atguigu.memento.game;public class Client {public static void main(String[] args) {//创建游戏角色GameRole gameRole = new GameRole();gameRole.setVit(100);gameRole.setDef(100);System.out.println("和boss大战前的状态");gameRole.display();//把当前状态保存caretakerCaretaker caretaker = new Caretaker();caretaker.setMemento(gameRole.createMemento());System.out.println("和boss大战~~~");gameRole.setDef(30);gameRole.setVit(30);gameRole.display();System.out.println("大战后,使用备忘录对象恢复到大战前");gameRole.recoverGameRoleFromMemento(caretaker.getMemento());System.out.println("恢复后的状态");gameRole.display();}}
22.7 备忘录模式的注意事项和细节
第23章 19解释器模式(Interpreter模式)
23.1 四则运算问题
23.2 传统方案解决四则运算问题分析
23.3 解释器模式基本介绍
23.4 解释器模式的原理类图
23.5 解释器模式来实现四则
package com.atguigu.interpreter;import java.util.HashMap;/*** 抽象类表达式,通过HashMap 键值对, 可以获取到变量的值* * @author Administrator**/
public abstract class Expression {// a + b - c// 解释公式和数值, key 就是公式(表达式) 参数[a,b,c], value就是就是具体值// HashMap {a=10, b=20}public abstract int interpreter(HashMap<String, Integer> var);
}
package com.atguigu.interpreter;import java.util.HashMap;/*** 变量的解释器* @author Administrator**/
public class VarExpression extends Expression {private String key; // key=a,key=b,key=cpublic VarExpression(String key) {this.key = key;}// var 就是{a=10, b=20}// interpreter 根据 变量名称,返回对应值@Overridepublic int interpreter(HashMap<String, Integer> var) {return var.get(this.key);}
}
package com.atguigu.interpreter;import java.util.HashMap;/*** 抽象运算符号解析器 这里,每个运算符号,都只和自己左右两个数字有关系,* 但左右两个数字有可能也是一个解析的结果,无论何种类型,都是Expression类的实现类* * @author Administrator**/
public class SymbolExpression extends Expression {protected Expression left;protected Expression right;public SymbolExpression(Expression left, Expression right) {this.left = left;this.right = right;}//因为 SymbolExpression 是让其子类来实现,因此 interpreter 是一个默认实现@Overridepublic int interpreter(HashMap<String, Integer> var) {return 0;}
}
package com.atguigu.interpreter;import java.util.HashMap;/*** 加法解释器* @author Administrator**/
public class AddExpression extends SymbolExpression {public AddExpression(Expression left, Expression right) {super(left, right);}//处理相加//var 仍然是 {a=10,b=20}..//一会我们debug 源码,就okpublic int interpreter(HashMap<String, Integer> var) {//super.left.interpreter(var) : 返回 left 表达式对应的值 a = 10//super.right.interpreter(var): 返回right 表达式对应值 b = 20return super.left.interpreter(var) + super.right.interpreter(var);}
}
package com.atguigu.interpreter;import java.util.HashMap;public class SubExpression extends SymbolExpression {public SubExpression(Expression left, Expression right) {super(left, right);}//求出left 和 right 表达式相减后的结果public int interpreter(HashMap<String, Integer> var) {return super.left.interpreter(var) - super.right.interpreter(var);}
}
package com.atguigu.interpreter;import java.util.HashMap;
import java.util.Stack;public class Calculator {// 定义表达式private Expression expression;// 构造函数传参,并解析public Calculator(String expStr) { // expStr = a+b// 安排运算先后顺序Stack<Expression> stack = new Stack<>();// 表达式拆分成字符数组 char[] charArray = expStr.toCharArray();// [a, +, b]Expression left = null;Expression right = null;//遍历我们的字符数组, 即遍历 [a, +, b]//针对不同的情况,做处理for (int i = 0; i < charArray.length; i++) {switch (charArray[i]) {case '+': //left = stack.pop();// 从stack取出left => "a"right = new VarExpression(String.valueOf(charArray[++i]));// 取出右表达式 "b"stack.push(new AddExpression(left, right));// 然后根据得到left 和 right 构建 AddExpresson加入stackbreak;case '-': // left = stack.pop();right = new VarExpression(String.valueOf(charArray[++i]));stack.push(new SubExpression(left, right));break;default: //如果是一个 Var 就创建要给 VarExpression 对象,并push到 stackstack.push(new VarExpression(String.valueOf(charArray[i])));break;}}//当遍历完整个 charArray 数组后,stack 就得到最后Expressionthis.expression = stack.pop();}public int run(HashMap<String, Integer> var) {//最后将表达式a+b和 var = {a=10,b=20}//然后传递给expression的interpreter进行解释执行return this.expression.interpreter(var);}
}
客户端
package com.atguigu.interpreter;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;public class ClientTest {public static void main(String[] args) throws IOException {String expStr = getExpStr(); // a+bHashMap<String, Integer> var = getValue(expStr);// var {a=10, b=20}Calculator calculator = new Calculator(expStr);System.out.println("运算结果:" + expStr + "=" + calculator.run(var));}// 获得表达式public static String getExpStr() throws IOException {System.out.print("请输入表达式:");return (new BufferedReader(new InputStreamReader(System.in))).readLine();}// 获得值映射public static HashMap<String, Integer> getValue(String expStr) throws IOException {HashMap<String, Integer> map = new HashMap<>();for (char ch : expStr.toCharArray()) {if (ch != '+' && ch != '-') {if (!map.containsKey(String.valueOf(ch))) {System.out.print("请输入" + String.valueOf(ch) + "的值:");String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();map.put(String.valueOf(ch), Integer.valueOf(in));}}}return map;}
}
23.6 解释器模式在Spring框架应用的源码分析★
package com.atguigu.spring.test;import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;public class Interpreter {public static void main(String[] args) {//创建一个 Parser 对象SpelExpressionParser parser = new SpelExpressionParser();////通过 Parser 对象 获取到一个Expression对象//会根据不同的 Parser 对象 ,返回不同的 Expression对象Expression expression = parser.parseExpression("10 * (2 + 1) * 1 + 66"); //96int result = (Integer) expression.getValue();System.out.println(result);}}
23.7 解释器模式的注意事项和细节
第24章 20状态模式(State)
24.1 APP抽奖活动问题
24.2 状态模式基本介绍
24.3 状态模式的原理类图
24.4 状态模式解决APP抽奖问题
package com.atguigu.state;/*** 状态抽象类* @author Administrator**/
public abstract class State {// 扣除积分 - 50public abstract void deductMoney();// 是否抽中奖品public abstract boolean raffle();// 发放奖品public abstract void dispensePrize();}
package com.atguigu.state;/*** 不能抽奖状态* @author Administrator**/
public class NoRaffleState extends State {// 初始化时传入活动引用,扣除积分后改变其状态RaffleActivity activity;public NoRaffleState(RaffleActivity activity) {this.activity = activity;}// 当前状态可以扣积分 , 扣除后,将状态设置成可以抽奖状态@Overridepublic void deductMoney() {System.out.println("扣除50积分成功,您可以抽奖了");activity.setState(activity.getCanRaffleState());}// 当前状态不能抽奖@Overridepublic boolean raffle() {System.out.println("扣了积分才能抽奖喔!");return false;}// 当前状态不能发奖品@Overridepublic void dispensePrize() {System.out.println("不能发放奖品");}
}
package com.atguigu.state;import java.util.Random;/*** 可以抽奖的状态* @author Administrator**/
public class CanRaffleState extends State {RaffleActivity activity;public CanRaffleState(RaffleActivity activity) {this.activity = activity;}//已经扣除了积分,不能再扣@Overridepublic void deductMoney() {System.out.println("已经扣取过了积分");}//可以抽奖, 抽完奖后,根据实际情况,改成新的状态@Overridepublic boolean raffle() {System.out.println("正在抽奖,请稍等!");Random r = new Random();int num = r.nextInt(10);// 10%中奖机会if(num == 0){// 改变活动状态为发放奖品 contextactivity.setState(activity.getDispenseState());return true;}else{System.out.println("很遗憾没有抽中奖品!");// 改变状态为不能抽奖activity.setState(activity.getNoRafflleState());return false;}}// 不能发放奖品@Overridepublic void dispensePrize() {System.out.println("没中奖,不能发放奖品");}
}
package com.atguigu.state;/*** 发放奖品的状态* @author Administrator**/
public class DispenseState extends State {// 初始化时传入活动引用,发放奖品后改变其状态RaffleActivity activity;public DispenseState(RaffleActivity activity) {this.activity = activity;}//@Overridepublic void deductMoney() {System.out.println("不能扣除积分");}@Overridepublic boolean raffle() {System.out.println("不能抽奖");return false;}//发放奖品@Overridepublic void dispensePrize() {if(activity.getCount() > 0){System.out.println("恭喜中奖了");// 改变状态为不能抽奖activity.setState(activity.getNoRafflleState());}else{System.out.println("很遗憾,奖品发送完了");// 改变状态为奖品发送完毕, 后面我们就不可以抽奖activity.setState(activity.getDispensOutState());//System.out.println("抽奖活动结束");//System.exit(0);}}
}
package com.atguigu.state;/*** 奖品发放完毕状态* 说明,当我们activity 改变成 DispenseOutState, 抽奖活动结束* @author Administrator**/
public class DispenseOutState extends State {// 初始化时传入活动引用RaffleActivity activity;public DispenseOutState(RaffleActivity activity) {this.activity = activity;}@Overridepublic void deductMoney() {System.out.println("奖品发送完了,请下次再参加");}@Overridepublic boolean raffle() {System.out.println("奖品发送完了,请下次再参加");return false;}@Overridepublic void dispensePrize() {System.out.println("奖品发送完了,请下次再参加");}
}
活动
package com.atguigu.state;/*** 抽奖活动 //* * @author Administrator**/
public class RaffleActivity {// state 表示活动当前的状态,是变化的State state = null;// 奖品数量int count = 0;// 四个属性,表示四种状态State noRafflleState = new NoRaffleState(this);State canRaffleState = new CanRaffleState(this);State dispenseState = new DispenseState(this);State dispensOutState = new DispenseOutState(this);//构造器//1. 初始化当前的状态为 noRafflleState(即不能抽奖的状态)//2. 初始化奖品的数量 public RaffleActivity( int count) {this.state = getNoRafflleState();this.count = count;}//扣分, 调用当前状态的 deductMoneypublic void debuctMoney(){state.deductMoney();}//抽奖 public void raffle(){// 如果当前的状态是抽奖成功if(state.raffle()){//领取奖品state.dispensePrize();}}public State getState() {return state;}public void setState(State state) {this.state = state;}//这里请大家注意,每领取一次奖品,count--public int getCount() {int curCount = count; count--;return curCount;}public void setCount(int count) {this.count = count;}public State getNoRafflleState() {return noRafflleState;}public void setNoRafflleState(State noRafflleState) {this.noRafflleState = noRafflleState;}public State getCanRaffleState() {return canRaffleState;}public void setCanRaffleState(State canRaffleState) {this.canRaffleState = canRaffleState;}public State getDispenseState() {return dispenseState;}public void setDispenseState(State dispenseState) {this.dispenseState = dispenseState;}public State getDispensOutState() {return dispensOutState;}public void setDispensOutState(State dispensOutState) {this.dispensOutState = dispensOutState;}
}
客户端
package com.atguigu.state;/*** 状态模式测试类* @author Administrator**/
public class ClientTest {public static void main(String[] args) {// 创建活动对象,奖品有1个奖品RaffleActivity activity = new RaffleActivity(1);// 我们连续抽300次奖for (int i = 0; i < 30; i++) {System.out.println("--------第" + (i + 1) + "次抽奖----------");// 参加抽奖,第一步点击扣除积分activity.debuctMoney();// 第二步抽奖activity.raffle();}}}
24.5 状态模式在实际项目-借贷平台 源码剖析★
package com.atguigu.state.money;/*** 状态接口* @author Administrator**/
public interface State {/*** 电审*/void checkEvent(Context context);/*** 电审失败*/void checkFailEvent(Context context);/*** 定价发布*/void makePriceEvent(Context context);/*** 接单*/void acceptOrderEvent(Context context);/*** 无人接单失效*/void notPeopleAcceptEvent(Context context);/*** 付款*/void payOrderEvent(Context context);/*** 接单有人支付失效*/void orderFailureEvent(Context context);/*** 反馈*/void feedBackEvent(Context context);String getCurrentState();
}
package com.atguigu.state.money;public abstract class AbstractState implements State {protected static final RuntimeException EXCEPTION = new RuntimeException("操作流程不允许");//抽象类,默认实现了 State 接口的所有方法//该类的所有方法,其子类(具体的状态类),可以有选择的进行重写@Overridepublic void checkEvent(Context context) {throw EXCEPTION;}@Overridepublic void checkFailEvent(Context context) {throw EXCEPTION;}@Overridepublic void makePriceEvent(Context context) {throw EXCEPTION;}@Overridepublic void acceptOrderEvent(Context context) {throw EXCEPTION;}@Overridepublic void notPeopleAcceptEvent(Context context) {throw EXCEPTION;}@Overridepublic void payOrderEvent(Context context) {throw EXCEPTION;}@Overridepublic void orderFailureEvent(Context context) {throw EXCEPTION;}@Overridepublic void feedBackEvent(Context context) {throw EXCEPTION;}
}
AllState
package com.atguigu.state.money;//各种具体状态类
class FeedBackState extends AbstractState {@Overridepublic String getCurrentState() {return StateEnum.FEED_BACKED.getValue();}
}class GenerateState extends AbstractState {@Overridepublic void checkEvent(Context context) {context.setState(new ReviewState());}@Overridepublic void checkFailEvent(Context context) {context.setState(new FeedBackState());}@Overridepublic String getCurrentState() {return StateEnum.GENERATE.getValue();}
}class NotPayState extends AbstractState {@Overridepublic void payOrderEvent(Context context) {context.setState(new PaidState());}@Overridepublic void feedBackEvent(Context context) {context.setState(new FeedBackState());}@Overridepublic String getCurrentState() {return StateEnum.NOT_PAY.getValue();}
}class PaidState extends AbstractState {@Overridepublic void feedBackEvent(Context context) {context.setState(new FeedBackState());}@Overridepublic String getCurrentState() {return StateEnum.PAID.getValue();}
}class PublishState extends AbstractState {@Overridepublic void acceptOrderEvent(Context context) {//把当前状态设置为 NotPayState。。。//至于应该变成哪个状态,由流程图来决定context.setState(new NotPayState());}@Overridepublic void notPeopleAcceptEvent(Context context) {context.setState(new FeedBackState());}@Overridepublic String getCurrentState() {return StateEnum.PUBLISHED.getValue();}
}class ReviewState extends AbstractState {@Overridepublic void makePriceEvent(Context context) {context.setState(new PublishState());}@Overridepublic String getCurrentState() {return StateEnum.REVIEWED.getValue();}}
package com.atguigu.state.money;/*** 状态枚举类* @author Administrator**/
public enum StateEnum {//订单生成GENERATE(1, "GENERATE"),//已审核REVIEWED(2, "REVIEWED"),//已发布PUBLISHED(3, "PUBLISHED"),//待付款NOT_PAY(4, "NOT_PAY"),//已付款PAID(5, "PAID"),//已完结FEED_BACKED(6, "FEED_BACKED");private int key;private String value;StateEnum(int key, String value) {this.key = key;this.value = value;}public int getKey() {return key;}public String getValue() {return value;}
}
package com.atguigu.state.money;//环境上下文
public class Context extends AbstractState{//当前的状态 state, 根据我们的业务流程处理,不停的变化private State state;@Overridepublic void checkEvent(Context context) {state.checkEvent(this);getCurrentState();}@Overridepublic void checkFailEvent(Context context) {state.checkFailEvent(this);getCurrentState();}@Overridepublic void makePriceEvent(Context context) {state.makePriceEvent(this);getCurrentState();}@Overridepublic void acceptOrderEvent(Context context) {state.acceptOrderEvent(this);getCurrentState();}@Overridepublic void notPeopleAcceptEvent(Context context) {state.notPeopleAcceptEvent(this);getCurrentState();}@Overridepublic void payOrderEvent(Context context) {state.payOrderEvent(this);getCurrentState();}@Overridepublic void orderFailureEvent(Context context) {state.orderFailureEvent(this);getCurrentState();}@Overridepublic void feedBackEvent(Context context) {state.feedBackEvent(this);getCurrentState();}public State getState() {return state;}public void setState(State state) {this.state = state;}@Overridepublic String getCurrentState() {System.out.println("当前状态 : " + state.getCurrentState());return state.getCurrentState();}
}
客户端
package com.atguigu.state.money;/**测试类*/
public class ClientTest {public static void main(String[] args) {//创建context 对象Context context = new Context();//将当前状态设置为 PublishStatecontext.setState(new PublishState());System.out.println(context.getCurrentState());// //publish --> not paycontext.acceptOrderEvent(context);
// //not pay --> paidcontext.payOrderEvent(context);
// // 失败, 检测失败时,会抛出异常
// try {
// context.checkFailEvent(context);
// System.out.println("流程正常..");
// } catch (Exception e) {
// // TODO: handle exception
// System.out.println(e.getMessage());
// }}}
24.6 状态模式的注意事项和细节
第25章 21策略模式(Strategy)
25.1 鸭子问题
25.2 传统方案解决鸭子问题
package com.atguigu.strategy;public abstract class Duck {public Duck() {}public abstract void display();//显示鸭子信息public void quack() {System.out.println("鸭子嘎嘎叫~~");}public void swim() {System.out.println("鸭子会游泳~~");}public void fly() {System.out.println("鸭子会飞翔~~~");}}
package com.atguigu.strategy;public class WildDuck extends Duck {@Overridepublic void display() {System.out.println(" 这是野鸭 ");}}
package com.atguigu.strategy;public class PekingDuck extends Duck {@Overridepublic void display() {System.out.println("~~北京鸭~~~");}//因为北京鸭不能飞翔,因此需要重写fly@Overridepublic void fly() {System.out.println("北京鸭不能飞翔");}}
package com.atguigu.strategy;public class ToyDuck extends Duck{@Overridepublic void display() {System.out.println("玩具鸭");}//需要重写父类的所有方法public void quack() {System.out.println("玩具鸭不能叫~~");}public void swim() {System.out.println("玩具鸭不会游泳~~");}public void fly() {System.out.println("玩具鸭不会飞翔~~~");}
}
25.3 传统方式解决鸭子问题分析和解决方案
25.4 策略模式基本介绍
25.5 策略模式的原理类图
25.6 策略模式解决鸭子问题
package com.atguigu.strategy.improve;public abstract class Duck {//属性, 策略接口FlyBehavior flyBehavior;//其它属性<->策略接口QuackBehavior quackBehavior;public Duck() {}public abstract void display();//显示鸭子信息public void quack() {System.out.println("鸭子嘎嘎叫~~");}public void swim() {System.out.println("鸭子会游泳~~");}public void fly() {//改进if(flyBehavior != null) {flyBehavior.fly();}}public void setFlyBehavior(FlyBehavior flyBehavior) {this.flyBehavior = flyBehavior;}public void setQuackBehavior(QuackBehavior quackBehavior) {this.quackBehavior = quackBehavior;}}
飞行策略
package com.atguigu.strategy.improve;public interface FlyBehavior {void fly(); // 子类具体实现
}
package com.atguigu.strategy.improve;public class GoodFlyBehavior implements FlyBehavior {@Overridepublic void fly() {System.out.println(" 飞翔技术高超 ~~~");}}
package com.atguigu.strategy.improve;public class BadFlyBehavior implements FlyBehavior {@Overridepublic void fly() {System.out.println(" 飞翔技术一般 ");}}
package com.atguigu.strategy.improve;public class NoFlyBehavior implements FlyBehavior{@Overridepublic void fly() {System.out.println(" 不会飞翔 ");}}
鸭子种类
package com.atguigu.strategy.improve;public class WildDuck extends Duck {//构造器,传入FlyBehavor 的对象public WildDuck() {flyBehavior = new GoodFlyBehavior();}@Overridepublic void display() {System.out.println(" 这是野鸭 ");}}
package com.atguigu.strategy.improve;public class PekingDuck extends Duck {//假如北京鸭可以飞翔,但是飞翔技术一般public PekingDuck() {flyBehavior = new BadFlyBehavior();}@Overridepublic void display() {System.out.println("~~北京鸭~~~");}}
package com.atguigu.strategy.improve;public class ToyDuck extends Duck{public ToyDuck() {flyBehavior = new NoFlyBehavior();}@Overridepublic void display() {System.out.println("玩具鸭");}//需要重写父类的所有方法public void quack() {System.out.println("玩具鸭不能叫~~");}public void swim() {System.out.println("玩具鸭不会游泳~~");}}
客户端
package com.atguigu.strategy.improve;public class Client {public static void main(String[] args) {WildDuck wildDuck = new WildDuck();wildDuck.fly();//ToyDuck toyDuck = new ToyDuck();toyDuck.fly();PekingDuck pekingDuck = new PekingDuck();pekingDuck.fly();//动态改变某个对象的行为, 北京鸭 不能飞pekingDuck.setFlyBehavior(new NoFlyBehavior());System.out.println("北京鸭的实际飞翔能力");pekingDuck.fly();}}
叫声策略
package com.atguigu.strategy.improve;public interface QuackBehavior {void quack();//子类实现
}
25.7 策略模式在JDK-Arrays应用的源码分析★
package com.atguigu.jdk;import java.util.Arrays;
import java.util.Comparator;public class Strategy {public static void main(String[] args) {//数组Integer[] data = { 9, 1, 2, 8, 4, 3 };// 实现升序排序,返回-1放左边,1放右边,0保持不变// 说明// 1. 实现了Comparator 接口(策略接口),匿名类 对象 new Comparator<Integer<(){..}// 2. 对象 new Comparator<Integer>(){..} 就是实现了策略接口 的对象// 3. public int compare(Integer o1, Integer o2){} ָ指定具体的处理方式Comparator<Integer> comparator = new Comparator<Integer>() {public int compare(Integer o1, Integer o2) {if (o1 > o2) {return -1;} else {return 1;}};};// 说明/** public static <T> void sort(T[] a, Comparator<? super T> c) {if (c == null) {sort(a); //默认方法} else { if (LegacyMergeSort.userRequested)legacyMergeSort(a, c); //使用策略对象celse// 使用策略对象cTimSort.sort(a, 0, a.length, c, null, 0, 0);}}*///方式1Arrays.sort(data, comparator);System.out.println(Arrays.toString(data)); // 降序排序//方式2- 通过lambda 表达式实现 策略模式Integer[] data2 = { 19, 11, 12, 18, 14, 13 };Arrays.sort(data2, (var1, var2) -> {if(var1.compareTo(var2) > 0) {return -1;} else {return 1;}});System.out.println("data2=" + Arrays.toString(data2));}}
25.8 策略模式的注意事项和细节
第26章 22职责链模式(责任链模式,Chain of Responsibility)
26.1 OA系统采购审批需求
26.2 传统方案解决OA系统审批
26.3 传统方案解决OA系统审批问题分析
26.4 职责链模式基本介绍
26.5 职责链模式的原理类图
26.6 职责链模式解决OA系统采购审批
package com.atguigu.responsibilitychain;//请求类
public class PurchaseRequest {private int type = 0; //请求类型private float price = 0.0f; //请求金额private int id = 0;//构造器public PurchaseRequest(int type, float price, int id) {this.type = type;this.price = price;this.id = id;}public int getType() {return type;}public float getPrice() {return price;}public int getId() {return id;}}
package com.atguigu.responsibilitychain;public abstract class Approver {Approver approver; //下一个处理者String name; // 名字public Approver(String name) {this.name = name;}//下一个处理者public void setApprover(Approver approver) {this.approver = approver;}//处理审批请求的方法,得到一个请求, 处理是子类完成,因此该方法做成抽象public abstract void processRequest(PurchaseRequest purchaseRequest);}
package com.atguigu.responsibilitychain;public class DepartmentApprover extends Approver {public DepartmentApprover(String name) {super(name);}@Overridepublic void processRequest(PurchaseRequest purchaseRequest) {if(purchaseRequest.getPrice() <= 5000) {System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");}else {approver.processRequest(purchaseRequest);}}}
package com.atguigu.responsibilitychain;public class CollegeApprover extends Approver {public CollegeApprover(String name) {super(name);}@Overridepublic void processRequest(PurchaseRequest purchaseRequest) {if(purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000) {System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");}else {approver.processRequest(purchaseRequest);}}
}
package com.atguigu.responsibilitychain;public class ViceSchoolMasterApprover extends Approver {public ViceSchoolMasterApprover(String name) {super(name);}@Overridepublic void processRequest(PurchaseRequest purchaseRequest) {if(purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 30000) {System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");}else {approver.processRequest(purchaseRequest);}}
}
package com.atguigu.responsibilitychain;public class SchoolMasterApprover extends Approver {public SchoolMasterApprover(String name) {super(name);}@Overridepublic void processRequest(PurchaseRequest purchaseRequest) {if(purchaseRequest.getPrice() > 30000) {System.out.println(" 请求编号 id= " + purchaseRequest.getId() + " 被 " + this.name + " 处理");}else {approver.processRequest(purchaseRequest);}}
}
客户端
package com.atguigu.responsibilitychain;public class Client {public static void main(String[] args) {//创建一个请求PurchaseRequest purchaseRequest = new PurchaseRequest(1, 31000, 1);//创建相关的审批人DepartmentApprover departmentApprover = new DepartmentApprover("张主任");CollegeApprover collegeApprover = new CollegeApprover("李院长");ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副校");SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("佟校长");//需要将各个审批级别的下一个设置好 (处理人构成环形: )departmentApprover.setApprover(collegeApprover);collegeApprover.setApprover(viceSchoolMasterApprover);viceSchoolMasterApprover.setApprover(schoolMasterApprover);schoolMasterApprover.setApprover(departmentApprover);departmentApprover.processRequest(purchaseRequest);viceSchoolMasterApprover.processRequest(purchaseRequest);}}
26.7 职责链模式在SpringMVC框架应用的源码分析★
preHandle
postHandle
afterCompletion
package com.atguigu.spring.test;import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerInterceptor;public class ResponsibilityChain {public static void main(String[] args) {// DispatcherServlet //说明/** * protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {* HandlerExecutionChain mappedHandler = null; * mappedHandler = getHandler(processedRequest);//获取到HandlerExecutionChain对象* //在 mappedHandler.applyPreHandle 内部 得到啦 HandlerInterceptor interceptor* //调用了拦截器的 interceptor.preHandle* if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}//说明:mappedHandler.applyPostHandle 方法内部获取到拦截器,并调用 //拦截器的 interceptor.postHandle(request, response, this.handler, mv);mappedHandler.applyPostHandle(processedRequest, response, mv);* }* * * //说明:在 mappedHandler.applyPreHandle内部中,* 还调用了 triggerAfterCompletion 方法,该方法中调用了 * HandlerInterceptor interceptor = getInterceptors()[i];try {interceptor.afterCompletion(request, response, this.handler, ex);}catch (Throwable ex2) {logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);}*/}}