[Design optimization]-correct use of the Value Object Mode

Source: Internet
Author: User

In J2EE software development, system modules are usually layered, as shown in:



The presentation layer is mainly responsible for data presentation, the business road layer is responsible for specific business logic processing, and the persistence layer is responsible for database and other persistent operations.

In large systems, these layers are likely to be separated and deployed on different servers. The two layers may communicate by remotely calling RMI. For example, an order has three attributes: customer name, product name, and number. For example, three data interactions are required to obtain an order.

Although this mode is feasibleTwo serious problems:

First, from the perspective of object-oriented design, this method is unreasonable, cumbersome, and does not have good maintainability.

Second, an order is sent for three communications, and the performance is too low.

To solve this problem, you can useValue ObjectMode. In this mode, each attribute of an object is encapsulated to spread the encapsulated object in the network, so that the system can have a better interaction model and reduce network communication data, this improves system performance.

Optimized architecture model:


In this way, you only need to perform a remote call to obtain the complete order information, which shortens the access response time and reduces network data traffic.

To implement such a model, we needPart 5.

The first role is the carrier of order information: Order entity class

Public class order implements serializable {Private Static final long serialversionuid =-limit 476071552949121l; private int orderid; private string clientname; private int number; private string productname; // The getter and setter methods are omitted}

The second role is the service interface called by the RMI server: iordermanager Interface

public interface IOrderManager extends Remote {public Order getOder(int id) throws RemoteException;public String getClientName(int id) throws RemoteException;public int getProdName(int id) throws RemoteException;public int getNumber(int id) throws RemoteException;}

The third role is the implementation class of the server interface: ordermanager object

Public class ordermanager extends unicastremoteobject implements iordermanager {Private Static final long serialversionuid = bytes; // limits that only subclass or this package class can use protected ordermanager () throws RemoteException {super ();} @ overridepublic order getoder (int id) throws RemoteException {system. out. print ("request received ed! Id = "+ id); order o = New Order (); O. setclientname ("ZQ"); O. setnumber (1); O. setorderid (2); O. setproductname ("car"); return O;} @ overridepublic string getclientname (int id) throws RemoteException {return "ZQ" ;}@ overridepublic int getprodname (int id) throws RemoteException {return 0 ;}@ overridepublic int getnumber (int id) throws RemoteException {return 1 ;}}

The fourth role is the RMI server: ordermanagerserver class

public class OrderManagerServer {public static void main(String[] args){try{LocateRegistry.createRegistry(1099);IOrderManager userManager = new OrderManager();Naming.bind("userManager", userManager);System.out.println("OrderManager is ready!");}catch (Exception e){System.out.println("OrderManager Server failed:"+e);}}}

The fifth is the client: ordercient class

public class OrderManagerServer {public static void main(String[] args){try{LocateRegistry.createRegistry(1099);IOrderManager userManager = new OrderManager();Naming.bind("userManager", userManager);System.out.println("OrderManager is ready!");}catch (Exception e){System.out.println("OrderManager Server failed:"+e);}}}

We first open the server and then execute the client. The running results are as follows:

Server: ordermanager is ready!

Request received ed! Id = 1 request received ed! Id = 1

Client: ZQ

Since then, a simple value object model has been implemented!

[Design optimization]-correct use of the Value Object Mode

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.