命令模式
命令模式,将一个请求封装成对象,使得请求发送者和请求接受者之间相互隔离,消除了两者之间的耦合。将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化
角色分配
- 抽象命令类
- 具体命令类
- 调用者(调用具体命令)
- 接收者(根据请求执行具体相关操作)
- 客户(使用)
命令模式的实现
1.抽象命令类
public abstract class Command {
protected Receiver receiver;
public Command(Receiver receiver) {
this.receiver = receiver;
}
public abstract void Execute();
}
2.具体命令类
public class ConcreteCommand extends Command {
public ConcreteCommand(Receiver receiver) {
super(receiver);
}
public void Execute() {
receiver.Action();
}
}
3.调用者
public class Invoker {
private Command command;
public Invoker(Command command) {
this.command = command;
}
public void ExecuteCommand() {
command.Execute();
}
}
4.接收者,接受到请求知道如何实施与执行一个与请求相关的操作
public class Receiver {
public void Action() {
System.out.println("执行请求");
}
}
5.使用
Receiver receiver = new Receiver();//将请求封装成一个对象
Command cmd = new ConcreteCommand(receiver);
Invoker invoker = new Invoker(cmd);
invoker.ExecuteCommand();
迭代器模式
迭代器模式,提供一种方法顺序的访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。
优缺点
优点:
- 支持不同方式遍历聚合对象,且可以有多个遍历
- 迭代器简化了聚合类,增加新的聚合类与迭代器无需修改原代码
缺点:
- 增加新的聚合类需要增加对应的迭代器类,类的个数增加,系统变复杂
角色分配
- 抽象聚合类、抽象迭代器
- 具体聚合类、具体迭代器(在具体聚合类中实现)
- 客户端(使用)
迭代器模式的实现
1.抽象聚合类
public interface Television{
public TVIterator createIterator();
}
2.抽象迭代器
public interface TVIterator {
public void setChannel(int i);//设置初始频道
public void next();//下一个频道
public boolean isLast();//是否为最后一个频道
}
3.具体聚合类与具体迭代器
public class TCLTelevision implements Television {
private Object[] obj = {"CCTV1","CCTV2","CCTV3","CCTV4","CCTV5","CCTV6","CCTV7","CCTV8","CCTV9","CCTV10","CCTV11","CCTV12","CCTV13","CCTVnews"};
public TVIterator createIterator() {
return new TCLIterator();
}
//具体迭代器
class TCLIterator implements TVIterator{
//当前频道
private int currentIndex = 0;
public void setChannel(int i) {
this.currentIndex = i;
}
public void next() {
if(currentIndex<obj.length){
currentIndex++;
}
}
public boolean isLast() {
if(currentIndex<14){
return false;
} else {
return true;
}
}
}
}
4.客户端(使用)
public class Client {
public static void display(Television tv){
TVIterator tvIterator = tv.createIterator();
System.out.println("正向输出节目表:");
while(!tvIterator.isLast()){
System.out.println(tvIterator.currentChannel().toString());
tvIterator.next();
}
}
public static void main(String[] args){
Television tv;
tv = new TCLTelevision();
display(tv);
System.out.println("-----------------------------");
reverseDisplay(tv);
}
}
观察者模式
观察者模式,在对象之间定义了一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象会收到通知并自动更新。
适用场景
订阅,被订阅者发布消息,订阅者都可以收到推送
角色分配
- 抽象被观察者
- 抽象观察者
- 具体被观察者
- 具体观察者
观察者模式的实现
1.抽象被观察者
public interface Observerable {
public void add(Observer o);//添加
public void notify();//通知
}
2.抽象观察者
public interface Observer {
public void update(String message);
}
3.具体被观察者
public class Server implements Observerable{
private List<Observer> list;
private String message;
public Server () {
list = new ArrayList<Observer>();
}
public void add(Observer o) {
list.add(o);
}
//遍历
public void notify() {
for(int i = 0; i < list.size(); i++) {
Observer oserver = list.get(i);
oserver.update(message);
}
}
public void setInfomation(String s) {
this.message = s;
System.out.println("推送消息: " + s);
//消息更新,通知所有观察者
notify();
}
}
4.具体观察者
public class User implements Observer {
private String name;
private String message;
public User(String name) {
this.name = name;
}
public void update(String message) {
this.message = message;
read();
}
public void read() {
System.out.println(name + " 收到推送消息: " + message);
}
}
5.使用
Server server = new Server ();
Observer userZhang = new User("ZhangSan");
Observer userLi = new User("LiSi");
server.add(userZhang);
server.add(userLi);
server.setInfomation("欢迎订阅本公众号!");//推送消息
/*Out:
推送消息:欢迎订阅本公众号!
ZhangSan 收到推送消息:欢迎订阅本公众号!
LiSi 收到推送消息:欢迎订阅本公众号!
*/
策略模式
策略模式,实现某一个功能有多种算法或者策略,我们可以根据应用场景的不同选择不同的算法或者策略来完成该功能。
优缺点
优点:
- 可以动态的改变对象的行为
缺点:
- 客户端必须知道所有的策略类,来决定使用哪一个
- 策略模式将产生许多策略类
角色分配
- 抽象策略类
- 具体策略类
- 环境类(使用具体策略的角色)
策略模式的实现
1.抽象策略类
public interface Stragety{//电影票类型策略
public void type();
}
2.具体策略类
(1)2D电影票
public class TwoD implements Stragety{
public void type(){
System.out.println("您购买的是 2D 电影票...");
}
}
(3)3D电影票
public class ThreeD implements Stragety{
public void type(){
System.out.println("您购买的是 3D 电影票...");
}
}
3.环境类
public class Customer{
private String name;
private Stragety stragety;
public Customer(String name,Stragety stragety){
this.name = name;
this.stragety = stragety;
}
public void watching(){
System.out.print(name + " ");
stragety.type();
}
}
4.使用
TwoD twod = new TwoD();
ThreeD threed = new ThreeD();
Customer c1 = new Customer("小明",twod);
Customer c2 = new Customer("小华",threed );
/*Out:
小明 您购买的是 2D 电影票...
小华 您购买的是 3D 电影票...
*/
😀
∠( ᐛ 」∠)_