The creation pattern of Java design patterns

Source: Internet
Author: User

The creation pattern is divided into five categories: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode

First, factory method mode: interface-Implementation Class-factory class

The purpose of the factory class is to produce sender objects, which are passed in by different parameters to produce different objects.

Changing the factory method in the factory class to multiple factory methods is the design pattern for multiple factory methods

Static Factory method design mode by changing the factory method in plant class to static

Second, abstract factory method mode: interface-Implementation Class-factory class

Features are:

The factory class implements an interface that can be extended later, such as a factory class that increases Bluetooth.

Factory design patterns are a special case of abstract factory design patterns.

Three, single case mode

Singleton object (Singleton), which guarantees that only one instance of the object exists in a JVM.

A hungry man mode: classes are created when loaded and slow to load classes, but run-time gets objects faster and thread-safe

/* Type: A hungry man mode */public class Singleton {//1. Privatization of the construction method, not allowing external direct creation of the object private Singleton () {}//2. Create a unique instance of the class, using the private static modifier private static Singleton instance=new Singleton ();//3. Provides a method for obtaining an instance, using public static to decorate public static Singleton GetInstance () {return instance;}}

Lazy mode: Class loading when not to create, loading classes faster, the first user gets to create an instance, the first time to get objects slower, thread insecure

/* Lazy mode */public class Singleton2 {//1. Privatization of construction, not allowing the creation of objects directly outside private Singleton2 () {}//2. Declaring a unique instance of a class, using private static modifier private static Singleton2 INSTANCE;//3. Provides a method for getting an instance, using public static to decorate public static Singleton2 getinstance () { if (instance==null) {instance=new Singleton2 ();} return instance;}}



The creation pattern of Java design patterns

Related Article

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: [email protected] 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.