Java implementation of Adapter (Adapter) mode [00 original]__java

Source: Internet
Author: User
Here is a "finger-round" example to illustrate the principle of adapter mode and simple application, this example from Java and schema.

1. Class Diagram


2. Java Implementation CodePackage cn.edu.ynu.sei.adapter;

/**
* Sphere Interface <br>
*
* @author 88250
* @version 1.0.0, 2007-8-28
*/
public interface BALLF
{
/**
* Calculated surface Area
* @return
*/
Public double CalculateArea ();

/**
* Calculate Volume
* @return
*/
Public double calculatevolume ();

/**
* Return radius value
* @return
*/
Public double Getradius ();

/**
* Set RADIUS value
* @param radius
*/
public void Setradius (double radius);
}

Package cn.edu.ynu.sei.adapter;

/**
* Square Type <br>
* This is the source role of the object's adapter pattern
*
* @author 88250
* @version 1.0.0, 2007-8-28
*/
public class Cube
{
private double width;

/**
* Builder
* @param width
*/
Public Cube (double width)
{
this. width = width;
}

/**
* Calculate Volume
* @return
*/
Public double Calculatevolume ()
{
return width * Width * width;
}

/**
* Calculated surface Area
* @return
*/
Public double Calculatefacearea ()
{
Return 6 * Width * width;
}

/**
* @return The width
*/
Public double getwidth ()
{
return width;
}

/**
* @param width of the width to set
*/
public void SetWidth (double width)
{
this. width = width;
}
}

Package cn.edu.ynu.sei.adapter;

/**
* This class acts as a "finger-round" adapter role
*
* @author 88250
* @version 1.0.0, 2007-8-28
*/
public class Magicfinger implements BALLF
{
private double radius;

Private static final Double PI = 3.1415926D;

Private Cube adaptee;

Public Magicfinger (Cube adaptee)
{
Super ();
this. Adaptee = Adaptee;
Radius = Adaptee.getwidth ();
}

@Override
Public double CalculateArea ()
{
return PI * 4.0D * (RADIUS * radius);
}

@Override
Public double Calculatevolume ()
{
Return PI * (4.0d/3.0d) * (RADIUS * radius * radius);
}

@Override
Public double Getradius ()
{
return radius;
}

@Override
public void Setradius (double radius)
{
this. Radius = radius;
}
}

3. SummaryThe above implementation is the object's adapter pattern, that is, the source role is a class. If the source role is an interface, then the adapter mode for the class, which is no longer repeat here.
The JDBC that we often use is the specific application of an adapter pattern, and the wine simulator (which can simulate Windows) under Linux is also the application of adapter mode.

4. Reference Documents"Design Patterns", "Java and 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.