没有使用工厂的模式简单的调用
package com.simpleFactory;
public interface car {
public void run();
}
package com.simpleFactory;
public class auDi implements car {
@Override
public void run() {
System.out.println("auDi is running");
}
}
package com.simpleFactory;
public class bYd implements car {
@Override
public void run() {
System.out.println("bYd is running");
}
}
/*主函数*/
package com.simpleFactory;
public class client01 {
public static void main(String[] args) {
car audo=new auDi();
car byd=new bYd();
audo.run();
byd.run();
}
}
package com.simpleFactory;
public interface car {
public void run();
}
package com.simpleFactory;
public class auDi implements car {
@Override
public void run() {
System.out.println("auDi is running");
}
}
package com.simpleFactory;
public class bYd implements car {
@Override
public void run() {
System.out.println("bYd is running");
}
}
package com.simpleFactory;
public class CarFactory {
public static car createCar(String type){//使用静态方法能够让类进行调用
if("audi".equals(type)){
return new auDi();
}else if("byd".equals(type)){
return new bYd();
}else{
return null;
}
}
}
package com.simpleFactory;
/**
* 简单工厂
*/
public class client02 {
public static void main(String[] args) {
car c1= CarFactory.createCar("audi");//只需要和工厂打交道,无需再和具体的类打交道
car c2= CarFactory.createCar("byd");
c1.run();
c2.run();
}
}