Java callback: Additional information

Source: Internet
Author: User

Java callback: Additional information

In what is Hollywood principles, yqj2065 makes code. Add a bit of code here.

1. Callback replaces polling with notification

In the layered architecture, the Client of the Upper-layer module calls the copy () method of the lower-layer module Server. The upper layer does not know the replication progress, but only the lower-layer Server knows the replication progress. The upper layer obtains progress data by means of Round Robin and notification. For the notification code, see the callback and Java 8 lambda expressions.

Round Robin. The lower-layer module Server0 stores the progress data in a member variable x and provides getX (). The Client accesses the data through polling.

In the round robin mode, Server0 in a thread tries to copy, and the main thread is inWhile (true)Query data anytime, anywhere until the replication progress data is 100.

Package principle. callback. sub; public class Server0 {private int x; // progress of the copy operation public int getX () {return x;} public void copy () {while (x <100) {try {Thread. sleep (10); x ++;} catch (InterruptedException e) {}}} package principle. callback; import principle. callback. sub. server0; public class Client0 {private Server0 server = new Server0 (); public void call () {server. copy ();} public static void test () thr Ows InterruptedException {Client0 c = new Client0 (); Runnable r = ()-> c. call (); new Thread (r ). start (); System. out. print ("Progress:"); int x2 = 0; while (true) {int x1 = c. server. getX (); // 10 Thread. sleep (int) (Math. random () * 100); if (x2> = 100) {System. out. println (); break;} if (x2! = X1) {System. out. print (x1 + "%"); x2 = x1 ;}}}}

The following are the results of the two tests.

Progress: 3% 5% 9% 16% 21% 23% 29% 31% 38% 48% 57% 63% 70% 76% 82% 85% 87% 88% 90% 91% 97% 100%

Progress: 1% 6% 12% 16% 24% 31% 39% 47% 49% 54% 61% 64% 67% 77% 83% 90% 100%

Advantages of the Round-Robin method: the upper-layer module can understand the required data anytime and anywhere (without being fooled by the lower-level Module). The dependency and method call relationships are clear. The lower-layer module code is concise. Disadvantages: the superiors keep their eyes on their lower-level workers, which is too difficult; too many calls are executed when data is obtained (especially when network programming, such as remote method calls, is unacceptable );

2. Callback and Polymorphism

When discussing callback and Hollywood principles, they all emphasized their application scenarios: layered architecture. In a non-layered architecture, can these ideas and technologies be used? If it can be used, should the concepts of callback and Hollywood principles be promoted to a more general situation-design between common classes?

The answer is: at the same layer, programmers can still use this technology, which is also a solution.Important Means of Inter-class dependency. It is emphasized that the Application scenario is a layered architecture becauseLayering requires the monotonicity of dependency forcibly, so callback and Hollywood principles have to be used to achieve reverse calling.The programmer has to define the parent IClient of a Client in the lower layer (or infrastructure/framework), which defines the interface for callback (int.

If it is not mandatory to require the monotonicity of dependency, the callback in the same layer is no different from the common polymorphism in the class diagram and its own code.


Of course, if the circular dependency shown in Figure 0-5 appears in the same layer, the programmer can define a B's parent type IB, in this way, the circular dependency between A and B can be changed to one-way dependency. However, it is optional for programmers to change to one-way dependency.

On the other hand, in the same layer, the concepts of callback and Hollywood principles can be used. Observer ModeAs the alias. In other words, Although the observer mode can be used as a synonym for callback and Hollywood principles. However, callback and Hollywood principles are used in layered architecture, while observer mode is used to describe the relationship between classes on the same layer.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.