java接口作用(2)
来源:互联网 发布:雅思考试推荐用书 知乎 编辑:程序博客网 时间:2024/06/05 05:28
用java接口实现工厂模式如下:
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){};
public void method1(){System.out.println("Implemention1 method1");}
public void method2(){System.out.println("Implemention1 method2");}
}
class Implementation2 implements Service{
Implementation2(){}
public void method1(){System.out.println("Implementation2 method1");}
public void method2(){System.out.println("Implementation2 method2");}
}
class Implementation1ServiceFactory implements ServiceFactory{
public Service getService(){
return new Implementation1();
}
}
class Implementation2ServiceFactory implements ServiceFactory{
public Service getService(){
return new Implementation2();
}
}
public class TopCoder{
public static void main(String[] args){
serviceConsumer(new Implementation1ServiceFactory());
serviceConsumer(new Implementation2ServiceFactory());
}
public static void serviceConsumer(ServiceFactory sf){
Service s=sf.getService();
s.method1();
s.method2();
}
}
该实现有一个小小的缺憾,那就是Implementation1 和 Implementation2的构造函数都是public。下面用Java的内部类实现工厂模式,可以弥补这个缺憾
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){};
public void method1(){System.out.println("Implemention1 method1");}
public void method2(){System.out.println("Implemention1 method2");}
public static ServiceFactory factory=new ServiceFactory(){
public Service getService(){
return new Implementation1();
}
};
}
class Implementation2 implements Service{
Implementation2(){}
public void method1(){System.out.println("Implementation2 method1");}
public void method2(){System.out.println("Implementation2 method2");}
public static ServiceFactory factory=new ServiceFactory(){
public Service getService(){
return new Implementation2();
}
};
}
public class TopCoder{
public static void main(String[] args){
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);
}
public static void serviceConsumer(ServiceFactory sf){
Service s=sf.getService();
s.method1();
s.method2();
}
}
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){};
public void method1(){System.out.println("Implemention1 method1");}
public void method2(){System.out.println("Implemention1 method2");}
}
class Implementation2 implements Service{
Implementation2(){}
public void method1(){System.out.println("Implementation2 method1");}
public void method2(){System.out.println("Implementation2 method2");}
}
class Implementation1ServiceFactory implements ServiceFactory{
public Service getService(){
return new Implementation1();
}
}
class Implementation2ServiceFactory implements ServiceFactory{
public Service getService(){
return new Implementation2();
}
}
public class TopCoder{
public static void main(String[] args){
serviceConsumer(new Implementation1ServiceFactory());
serviceConsumer(new Implementation2ServiceFactory());
}
public static void serviceConsumer(ServiceFactory sf){
Service s=sf.getService();
s.method1();
s.method2();
}
}
该实现有一个小小的缺憾,那就是Implementation1 和 Implementation2的构造函数都是public。下面用Java的内部类实现工厂模式,可以弥补这个缺憾
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){};
public void method1(){System.out.println("Implemention1 method1");}
public void method2(){System.out.println("Implemention1 method2");}
public static ServiceFactory factory=new ServiceFactory(){
public Service getService(){
return new Implementation1();
}
};
}
class Implementation2 implements Service{
Implementation2(){}
public void method1(){System.out.println("Implementation2 method1");}
public void method2(){System.out.println("Implementation2 method2");}
public static ServiceFactory factory=new ServiceFactory(){
public Service getService(){
return new Implementation2();
}
};
}
public class TopCoder{
public static void main(String[] args){
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);
}
public static void serviceConsumer(ServiceFactory sf){
Service s=sf.getService();
s.method1();
s.method2();
}
}
- java接口作用(2)
- 接口的作用(java)
- java 接口继承接口作用
- JAVA接口的作用
- JAVA接口的作用
- java 接口的作用
- java的接口作用
- java接口作用是什么?
- JAVA接口的作用
- java接口作用
- java接口的作用
- java接口作用是什么
- JAVA接口的作用
- java接口的作用
- Java中的接口作用
- Java接口作用
- java接口的作用
- Java接口作用
- 数据结构算法
- C#类,类成员访问修饰符
- VPN加密算法大全
- Jsp页面使用JavaBeans
- Samsung Announced First Smart TV App Developed Using Adobe AIR
- java接口作用(2)
- 关于文件流的写入与读取总结
- IP组播与组播协议
- GCT进行时
- Started to climb the Akka mountain
- This element neither has attached source nor attached Javadoc
- DIV不随滚动条而滚动
- 入职以来的工作总结
- 一致性hash算法 - consistent hashing