现有一个接口DataOperation定义了排序方法Sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序,类BinarySearch的binarySearch(int[],int)方法实现了二分查找算法,现使用适配器模式设计一个系统,在不修改源码的情况下将类QuickSort和类BinarySearch的方法适配到DataOperation接口中。绘制类图并编程实现。
public class QuickSort {
public void quickSort() {
System.out.println("快速排序");
}
}
public interface DataOperation {
public void sort();
public void search();
}
public class QuickSortAdapter extends QuickSort implements DataOperation{
@Override
public void sort() {
System.out.println("适配器接口");
super.quickSort();
}
@Override
public void search() {
}
}
修改实例仿生机器人,使得机器人可以像鸟一样叫,并像狗一样的跑,请绘制类图并编程实现。
public interface Robot {
public void cry();
public void move();
}
public class Bird {
public void tweedle(){
System.out.println("鸟儿吱吱叫");
}
public void fly() {
System.out.println("鸟飞起来");
}
}
public class BirdAdapter extends Bird implements Robot{
@Override
public void cry() {
System.out.println("机器人");
super.tweedle();
}
@Override
public void move() {
System.out.println("机器人");
super.fly();
}
}
public class Dog {
public void wang(){
System.out.println("狗汪汪叫");
}
public void run() {
System.out.println("狗快快跑");
}
}
public class DogAdapter extends Dog implements Robot{
@Override
public void cry() {
System.out.println("机器人");
super.wang();
}
@Override
public void move() {
System.out.println("机器人");
super.run();
}
}
public class Client {
public static void main(String args[])
{
Robot robot = (Robot)XMLUtil.getBean();
robot.cry();
robot.move();
}
}
<?xml version="1.0"?>
<config>
<className>BirdAdapter</className>
</config>
如果系统中某对象有三个维度,如某日志记录器既可以支持不同的操作系统,还可以支持多种编程语言,并且可以使用不同的输出方式。请使用桥接模式设计该系统。
public abstract class EncodingLanguage {
OutPut output;
public void setOutput(OutPut out)
{
this.output = out;
}
public abstract void language(String system);
}
public class C extends EncodingLanguage {
public void language(String system) {
String language = "C语言";
this.output.output(system,language);
}
}
public class Java extends EncodingLanguage {
public void language(String system){
String language = "Java语言\n";
this.output.output(system,language);
}
}
public abstract class OperatingSystem {
EncodingLanguage encodingLanguage;
public void setlanguage(EncodingLanguage Lang)
{
this.encodingLanguage = Lang;
}
public abstract void type();
}
public class Windows extends OperatingSystem {
public void type() {
String system="windows系统";
this.encodingLanguage.language(system);
}
}
public class ContrleOut extends OutPut{
public void output(String system,String language) {
String means = "控制台方式";
System.out.println(system+""+language+means);
}
}
public class Client {
public static void main(String[] args) {
OutPut output = null;
EncodingLanguage language=null;
OperatingSystem system=null;
if (output==null && language==null && system==null) {
output = (OutPut) XMLUtil.getBean("OutPut");
language = (EncodingLanguage) XMLUtil.getBean("EncodingLanguage");
system = (OperatingSystem) XMLUtil.getBean("OperatingSystem");
}
system.setlanguage(language);
language.setOutput(output);
system.type();
}
}