|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//工厂设计模式
//*****************************
interface Fruit
{
public abstract void eat();
}
//*********************************
class Apple implements Fruit
{
public void eat()
{
System.out.println("Apple类==>吃苹果");
}
}
//****************************************************
class Orange implements Fruit
{
public void eat()
{
System.out.println("Orange类==>吃橘子");
}
}
//*****************************************************
class Factory
{
public static Fruit getInstance(String className)
{
Fruit f=null;
if("apple".equals(className))
{
f=new Apple();
}
if("orange".equals(className))
{
f=new Orange();
}
return f;
}
}
//******************************************************
public class InterfaceDemo
{
public static void main(String[] args)
{
Fruit f=null;
f=Factory.getInstance(args[0]);
f.eat();
}
} |
|