SpringCloud (a, SpringCloud basis)

table of Contents:

  • Observer Pattern
  • Proxy mode

Observer Pattern:

java implements its own observer pattern >>> java.util.Observable;

1, public synchronized void addObserver (Observer o); add an observer

2, public synchronized void deleteObserver (Observer o); delete an observer

3, protected synchronized void setChanged (); modification notification tag, so that the object can be viewed by an observer changes remember that

4, public void notifyObservers (Object arg); notification observer, it is observed has changed

. 1  public  class ObserverDemo {
 2  
. 3      public  static  void main (String [] args) {
 . 4          // observed object 
. 5          MyObservable OBS = new new MyObservable ();
 . 6  
. 7          // Add the observer 
. 8          obs.addObserver ((O, Arg ) -> System.out.println ( "service list changed. 1:" + Arg));
 . 9  
10          the Observer O = (O1, Arg) -> System.out.println ( "service list changed 2:" + Arg);
 . 11          obs.addObserver (O);
 12 is  
13 is          obs.setServerList ( "192.168.110.1" );
 14  
15         obs.deleteObserver(o);
16         obs.setServerList("192.168.110.2");
17     }
18 }
19 
20 class MyObservable extends Observable {
21 
22     public void setServerList(String serverList) {
23         this.setChanged();
24         this.notifyObservers(serverList);
25     }
26 }

 Proxy mode:

1, the static agent

The agency is in addition to the task itself needs to be done outside of the core business of an agent to do, such as advertising and other stars will take things to the broker.

 1 public class Client {
 2     public static void main(String[] args) {
 3         Teacher teacher = new ProxyDemo(new MathTeacher());
 4         teacher.teach();
 5     }
 6 }
 7 
 8 class ProxyDemo implements Teacher {
 9     private Teacher teacher;
10 
11     public ProxyDemo(Teacher teacher) {
12         this.teacher = teacher;
13     }
14 
15     @Override
16     public void teach() {
17         before();
18         teacher.teach();
19         after();
20     }
21 
22     public void before() {
23         System.err.println("准备资料。。。");
24     }
25 
26     public void after() {
27         System.err.println("布置作业。。。");
28     }
29 }
30 
31 interface Teacher {
32     void teach();
33 }
34 
35 class MathTeacher implements Teacher {
36     @Override
37     public void teach() {
38         System.err.println("上数学课");
39     }
40 }

The disadvantage is that if the static agent need to proxy an object, then the code needs to get heavy again, this is very tedious and repetitive, so there will be a dynamic proxy.

2, dynamic proxy

 1 public class Client {
 2     public static void main(String[] args) {
 3         Dynamic dynamic = new Dynamic(new MathTeacher());
 4         Teacher mathTeacher = (Teacher) dynamic.getProxyInstance();
 5         mathTeacher.teach();
 6 
 7         System.err.println("-----------------------");
 8 
 9         Dynamic dynamic2 = new Dynamic(new EnglishTeacher());
10         Teacher mathTeacher2 = (Teacher) dynamic2.getProxyInstance();
11         mathTeacher2.teach();
12     }
13 }
14 
15 class Dynamic implements InvocationHandler {
16     private Object target;
17 
18     public Dynamic(Object target) {
19         this.target = target;
20     }
21 
22     public void before() {
23         System.out.println("准备资料。。。");
24     }
25 
26     public void after() {
27         System.out.println("布置作业。。。");
28     }
29 
30     @Override
31     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
32         before();
33         Object result = method.invoke(target, args);
34         after();
35         return result;
36     }
37 
38     public Object getProxyInstance() {
39         return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
40     }
41 }
42 
43 interface Teacher {
44     void teach();
45 }
46 
47 class MathTeacher implements Teacher {
48     @Override
49     public void teach() {
50         System.out.println("上数学课");
51     }
52 }
53 
54 class EnglishTeacher implements Teacher {
55     @Override
56     public voidTeach () {
 57 is          System.out.println ( "English class" );
 58      }
 59 }

 

Guess you like

Origin www.cnblogs.com/bzfsdr/p/11587815.html