InterfaceDemo工厂设计模式
//工厂设计模式//*****************************
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);
f.eat();
}
}
页:
[1]