The creation pattern of Java design patterns

Source: Internet
Author: User

There are five types of Creation modes: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.
Single-case mode: Original Link: How to write the singleton pattern correctly

meaning : In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources .

recommended notation One (static inner class):
public class Singleton {      private static class Singletonholder {          private static final Singleton INSTANCE = new Sing Leton ();      }      Private Singleton () {} public      static final Singleton getinstance () {          return singletonholder.instance;     }  }

Recommended notation Two (enumeration):
public enum easysingleton{    INSTANCE;}

Factory mode:original link: In a comprehensible factory model meaning : Factory mode is the most commonly used instantiation object pattern, and it is a pattern that replaces the new operation with a factory method . (Decide which instance to create based on incoming parameters)

Chestnuts:
Abstract product role public interface Car {public void drive (); }//Specific product role one public class Benz implements Car {public void Drive () {System.out.println ("Driving B        Enz "); }}//Specific product role two public class BMW implements Car {public void Drive () {System.out.println ("Drivi        ng BMW "); }}//factory class Role public class Driver {//factory method. Note Return type is abstract product role public static Car Drivercar (String s) throw s Exception {//Judgment logic, return specific product roles to client if (S.equalsignorecase ("Benz")) return new Benz            ();            else if (s.equalsignorecase ("BMW")) return new BMW ();        else throw new Exception (); }}//Welcome upstart ... public class magnate {public static void main (string[] args) {Car car = D River.drivercar ("Benz");//Tell the driver that I am riding the Mercedes Car.drive today ();//Next command: drive}}


Cond.....


The creation pattern of Java design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.