当前位置: 代码迷 >> J2SE >> 关于最基础的工厂的模式,究竟这种设计模式有什么优势呢?解决办法
  详细解决方案

关于最基础的工厂的模式,究竟这种设计模式有什么优势呢?解决办法

热度:107   发布时间:2016-04-24 12:51:39.0
关于最基础的工厂的模式,究竟这种设计模式有什么优势呢?
下面是一个最基础的工厂模式,可是它究竟有什么优势呢?我觉得通过接口 —— 实现类 —— 获得另一个类的对象。这个过程反而我觉得麻烦很多。所以特地向大家请教。
Java code
import static MyTools.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("Implementation1 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 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());  }}


------解决方案--------------------
http://topic.csdn.net/u/20080919/10/bdb442fb-18b6-4061-bf9a-d2ac842c9246.html

相信你看了这个链接之后,一切都明白了
------解决方案--------------------
Effective Java

Item1 说的最清楚了..
------解决方案--------------------
java支持单重继承,为了能实现c++那样多重继承的功能,设计了接口,比如男人可以继承人的一些特性(吃饭,睡觉、、、),但是为了表现猪也有这些特性,我们可以定义一个接口(专管吃,睡),猪虽然不能继承人这个父类,但可以实现接口.不知你明不明白?
  相关解决方案