设计原则
封装、继承、多态、抽象分别可以解决哪些编程问题
封装:也叫做信息隐藏或数据保护访问。数据 通过暴露有限的访问接口,授权外部仅能通过类提供接口访问,对内的类private私有化属性,通过封装简化操作,让用户更好使用,隐藏信息、保护数据
抽象:主要是讲如何隐藏方法的具体实现,让调用者之需要关系方法提供了哪些功能,并不需要知道这些功能是如何实现的,可借助interface和abstract两个关键字实现,简化操作,让用户只关心功能点,不用关心具体实现
继承:继承的好处就是代码复用,Java只支持单一继承
多态:多态就是指子类可以替代父类,多态能提供代码的扩展性和复用性
设计模式开源实战
工厂模式在Calendar类只能够的应用
大多数工厂模式都是以Factory作为后缀来命名。但是实际运用过程中非常灵活,比如java JDK中的一个应用:java.util.Calendar。从命名上不好看出它是一个工厂类
Calendar类,提供了getInstanc()工厂方法获取不同的TimeZone和Locale,创建不同的Calendar子类对象
public static Calendar getInstance(TimeZone zone,Locale aLocale){return createCalendar(zone, aLocale);}private static Calendar createCalendar(TimeZone zone,Locale aLocale){CalendarProvider provider =LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale).getCalendarProvider();if (provider != null) {try {return provider.getInstance(zone, aLocale);} catch (IllegalArgumentException iae) {// fall back to the default instantiation}}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 no known calendar type is explicitly specified,// perform the traditional way to create a Calendar:// create a BuddhistCalendar for th_TH locale,// a JapaneseImperialCalendar for ja_JP_JP locale, or// a GregorianCalendar for any other locales.// NOTE: The language, country and variant strings are interned.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;}
建造者模式在Calendar类中的应用
private String type;private TimeZone zone;private boolean lenient = true;private Locale locale;private int firstDayOfWeek, minimalDaysInFirstWeek;/*** Constructs a {@code Calendar.Builder}.*/public Builder() {}public Builder setInstant(long instant) {if (fields != null) {throw new IllegalStateException();}this.instant = instant;nextStamp = COMPUTED;return this;}public Builder setInstant(Date instant) {return setInstant(instant.getTime()); // NPE if instant == null}public Builder set(int field, int value) {// Note: WEEK_YEAR can't be set with this method.if (field < 0 || field >= FIELD_COUNT) {throw new IllegalArgumentException("field is invalid");}if (isInstantSet()) {throw new IllegalStateException("instant has been set");}allocateFields();internalSet(field, value);return this;}
既然有了getInstance()工厂方法来创建Calendar类对象,为什么还要用Builder来创建Calendar类对象呢?这两者的区别在哪里呢?
工厂模式是用来创建不同但相关类型的对象(继承同一父类或接口的一组子类),由给定的参数来决定创建哪种类型的对象。建造者模式用来创造一种类型的复杂的对象,通过设置不同的可选参数,定制化的创建不同的对象
装饰器模式在Collections类中的应用
适配器模式在Collections类中的应用
适配器模式可以用来兼容新旧老版本的接口。老版本的JDK提供了Enumeration类来遍历容器。新版本的JDK用Iterator类来替代Enumeration类来遍历容器。
持续更新中