Java design pattern -- simple factory pattern

1. Simple factory pattern definition Provides a function of creating object instances without caring about their impleme...

1. Simple factory pattern definition

Provides a function of creating object instances without caring about their implementation. The type of the created instance can be an interface, an abstract class, or a concrete class.

2. The simple factory contains the following roles

  • CarFactory: a simple factory for producing cars
    The simple factory that produces cars is responsible for realizing all the logic of producing cars
  • Car: Abstract automotive products
    Abstract automobile product is the parent class of all automobiles and is responsible for describing the properties and behaviors of all automobiles
  • Porsche: Porsche cars
    The specific product is the production of Porsche cars.
  • Ferrari: Ferrari
    The specific product is Ferrari.

3. The simple factory pattern is represented by UML class diagram

4. The simple factory mode is represented by code

  • Vehicle interface
/** * Automobile interface */ public interface Car { void run(); }
  • Porsche
/** * Porsche */ public class Porsche implements Car { @Override public void run() { System.out.println("Porsche running ..."); } }
  • Ferrari
public class Ferrari implements Car { @Override public void run() { System.out.println("Ferrari running ..."); } }
  • Vehicle factory
/** * Vehicle factory * */ public class CarFactory { public static Car getCar(String carType) { switch (carType) { case "ferrari": return new Ferrari(); case "porsche": return new Porsche(); default: throw new RuntimeException("No such vehicle"); } } }
  • Receive customer orders and start production
/** * Simple factory mode * */ public class SimpleFactoryPattern { public static void main(String[] args) { // First day driving Ferrari String carType = "ferrari"; CarFactory.getCar(carType).run(); // Drive a Porsche the next day carType = "porsche"; CarFactory.getCar(carType).run(); System.out.println("Happiness is so simple"); } }
  • Execution results:

5. Advantages and disadvantages of factory mode

advantage:

  • encapsulation
    The simple factory helps us realize the encapsulation of components and enable the external implementation of interface oriented programming

  • decoupling
    Through a simple factory, the client and the specific logic are decoupled

  • decoupling
    Through a simple factory, the client and the specific logic are decoupled

Disadvantages:

  • Increase the complexity of expansion
    Adding a new product requires modifying the factory logic. When there are many product types, the factory will be too bloated and increase complexity.

6. When to choose a simple factory

  • Encapsulate the specific implementation, and let the client operate the specific implementation through the interface.
  • Want to centrally manage and control the specific responsibilities of creating objects

7. Application of simple factory model

  • Application of simple factory pattern in Java
public static Calendar getInstance() { return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT)); } 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; // Application of simple factory model 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; } } } // Application of simple factory model 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; }
  • Application of simple factory pattern in Spring

mark, welcome to comment

16 October 2021, 03:05 | Views: 4436

Add new comment

For adding a comment, please log in
or create account

0 comments