public class teet16 {
public static void main(String[] args){
Function1 x=Execute1.getservice("phone");
x.work();;
Execute2 y=new Execute2();
Function1 s=y.getservice("computer");
s.work();;
}
}
interface Function1{
abstract void work();
}
class Phone implements Function1{
public void work(){
System.out.println("Phone can go to work");
}
}
class Computer implements Function1{
public void work(){
System.out.println("computer can go to work");
}
}
class Execute1{
public static Function1 getservice(String name){
if(name.equals("computer")){
return new Computer();
}
else if(name.equals("phone")){
return new Phone();
}
else
return null;
}
}
class Execute2{
public Function1 getservice(String name){
if(name.equals("computer")){
return new Computer();
}
else if(name.equals("phone")){
return new Phone();
}
else
return null;
}
}
qf简单工厂设计模式练习题
最新推荐文章于 2024-07-09 23:34:12 发布