1 / 132

The Net Worth of an Object-Oriented Pattern

The Net Worth of an Object-Oriented Pattern. Practical Implications of the Java RMI. Adrian German Lecturer, Computer Science Department Indiana University Bloomington. There will be a server, implemented as follows:. class ServerImplementation { }.

arnold
Download Presentation

The Net Worth of an Object-Oriented Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Net Worth of an Object-Oriented Pattern Practical Implications of the Java RMI Adrian German Lecturer, Computer Science Department Indiana University Bloomington

  2. There will be a server, implemented as follows: class ServerImplementation { }

  3. There will be many clients, implemented as follows: class ClientImplementation { }

  4. The server has a business card: public interface Server { }

  5. And it implements it: class ServerImplementation implements Server { }

  6. The client also has a business card: public interface Client { }

  7. And, like the server, it makes a promise to fulfill it: class ClientImplementation implements Client { }

  8. The server keeps track of clients: class ServerImplementation implements Server { Client[] clients = new Client[100]; int index = -1; }

  9. The server keeps track of clients: class ServerImplementation implements Server { Client[] clients = new Client[100]; int index = -1; } The index of the most recently allocated cell is -1 if the array is empty.

  10. The server keeps track of clients: class ServerImplementation implements Server { Client[] clients = new Client[100]; int index = -1; } The index of the most recently allocated cell is -1 if the array is empty. Note that: • the server calls the clients by using the title (type) printed on their business cards

  11. The server keeps track of clients: class ServerImplementation implements Server { Client[] clients = new Client[100]; int index = -1; } The index of the most recently allocated cell is -1 if the array is empty. Note that: • the server calls the clients by using the title (type) printed on their business cards• plain arrays are used as instance variables to just make the type(s) more explicit

  12. What else does the server do?

  13. What else does the server do? public interface Server { public void register(Client client); }

  14. What else does the server do? public interface Server { public void register(Client client); } It allows clients to register with it, whatever that means.

  15. Although that, in this case, amounts to: synchronized public void register(Client client) { clients[++this.index] = client; client.setID(new Integer(this.index)); for (int i = 0; i < this.index; i++) { clients[i].register(new Integer(this.index), client); client.register(new Integer(i), clients[i]); } }

  16. Although that, in this case, amounts to: synchronized public void register(Client client) { clients[++this.index] = client; client.setID(new Integer(this.index)); for (int i = 0; i < this.index; i++) { clients[i].register(new Integer(this.index), client); client.register(new Integer(i), clients[i]); } } So we see that, apparently, a client: • can set its ID (through/during registration with the server,)

  17. Although that, in this case, amounts to: synchronized public void register(Client client) { clients[++this.index] = client; client.setID(new Integer(this.index)); for (int i = 0; i < this.index; i++) { clients[i].register(new Integer(this.index), client); client.register(new Integer(i), clients[i]); } } So we see that, apparently, a client: • can set its ID (through/during registration with the server,) and• allows any other client to register with it (also through/by the server)

  18. This changes the client business card a little: (that the server knows) public interface Client { public void setID(Integer index); public void register(Integer index, Client client); }

  19. This changes the client business card a little: (that the server knows) public interface Client { public void setID(Integer index); public void register(Integer index, Client client); } Let’s see how these promises can be fulfiled.

  20. First off, every client will have an own numeric id: class ClientImplementation implements Client { int id; synchronized public void setID (Integer index) { this.id = index.intValue(); } }

  21. Secondly, the world (collection) of peers initially registered with the server is replicated in each client: class ClientImplementation implements Client { int id; synchronized public void setID (Integer index) { this.id = index.intValue(); } Client[] peer = new Client[100]; int index = -1; synchronized public void register(Integer index, Client client) { this.index = index.intValue(); this.peer[this.index] = client; } }

  22. Secondly, the world (collection) of peers initially registered with the server is replicated in each client: class ClientImplementation implements Client { int id; synchronized public void setID (Integer index) { this.id = index.intValue(); } Client[] peer = new Client[100]; int index = -1; synchronized public void register(Integer index, Client client) { this.index = index.intValue(); this.peer[this.index] = client; } }

  23. Let’s go back to the server.

  24. Let’s go back to the server. How does it get started and what does it do?

  25. Let’s go back to the server. How does it get started and what does it do? The server has no constructor.

  26. Let’s go back to the server. How does it get started and what does it do? The server has no constructor. The server is implemented as a thread.

  27. In its run() method the server constantly broadcasts to clients: class ServerImplementation implements Server, Runnable { ... public void run() { while (true) { try { Thread.sleep((int)(Math.random() * 5000 + 5000)); this.broadcast(); } catch (Exception e) { } } } }

  28. Broadcasting amounts to the following: synchronized public void broadcast() { for (int i = 0; i <= this.index; i++) clients[i].setAvailable(new Boolean(false)); for (int i = 0, check = 0; i <= this.index; i++) check += clients[i].getBalance(); for (int i = 0; i <= this.index; i++) clients[i].setAvailable(new Boolean(true)); }

  29. Broadcasting amounts to the following: synchronized public void broadcast() { for (int i = 0; i <= this.index; i++) clients[i].setAvailable(new Boolean(false)); for (int i = 0, check = 0; i <= this.index; i++) check += clients[i].getBalance(); for (int i = 0; i <= this.index; i++) clients[i].setAvailable(new Boolean(true)); }

  30. Broadcasting amounts to the following: synchronized public void broadcast() { for (int i = 0; i <= this.index; i++) clients[i].setAvailable(new Boolean(false)); for (int i = 0, check = 0; i <= this.index; i++) check += clients[i].getBalance(); for (int i = 0; i <= this.index; i++) clients[i].setAvailable(new Boolean(true)); }

  31. During broadcasting:

  32. During broadcasting: • clients are first shut down • (by setting their availability to false)

  33. During broadcasting: • clients are first shut down • (by setting their availability to false) • the sum of points for the entire • collection of clients is calculated

  34. During broadcasting: • clients are first shut down • (by setting their availability to false) • the sum of points for the entire • collection of clients is calculated • and clients are then turned back on, one by one

  35. During broadcasting: • clients are first shut down • (by setting their availability to false) • the sum of points for the entire • collection of clients is calculated • and clients are then turned back on, one by one Here’s how we accomplish these steps.

  36. Setting the availability and returning the current balance are advertised by the clients. public interface Client { public void register(Client client); public void setID(Integer index); public void setAvailable(Boolean availability); public int getBalance(); }

  37. Setting the availability and returning the current balance are advertised by the clients. public interface Client { public void register(Client client); public void setID(Integer index); public void setAvailable(Boolean availability); public int getBalance(); } The implementation of these two functions is immediate.

  38. Boolean available = new Boolean(true); synchronized public void setAvailable(Boolean availability) { this.available = availability; } int balance; public int getBalance() { return this.balance; }

  39. Boolean available = new Boolean(true); synchronized public void setAvailable(Boolean availability) { this.available = availability; } int balance; public int getBalance() { return this.balance; } What else does the server do?

  40. Broadcasting is just a little bit more complicated but other than that, that’s it. // after turning the clients off String report = “”, calculation=“”; int check = 0; for (int i = 0; i <= this.index; i++) { report += clients[i].report() + "\n"; calculation += "(" + clients[i].getBalance() + ").."; check += clients[i].getBalance(); } System.out.println("Server sees: " + report); System.out.println(calculation + " ---> " + check); // now turn the clients back on

  41. Clients need to provide the added level of functionality: public interface Client { public void setID(Integer index); public void register(Integer index, Client client); public void setAvailable(Boolean availability); public int getBalance(); public String report(); }

  42. Implementation for this extra feature is immediate, as well: class ClientImplementation implements Client { ... String name; public String report() { return this.name + ": " + this.balance; } }

  43. Implementation for this extra feature is immediate, as well: class ClientImplementation implements Client { ... String name; public String report() { return this.name + ": " + this.balance; } } How does one start the server?

  44. The server provides a method for that: class ServerImplementation implements Server, Runnable { ... public void startAsLocalServer() { new Thread(this).start(); } }

  45. If we were to try this program out we'd do something like this: class LocalSetup { public static void main(String[] args) { } }

  46. A server needs to be created, then started: class LocalSetup { public static void main(String[] args) { ServerImplementation server = new ServerImplementation(); server.startAsLocalServer(); } }

  47. A server needs to be created, then started: class LocalSetup { public static void main(String[] args) { ServerImplementation server = new ServerImplementation(); server.startAsLocalServer(); } } We need some clients, too.

  48. A server needs to be created, then started: class LocalSetup { public static void main(String[] args) { ServerImplementation server = new ServerImplementation(); server.startAsLocalServer(); } } We need some clients, too. But how does one create a client?

  49. class ClientImplementation implements Client { ... public ClientImplementation(String name) { this.name = name; } } We need some clients, too. But how does one create a client?

  50. So we can create a few clients now: class LocalSetup { public static void main(String[] args) { ServerImplementation server = new ServerImplementation(); server.startAsLocalServer(); for (int i = 0; i < 6; i++) { ClientImplementation dealer = new ClientImplementation("Dealer_" + i); dealer.startAsClientOf(server); } } }

More Related