///工厂设计模式
public static void main(String[] args) {
Z f= F.sun("nihao");
f.fun();
}
interface Z{
public abstract void fun();
}
class D1 implements Z{
public void fun(){
System.out.println("111");
}
}
class D2 implements Z{
public void fun(){
System.out.println("222");
}
}
class F{
public static Z sun(String NAME){
Z r=null;
if("nihao".equals(NAME)){
r=new D1();
}
if("buhao".equals(NAME)){
r=new D2();
}return r;
}
}
/* Fruit a= Factory.func("apple");
a.fun();
}
}
interface Fruit{
public void fun();
}
class Apple implements Fruit{
public void fun(){
System.out.println("苹果");
}
}
class Orange implements Fruit{
public void fun(){
System.out.println("橘子");
}
}
class Factory{
public static Fruit func(String className){
Fruit r=null;
if("apple".equals(className)){
r= new Apple();
}
if("orange".equals(className)){
r=new Orange();
}
return r;
}
}*/
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:工厂设计模式 - Python技术站