Vous êtes sur la page 1sur 7

Adapter pattern

Java

Before

Because the interface between Line and Rectangle objects is incompatible, the user has to
recover the type of each shape and manually supply the correct arguments.

class LegacyLine {
public void draw(int x1, int y1, int x2, int y2) {
System.out.println("line from (" + x1 + ',' + y1 + ") to (" + x2 + ','
+ y2 + ')');
}
}

class LegacyRectangle {
public void draw(int x, int y, int w, int h) {
System.out.println("rectangle at (" + x + ',' + y + ") with width " + w
+ " and height " + h);
}
}

public class AdapterDemo {

public static void main(String[] args) {


Object[] shapes = {
new LegacyLine(), new LegacyRectangle()
};
// A begin and end point from a graphical editor
int x1 = 10, y1 = 20;
int x2 = 30, y2 = 60;

for (Object obj : shapes) {


if (LegacyLine.class.isInstance(obj)) {
LegacyLine.class.cast(obj).draw(x1, y1, x2, y2);
} else if (LegacyRectangle.class.isInstance(obj)) {
LegacyRectangle.class.cast(obj).draw(Math.min(x1, x2),
Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
}
}
}
}
//line from (10,20) to (30,60)
//rectangle at (10,20) with width 20 and height 40
After

The Adapter’s “extra level of indirection” takes care of mapping a user-friendly common
interface to legacy-specific peculiar interfaces.

class LegacyLine
{
public void draw(int x1, int y1, int x2, int y2)
{
System.out.println("line from (" + x1 + ',' + y1 + ") to (" + x2 + ','
+ y2 + ')');
}
}

class LegacyRectangle
{
public void draw(int x, int y, int w, int h)
{
System.out.println("rectangle at (" + x + ',' + y + ") with width " + w
+ " and height " + h);
}
}

interface Shape
{
void draw(int x1, int y1, int x2, int y2);
}

class Line implements Shape


{
private LegacyLine adaptee = new LegacyLine();
public void draw(int x1, int y1, int x2, int y2)
{
adaptee.draw(x1, y1, x2, y2);
}
}

class Rectangle implements Shape


{
private LegacyRectangle adaptee = new LegacyRectangle();
public void draw(int x1, int y1, int x2, int y2)
{
adaptee.draw(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1),
Math.abs(y2 - y1));
}
}
public class AdapterDemo
{
public static void main(String[] args)
{
Shape[] shapes =
{
new Line(), new Rectangle()
};
// A begin and end point from a graphical editor
int x1 = 10, y1 = 20;
int x2 = 30, y2 = 60;
for (Shape shape : shapes)
shape.draw(x1, y1, x2, y2);
}
}
//line from (10,20) to (30,60)
//rectangle at (10,20) with width 20 and height 40

What is RMI?

Answer
Remote Method Invocation. It is an approach where a method on a remote machine invokes another
method on another machine to perform some computation and return the result to the calling method.

What is RMI?

To interact with the methods of objects in other / remote machines using JVM, RMI is used. This
process allows the information exchanging using multiple JVMs. It provides the location
transparency by giving the sense that the methods accessing locally.

Explain the advantages and disadvantages of RMI.

Advantages of RMI:

 Simple and clean to implement that leads to more robust, maintainable and flexible applications
 Distributed systems creations are allowed while decoupling the client and server objects
simultaneously
 It is possible to create zero-install client for the users.
 No client installation is needed except java capable browsers
 At the time of changing the database, only the server objects are to be recompiled but not the
server interface and the client remain the same.

Disadvantages of RMI:

 Less efficient than Socket objects.


 Assuming the default threading will allow ignoring the coding, being the servers are thread- safe
and robust.
 Cannot use the code out of the scope of java.
 Security issues need to be monitored more closely

Explain the architecture of RMI.

RMI is based on client-server architecture model. The stub plays the role of a proxy server for
the remote objects. The skeleton lives in the same JVM as the remote object and communication
will be handled with the stub. The remote references are managed by the registry. The binding of
server with reference to itself will be on the registry. The clients communicate with a registry
which in turn obtains a remote reference to the server. This could be a remote host. The remote
reference could be obtained by the client from the registry in order to invoke the methods from
the remote object.

RMI vs. CORBA.

RMI and CORBA – one is not “better” than other. The comparisons between these two
technologies reveal the strengths and weaknesses. The applicability of these two technologies is
purely depends on the application’s demand, feasibility, performance.

The selection of RMI or CORBA depends on the following advantages of RMI and CORBA.

RMI Advantages:

 Portable across the platforms


 Code can execute on remote JVMs
 Existing systems can adapt RMI as this technology is available from JDK 1.02

RMI disadvantages:

 Can use only the java supported platforms


 Limited functionality because of security restrictions
 No support for legacy systems

CORBA advantages:

 The services are non-platform and non-language dependent


 Encourages various implementations based on the same interface.
 Supports for primitive data types and data structures as parameters
 Provides easy ways to link between objects and systems

CORBA disadvantages:

 Interface Definition Language mapping needs writing for one language may not support the
another language.
 New changes in the existing system / code may not integrate with the IDL language tools.
 Data transfer or objects is not supported by CORBA
 If CORBA specifications fails for adoption by the industry, then it become legacy system.

Vous aimerez peut-être aussi