匿名类的工厂模式
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);
}
}
{
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);
}
}
相关文章推荐
- Java实现(01)——工厂模式(Factory Pattern)
- 工厂模式---一个模子里刻出来
- Java中接口(interface)和匿名类
- 设计模式-------工厂模式
- 工厂模式
- 安卓复习之旅—工厂模式(三)
- 详解js产生对象的3种基本方式(工厂模式,构造函数模式,原型模式)
- 工厂模式在JS中的实践
- 设计模式-工厂模式
- 设计模式系列——三个工厂模式(简单工厂模式,工厂方法模式,抽象工厂模式)
- java设计模式 工厂模式
- 工厂模式、call、apply
- 工厂模式
- Java匿名类
- 设计模式——工厂模式
- 工厂模式(简单工厂、工厂方法)
- 工厂模式(懒汉式、饿汉式)
- 设计模式C++实现——工厂模式
- 设计模式第二话——工厂模式和抽象工厂模式
- 深入浅出设计模式之装饰者模式、工厂模式