Java工厂设计模式
2016-04-12 15:10
513 查看
package lu.interfaces;
import static lu.utils.Print.*;
/*工厂方法设计模式
* 在工厂对象上调用的是创建方法,而该工厂对象将生成接口的某个实现的对象。
* 通过这种方式,我们的代码将完全与接口的实现分离,这就使得我们可以透明地将某个实现替换为另一个实现。*/
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){}
public void method1(){print("Implementation1 method1");}
public void method2(){print("Implementation2 method2");}
}
class Implementation1Factory implements ServiceFactory{
public Service getService(){
return new Implementation1();
}
}
class Implementation2 implements Service{
Implementation2(){}
public void method1(){print("Implementation2 method1");}
public void method2(){print("Implementation2 method2");}
}
class Implementation2Factory implements ServiceFactory{
public Service getService(){
return new Implementation2();
}
}
public class Factory{
public static void serviceConsumer(ServiceFactory fact){
Service s=fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args){
serviceConsumer(new Implementation1Factory());
//Implementations are completely interchangeable;
serviceConsumer(new Implementation2Factory());
}
}
Output
Implementation1 method1
Implementation2 method2
Implementation2 method1
Implementation2 method2
import static lu.utils.Print.*;
/*工厂方法设计模式
* 在工厂对象上调用的是创建方法,而该工厂对象将生成接口的某个实现的对象。
* 通过这种方式,我们的代码将完全与接口的实现分离,这就使得我们可以透明地将某个实现替换为另一个实现。*/
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){}
public void method1(){print("Implementation1 method1");}
public void method2(){print("Implementation2 method2");}
}
class Implementation1Factory implements ServiceFactory{
public Service getService(){
return new Implementation1();
}
}
class Implementation2 implements Service{
Implementation2(){}
public void method1(){print("Implementation2 method1");}
public void method2(){print("Implementation2 method2");}
}
class Implementation2Factory implements ServiceFactory{
public Service getService(){
return new Implementation2();
}
}
public class Factory{
public static void serviceConsumer(ServiceFactory fact){
Service s=fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args){
serviceConsumer(new Implementation1Factory());
//Implementations are completely interchangeable;
serviceConsumer(new Implementation2Factory());
}
}
Output
Implementation1 method1
Implementation2 method2
Implementation2 method1
Implementation2 method2
相关文章推荐
- java多线程编程如何控制执行顺序
- eclipse自动补全失效解决办法
- 聊聊高并发(十四)理解Java中的管程,条件队列,Condition以及实现一个堵塞队列
- springMVC、spring、jpa、springData整合配置
- 警告: No configuration found for the specified action: 'checkLogin' in namespace: ''.的解决办法
- Spark:java.io.IOException: No space left on device
- web.xml中引入SPRING配置文件classpath的含义
- 将json格式的数组字符串转换为java集合
- javadoc解决中文乱码问题
- 解决java compiler level does not match the version of the installed java project facet
- java多线程入门学习(一)
- java.io包详细解说
- java.util.ConcurrentModificationException at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(L
- 求解数组环中最大子数组和的问题(java)
- javadoc 出现:错误: 编码GBK的不可映射字符
- java 基础数据遍历(1)----将一个字符串中的空格替换成“%20”
- java多线程那些事之并发集合
- java 关于初学面向对象的问题一:对象的三大特性
- java.sql.Date 与 java.util.Date 的转换
- 权限管理框架实现(2)--Struts中ValueStack使用