创建型模式,就是创建对象的模式,抽象了实例化的过程。它帮助一个系统独立于如何创建、组合和表示它的那些对象。关注的时对象的创建,创建型模式将创建对象的过程进行了抽象,也可以理解为将创建对象的过程进行了封装,作为客户程序仅仅需要去使用对象,而不再关心创建过程中的逻辑。
具体的创建型模式可分为:
所谓的单例设计模式,就是采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法。例如:Hibernate的SessionFactory,它充当数据存储源的代理,并负责创建Session对象。SessionFactory并不是轻量级的,一般情况下,一个项目通常只需要一个SessionFactory就够,这时就需要使用单例模式。
单例设计模式有八种方式(推荐使用1、2、6、7、8):
class Singleton{ private static final Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getInstant() { return singleton; } }
优点:这种写法比较简单,就是在类装载的时候就完成实例化,避免了线程同步问题。
缺点:在类装载的时候就完成实例化,没有达到Lazy Loading的效果。如果从始至终从未使用过这个实例,则会造成内存的浪费。
结论:这种单例模式可用,但是可能造成内存浪费
class Singleton1{ private static Singleton1 singleton; private Singleton1(){} static { singleton = new Singleton1(); } public static Singleton1 getInstance(){ return singleton; } }
优缺点:这种方式和上面的方式其实类似,只不过将类实例化的过程放在了静态代码块中,也是在类装载的时候,就执行静态代码块中的代码,初始化类的实例。优缺点和上面是一样的。
结论:这种单例模式可用,但是可能造成内存浪费。
class Singleton2{ private static Singleton2 singleton; private Singleton2(){} public static Singleton2 getSingleton(){ if (singleton == null) { singleton = new Singleton2(); } return singleton; } }
class Singleton3{ private static Singleton3 singleton; private Singleton3(){} public static synchronized Singleton3 getSingleton(){ if (singleton == null) { singleton = new Singleton3(); } return singleton; } }
class Singleton4{ private static Singleton4 singleton4; private Singleton4(){} public static Singleton4 getInstance() { if (singleton4 == null) { synchronized (Singleton4.class) { singleton4 = new Singleton4(); } } return singleton4; } }
class Singleton5{ private static volatile Singleton5 singleton; private Singleton5(){} public static Singleton5 getSingleton(){ if (singleton == null) { synchronized (Singleton5.class) { if (singleton == null) { singleton = new Singleton5(); } } } return singleton; } }
class Singleton6 { private Singleton6(){} private static class StaticClass { private static final Singleton6 INSTANCE = new Singleton6(); } public static Singleton6 getInstance() { return StaticClass.INSTANCE; } }
enum Singleton7{ INSTANCE; }
Runtime源码

需求:
源码:详情
public class OrderPizza { public OrderPizza() { Pizza pizza = null; String orderType; do { orderType = getOrderType(); if ("chess".equals(orderType)) { pizza = new ChessPizza(); pizza.name = "奶酪"; }else if ("greek".equals(orderType)) { pizza = new GreekPizza(); pizza.name = "希腊"; }else if ("china".equals(orderType)) { pizza = new ChinaPizza(); pizza.name = "中国"; } else { break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } }

把创建Pizza对象封装到一个类(工厂类)中,在订购时只需要根据不同的orderType就能获得对应的Pizza类。
源码:详情
public class SimpleFactory { public Pizza getPizza(String type) { Pizza pizza = null; if ("chess".equals(type)) { pizza = new ChessPizza(); pizza.setName("奶酪"); }else if ("greek".equals(type)) { pizza = new GreekPizza(); pizza.setName("希腊"); }else if ("china".equals(type)) { pizza = new ChinaPizza(); pizza.setName("中国"); } return pizza; } } public class OrderPizza { public OrderPizza(SimpleFactory simpleFactory) { do { String type = getOrderType(); Pizza pizza = simpleFactory.getPizza(type); if (pizza == null) { System.out.println("当前pizza的种类没有。"); break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } }

如果需要重新添加一个pizza的种类,需要在添加一个类继承Pizza类(可选择重写方法),在简单工厂中添加对应的创建逻辑。不需要在每个OrderPizza类中修改代码。
需求变动:客户在点披萨时,可以点不同口味的披萨,比如 北京的奶酪 pizza、北京的胡椒 pizza 或者是伦敦的奶酪 pizza、伦敦的胡椒 pizza。
源码:详情
//创建一个抽象的订购披萨类,根据地区不同都需要继承这个类,然后再根据口味确定最终的成品 public abstract class OrderPizza { abstract Pizza createPizza(String type); public OrderPizza() { do { String type = getOrderType(); Pizza pizza = createPizza(type); if (pizza == null) { System.out.println("当前pizza的种类没有。"); break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } private String getOrderType() { System.out.println("输入订单类型:chess,pepper"); Scanner scanner = new Scanner(System.in); String next = scanner.next(); System.out.println(next); return next; } } //北京pizza的订购类 public class BJOrderPizza extends OrderPizza{ @Override Pizza createPizza(String type) { Pizza pizza = null; if ("chess".equals(type)) { pizza = new BJChessPizza(); pizza.setName("北京chesses"); } else if ("pepper".equals(type)) { pizza = new BJPepperPizza(); pizza.setName("北京pepper"); } return pizza; } } //伦敦pizza的订购类 public class LDOrderPizza extends OrderPizza{ @Override Pizza createPizza(String type) { Pizza pizza = null; if ("pepper".equals(type)) { pizza = new LDPepperPizza(); pizza.setName("伦敦pepper"); } else if ("chess".equals(type)) { pizza = new LDChessPizza(); pizza.setName("伦敦chesses"); } return pizza; } }

抽象工厂模式:定义了一个interface用于创建相关或有依赖关系的对象簇,而无需指明具体的类。抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合。
从设计层面看,抽象工厂模式就是对简单工厂模式的改进(或者称为进一步的抽象)。
将工厂抽象成两层,AbsFactory(抽象工厂)和具体实现的工厂子类。程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。
如果产品的种类很多的话,使用抽象工厂模式的灵活性会很高;如果产品的种类不多的话,使用简单工厂模式就足够了
源码:详情
Pizza、BJChessPizza、BJPepperPizza、LDChessPizza、LDPepperPizza与工厂方法基本相同。
//定义抽象工厂类 public interface AbsFactory { public Pizza createPizza(String type); } //定义伦敦工厂类 public class LDFactory implements AbsFactory{ @Override public Pizza createPizza(String type) { if ("chess".equals(type)) { return new LDChessPizza(); } else if ("pepper".equals(type)) { return new LDPepperPizza(); } return null; } } //定义北京工厂类 public class BJFactory implements AbsFactory{ @Override public Pizza createPizza(String type) { if ("chess".equals(type)) { return new BJChessPizza(); } else if ("pepper".equals(type)) { return new BJPepperPizza(); } return null; } } //定义订购类 public class OrderPizza { AbsFactory absFactory; public OrderPizza(AbsFactory absFactory) { setFactory(absFactory); } private void setFactory(AbsFactory absFactory) { this.absFactory = absFactory; do { String orderType = getOrderType(); Pizza pizza = absFactory.createPizza(orderType); if (pizza == null) { System.out.println("当前pizza的种类没有。"); break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } private String getOrderType() { System.out.println("输入订单类型:chess,pepper"); Scanner scanner = new Scanner(System.in); String next = scanner.next(); System.out.println(next); return next; } } //定义client用户 public class Test { public static void main(String[] args) { new OrderPizza(new LDFactory()); } }

Calendar中createCalendar源码:
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"); //根据不同的caltype类型匹配对应的类 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") { } else { cal = new GregorianCalendar(zone, aLocale); } } return cal; }
原文:https://www.cnblogs.com/bbgs-xc/p/15174634.html