preface

These two days review OKHttp source code, saw some design patterns, usually write business logic more or less will use, a simple record ^^–^^

Both the observer pattern and the chain of responsibility pattern belong to the behavior class design pattern. They seem to be the same, but the trigger mechanism has similar advantages. Both have a trigger chain, which is called when the observer is notified, and the next handler is called in the chain of responsibility pattern.

directory

Observer mode

1. Observer mode

Everything is done, legalism, advocated to establish the rule of law society, implement the system of heavy fines, and everything is done are students of xunzi, lis Reese is a captain in the state of qin, and everything is done is South Korea’s heavyweight, Reese is committed to the national unification, and arranged the spy to everything is done, everything is done, Reese is like the palm of his hand. Han Feizi as being monitored, the object of observation, Han Feizi around undercover observation of every move, report to Li Si. This is called the observer pattern in design patterns.

Han Feizi as the observer, through undercover continuous observation

Public void addObserver(Observer) Observable{Observable (Observable) public void addObserver(Observer); Public void deleteObserver(Observer Observer); Public void notifyObservers(String content); }Copy the code

Now, how did Han Feizi do that

pubilc interface IHanFeiZi{ public void eat(); // Eat public void haveFun(); Puiblic class HanFeiZi implements IHanFeiZi,Observable{private ArrayList<Observer> observerList = new ArrayList<>(); Public void addObserver(Observer Observer){observerList.add (Observer); } public void deleteObserver(Observer observer){ this.observerList.remove(observer); } public void notifyObservers(String content){ for(Observer observer:observerList){ observer.update(content); Public void eat(){this.notifyObservers(" Observers ");}} public void eat(){this.notifyObservers(" Observers "); } public void haveFun(){this.notifyObservers(" Observers "); }}Copy the code

Observer Lees

public interface Observer{ public void update(String content); // If someone else moves, Public class LiSi implements Observer{public void update(String content){ this.reportToQinShiHuang(content); } private void reportToQinShiHuang(String content){// }}Copy the code

Maybe when Reese sent an undercover to Han Feizi, someone else might have put an undercover,

Copy the code

Method is called in the following scenario

Observer liSi = new LiSi(); Observer wangSi = new WangSi(); HanFeiZi hanFeiZi = new HanFeiZi(); hanFeiZi.addObserver(liSi); hanFeiZi.addObserver(wangSi); hanFeiZi.eat(); // As soon as you start eating, everyone knowsCopy the code

2. Advantages and disadvantages of observer mode and application scenarios

The observer pattern, also known as the publish/subscribe pattern, is notified and updated whenever an observed object changes state by all objects that depend on it.

The advantage is that there is an abstract coupling between the Observer and the observed, as you can see in the LiSi and HanFeiZi classes mentioned above, and it also follows the single responsibility principle, where each class has a single responsibility, and connects these classes with an Observer and an Observable to form a perfect trigger mechanism.

The downside, of course, is that, as noted above with notifyObservers(), you are about to inform every observer that, if one is astute, the rest aren’t going to work, affecting the overall business logic. The solution is to be asynchronous, which requires other considerations.

Common technologies that use the observer pattern are RxJava, LifeCycler, EventBus, etc. Real-world scenarios include file systems, ATM withdrawals, radio broadcasts, etc.

Second, the responsibility chain model

Multiple objects have the opportunity to process the request

public abstract class Handler{ private Handler nextHandler; Public final Response handleMessage(Request Request){Response Response = null; if(this.getHandlerLevel().equals(request.getRequestLevel())){ response = this.echo(request); }else{// if(this.nexthandler! = null){ response = this.nextHandler.handleMessage(request); } } return response; } public void setNext(Handler handler){ this.nextHandler = handler; } protected abstract Level getHandlerLevel(); protected abstract Response echo(Request request); }Copy the code

This class is used to define and set the next handler, and to define the handleMessage method that is open to the public. The specific processing is as follows

Public class ConcreteHandler1 extends Handler{public class ConcreteHandler1 extends Handler{protected Response echo(Request Request){return null; } protected Level getHandlerLevel(){// handle Level reutrn null; }} ConcreteHandler2Copy the code

call

Handler handler1 = new ConcreteHandler1();
Handler handler2 = new ConcreteHandler2();
handler1.setNext(handler2);
Response response = handler1.handleMessage(new Request());
Copy the code

The advantages of the chain of responsibility pattern is to separate requests and processing, the requester who don’t know this Request is processing, processing of people also do not need to know who is calling, disadvantage is that if there are too many persons responsible, chain calls, like recursion, logic will be complex, add to the Request processing need to traverse all the processing, It’s also not performance friendly. It is recommended to use the chain of responsibility mode in cases where there are few handlers, or to set a threshold for setNext.