1.简单的介绍各个类之间的关系UML图如下:
2.左边的3个类的代码代码路径,右边的代码如下:
package com.xiaobi.golf.factory.factoryMethod;
import com.xiaobi.golf.factory.Car;
/**
* 工厂方法接口
*/
public interface CarFactory {
Car createCar();
}
package com.xiaobi.golf.factory.factoryMethod;
import com.xiaobi.golf.factory.BydCar;
import com.xiaobi.golf.factory.Car;
public class BydCarFactory implements CarFactory {
@Override
public Car createCar() {
return new BydCar();
}
}
package com.xiaobi.golf.factory.factoryMethod;
import com.xiaobi.golf.factory.AudiCar;
import com.xiaobi.golf.factory.Car;
public class AudiCarFactory implements CarFactory {
@Override
public Car createCar() {
return new AudiCar();
}
}
package com.xiaobi.golf.factory.simpleFactory;
import com.xiaobi.golf.factory.Car;
public class Client01 {
public static void main(String[]args){
Car car01= CarFactory.createCar("奥迪");
Car car02=CarFactory.createCar("比亚迪");
System.out.println(car01.run());
System.out.println(car02.run());
}
}