您的位置:首页 > 其它

匿名类的工厂模式

2009-08-09 09:40 288 查看
interface Service

{

void method1();

void method2();

}

interface ServiceFactory

{

Service getService();

}

class Implementation1 implements Service

{

private Implementation1()

{

}

public void method1()

{

System.out.println("Impelmentation1 method1()");

}

public void method2()

{

System.out.println("Impelmentation1 method2()");

}

public static ServiceFactory factory = new ServiceFactory(){

public Service getService()

{

return new Implementation1();

}

};

}

class Implementation2 implements Service

{

private Implementation2()

{

}

public void method1()

{

System.out.println("Impelmentation2 method1()");

}

public void method2()

{

System.out.println("Impelmentation2 method2()");

}

public static ServiceFactory factory = new ServiceFactory(){

public Service getService()

{

return new Implementation2();

}

};

}

public class Factories

{

public static void ServiceConsumer(ServiceFactory fact)

{

Service s = fact.getService();

s.method1();

s.method2();

}

public static void main(String args[])

{

ServiceConsumer(Implementation1.factory);

ServiceConsumer(Implementation2.factory);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: