| |
34. 4. 3. 适配器模式:两个接口 |
|
public class TestAdapter {
public static void main(String args[]) {
SimpleName aceObject = new SimpleName();
aceObject.setName("A");
FullNameSimpleNameAdapter adapter = new FullNameSimpleNameAdapter(aceObject);
System.out.println("Customer's first name: " + adapter.getFirstName());
System.out.println("Customer's last name: " + adapter.getLastName());
}
}
interface SimpleNameInterface {
public void setName(String n);
public String getName();
}
class SimpleName implements SimpleNameInterface {
String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
interface FullNameInterface {
public void setFirstName(String f);
public void setLastName(String l);
public String getFirstName();
public String getLastName();
}
class FullNameSimpleNameAdapter implements FullNameInterface {
SimpleName aceObject;
String firstName;
String lastName;
public FullNameSimpleNameAdapter(SimpleName a) {
aceObject = a;
firstName = aceObject.getName().split(" ")[0];
lastName = aceObject.getName().split(" ")[1];
}
public void setFirstName(String f) {
firstName = f;
}
public void setLastName(String l) {
lastName = l;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
|
|
|