工厂设计模式
来源:互联网 发布:2017广电禁止网络电视 编辑:程序博客网 时间:2024/05/29 18:46
package interfaces;
/**
* 工厂设计模式
* */
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){}
public void method1(){System.out.println("Implementation1 method1");}
public void method2(){System.out.println("Implementation1 method2");}
}
class Implementation1Factory implements 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");}
}
class Implementation2Factory implements 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(new Implementation1Factory());
serviceConsumer(new Implementation2Factory());
}
/**
* 工厂设计模式
* */
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
Implementation1(){}
public void method1(){System.out.println("Implementation1 method1");}
public void method2(){System.out.println("Implementation1 method2");}
}
class Implementation1Factory implements 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");}
}
class Implementation2Factory implements 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(new Implementation1Factory());
serviceConsumer(new Implementation2Factory());
}
}
输出结果:
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
阅读全文
0 0
- 设计模式--工厂设计
- 设计模式------工厂模式
- 设计模式------工厂模式
- 设计模式 [ 工厂模式 ]
- 设计模式--工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式 工厂模式
- 设计模式-工厂模式
- 设计模式 -- 工厂模式
- 设计模式-工厂模式
- 设计模式----工厂模式
- 设计模式---工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式---工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- [第五章]函数对变量的作用:局部变量和全局变量
- 关于串匹配的简单理解
- 计算机视觉与模式识别 code
- Windows下安装scikit-learn
- redis简介及配置
- 工厂设计模式
- 使用IIS配置Http
- [BZOJ4872][Shoi2017]分手是祝愿 数学期望
- Struts2 jsp页面url访问查Action中对应指定方法
- notepad++查看项目源码
- IMWeb提升营Day6
- CUDA从入门到精通
- POJ 1636 Prison rearrangement 笔记
- NAND_flash和NOR_flash区别(最详细)