借助匿名内部类实现另类工厂模式[更新]
:
//interface
public interface Service {
/**
* <P>计算.</P>
*/
void calculate() ;
/**
* <P>存储计算结果.</P>
*/
void saveInfo() ;
}
//factory
public interface ServiceFactory {
/**
* <P>得到Service实现.</P>
* @return
*/
Service getService() ;
}
//implements
//bi
public class ServiceBI implements Service {
/**
* <P>计算.</P>
*/
@Override
public void calculate() {
System.out.println("计算 BI.");
}
/**
* <P>存储计算结果.</P>
*/
@Override
public void saveInfo() {
System.out.println("存储计算结果.");
}
//得到工厂实现类. 工厂实现类只是为了得到Service实现类.
static ServiceFactory factory = new ServiceFactory() {
@Override
public Service getService() {
return new ServiceBI() ; //在自己类中实例化自己,这样相对好很多
}
} ;
}
//ci
public class ServiceCI implements Service {
@Override
public void calculate() {
System.out.println(" 计算 CI.");
}
@Override
public void saveInfo() {
System.out.println("保存计算结果.") ;
}
static ServiceFactory factory = new ServiceFactory() {
@Override
public Service getService() {
return new ServiceCI();
}
} ;
}
//test
public class CalculateService {
static void callFun(ServiceFactory factory) {
//规范必须使用接口自己的方法得到接口实现.
Service service = factory.getService() ;
service.calculate();
service.saveInfo();
}
public static void main(String[] args) {
//bi
callFun(ServiceBI.factory);
//ci
callFun(ServiceCI.factory);
}
}
以上代码实现了工厂匿名内部类完成接口实例化,但是我们一般需要多个非基础类的实例,所以,最好的情况下是以上Service
接口的实现类应该都是单例的才好,以上代码显然有多线程多实例问题。
我想到了一种解决办法,不知道有没问题,我经过简单的多线程测试没有发现多个实例问题。如果有问题,请大家直言不讳,谢谢!
代码如下:
//interface
public interface Service {
void method1() ;
void method2() ;
}
public interface ServiceFactory {
Service getService() ;
}
//implements
public class ServiceImpl0 implements Service {
private ServiceImpl0() {
System.out.println("ServiceImpl0 初始化....");
}
@Override
public void method1() {
System.out.println("我是ServiceImpl0 method1.");
}
@Override
public void method2() {
System.out.println("我是ServiceImpl0 method2.");
}
public static final ServiceFactory FACTORY = new ServiceFactory() {
@Override
public Service getService() {
return InitService.SingleService ;
}
} ;
/**
* <P>单例实现</P>
*/
private static class InitService {
private static final Service SingleService = new ServiceImpl0() ;
}
}
public class ServiceTest {
public static void call(ServiceFactory factory) {
Service service = factory.getService() ;
service.method1();
service.method2();
}
/*public static void main(String[] args) {
call(ServiceImpl0.FACTORY);
call(ServiceImple1.FACTORY);
}*/
}
//thread
public class Mythread implements Runnable {
@Override
public void run() {
System.out.println("开始一个线程. id " + Thread.currentThread().getName());
//
ServiceTest.call(ServiceImpl0.FACTORY);
}
}
//test
Mythread mythread = new Mythread() ;
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
new Thread(mythread).start();
匿名对象生成的class 是一个单独文件的内部类,如下:
//接口
public interface Service {
void method1() ;
void method2() ;
}
//main
public class Servicetest {
public static void main(String[] args) {
Service service = new Service() {
@Override
public void method1() {
System.out.println("我是匿名对象方法1.");
}
@Override
public void method2() {
System.out.println("我是匿名对象方法2.");
}
} ;
service.method1();
service.method2();
}
}
编译后Servicetest
和Service
文件没有变化,只是多出了一个匿名对象的类文件,如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
final class Servicetest$1 implements Service {
Servicetest$1() {
}
public void method1() {
System.out.println("我是匿名对象方法1.");
}
public void method2() {
System.out.println("我是匿名对象方法2.");
}
}