public class ccuyw {
public static void main(String[] args) {
Workshop w=new Phone();
w.produce();
Workshop WS=new fabricate().produces("phone");
if ("phone"!=null);
WS.produce();
}
}
class fabricate{
public Workshop produces(String name){
if ("phone".equalsIgnoreCase(name)){
return new Phone();
}else if ("Computer".equalsIgnoreCase(name)){
return new Computer();
}
return null;
}
}
interface Workshop{
public void produce();
}
class Phone implements Workshop{
@Override
public void produce() {
System.out.println("生产手机..................");
}
}
class Computer implements Workshop{
@Override
public void produce() {
System.out.println("生产手机..................");
}
}
简单的工厂设计模式
最新推荐文章于 2024-11-13 20:43:20 发布