tailieunhanh - head first design patterns phần 8

Được rồi, chúng tôi đã nhìn thấy rằng đại diện cho nước giải khát của chúng tôi cộng với chương trình giá cả gia vị với thừa kế đã không làm việc rất tốt - chúng tôi nhận được vụ nổ lớp học, thiết kế cứng nhắc, hoặc chúng tôi có thêm chức năng để các lớp cơ sở là không thích hợp cho một số các lớp con. | the proxy pattern This is a pretty slick idea. We re going to write some code that takes a method invocation somehow transfers it over the network and invokes the same method on a remote object. Then I presume when the call is complete the result gets sent back over the network to our client. But it seems to me X. this code is going to be very ---------- tricky to write. y Hold on now we aren t going to write that code ourselves it s pretty much built into Java s remote invocation functionality. All we have to do is retrofit our code so that it takes advantage of RMI. i Before going further think about how you d design a system to enable remote method invocation. How would you make it easy on the developer so that she has to write as little code as possible How would you make the remote invocation look seamless Should making remote calls be totally transparent Is that a good idea What might be a problem with that approach you are here 435 RMI detour Adding a remote proxy to the rumball Machine monitoring code On paper this looks good but how do we create a proxy that knows how to invoke a method on an object that lives in another JVM Hmmm. Well you can t get a reference to something on another heap right In other words you can t say Duck d object in another heap Whatever the variable d is referencing must be in the same heap space as the code running the statement. So how do we approach this Well that s where Java s Remote Method Invocation comes in. RMI gives US a way to find objects in a remote JVM and allows US to invoke their methods. You may have encountered RMI in Head First Java if not we re going to take a slight detour and come up to speed on RMI before adtling the proxy support to the Gumball Machine code. So. here s what we re going to do 0 First we re going to take the RMI Detour and check RMI out. Even if you are familiar with RM I you might want to follow along and check out the scenery. 0 Then we re going to take our GumballMachine and make it a .