其他分享
首页 > 其他分享> > 工厂模式 https://www.jianshu.com/p/6dfb5b66d088

工厂模式 https://www.jianshu.com/p/6dfb5b66d088

作者:互联网

简单工厂:

定义与类型

适用场景:

优点

缺点

UML关系图:

  Simple_Factory.png

 

简单工厂有3个对象:
工厂类(Factory):根据内部逻辑返回相应的产品
抽象产品类(Product):提供抽象方法供具体产品类实现
具体产品类(ConcreteProduct):提供具体的产品

public abstract class Video {
    public abstract void produce();
}
public class PythonVideo extends Video {
    @Override
    public void produce() {
        System.out.println("Python课程视频");
    }
}
public class JavaVideo extends Video {
    @Override
    public void produce() {
        System.out.println("Java课程视频");
    }
}
public class VideoFactory {
    public Video getVideo(String type){
        if("java".equalsIgnoreCase(type)){
            return new JavaVideo();
        }else if("python".equalsIgnoreCase(type)){
            return new PythonVideo();
        }
        return null;
    }
}
 public static void main(String[] args) {
   VideoFactory videoFactory = new VideoFactory();
   Video video = videoFactory.getVideo("java");
   if(video == null){
      return;
   }
   video.produce();
}

改进版:
通过上面的代码,证实了之前提到的缺点(增加新的产品需要修改工厂类的判断逻辑),这里我们可以通过反射来修改工厂类进行改进:

public class VideoFactory {
    public Video getVideo(Class c){
        Video video = null;
        try {
            video = (Video) Class.forName(c.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return video;
    }
}
public static void main(String[] args) {
      VideoFactory videoFactory = new VideoFactory();
      Video video = videoFactory.getVideo(JavaVideo.class);
      if(video == null){
          return;
      }
      video.produce();
  }

这样当我们在新增产品的时候就不在需要修改工厂类了。

简单工厂在源码中的使用--Calendar:

  Calendar cal = Calendar.getInstance(zone.toTimeZone(), locale);
  public static Calendar getInstance(TimeZone zone, Locale aLocale) {
      return createCalendar(zone, aLocale);
  }

  private static Calendar createCalendar(TimeZone zone, Locale aLocale) {
      '部分删减'
      Calendar cal = null;

      if (aLocale.hasExtensions()) {
          String caltype = aLocale.getUnicodeLocaleType("ca");
          if (caltype != null) {
              switch (caltype) {
              case "buddhist":
              cal = new BuddhistCalendar(zone, aLocale);
                  break;
              case "japanese":
                  cal = new JapaneseImperialCalendar(zone, aLocale);
                  break;
              case "Gregory":
                  cal = new GregorianCalendar(zone, aLocale);
                  break;
              }
          }
      }
      if (cal == null) {
          if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
              cal = new BuddhistCalendar(zone, aLocale);
          } else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
                     && aLocale.getCountry() == "JP") {
              cal = new JapaneseImperialCalendar(zone, aLocale);
          } else {
              cal = new GregorianCalendar(zone, aLocale);
          }
      }
      return cal;
  }



工厂方法:

模式介绍

定义与类型

使用场景

缺点

UML类图:

  Factory_Method.png

 

工厂方法有4个对象:
抽象工厂类(Factory):提供抽象方法供具体工厂实现
具体工厂类(ConcreteFactory):提供具体的工厂
抽象产品类(Product):提供抽象方法供具体产品类实现
具体产品类(ConcreteProduct):提供具体的产品

public abstract class Video {
    public abstract void produce();
}
public class PythonVideo extends Video {
    @Override
    public void produce() {
        System.out.println("Python课程视频");
    }
}
public class JavaVideo extends Video {
    @Override
    public void produce() {
        System.out.println("Java课程视频");
    }
}
public abstract class VideoFactory {
    public abstract Video getVideo();
}
public class PythonVideoFactory extends VideoFactory {
    @Override
    public Video getVideo() {
        return new PythonVideo();
    }
}
public class JavaVideoFactory extends VideoFactory {
    @Override
    public Video getVideo() {
        return new JavaVideo();
    }
}
public static void main(String[] args) {
      VideoFactory videoFactory = new PythonVideoFactory();
      Video video = videoFactory.getVideo();
      video.produce();
  }
简单工厂在源码中的使用--Collection:
Iterator<E> iterator();
 public Iterator<E> iterator() {
        return new Itr();
 }
public interface Iterator<E> {
    boolean hasNext();

    E next();

    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}
  private class Itr implements Iterator<E> {
      int cursor;       // index of next element to return
      int lastRet = -1; // index of last element returned; -1 if no such
      int expectedModCount = modCount;

      Itr() {}

      public boolean hasNext() {
          return cursor != size;
      }
      '省略代码...'
  }


作者:二妹是只猫
链接:https://www.jianshu.com/p/6dfb5b66d088
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

标签:www,return,6dfb5b66d088,工厂,aLocale,Video,https,new,public
来源: https://www.cnblogs.com/wcss/p/12970352.html