Singleton mode and Factory mode

Source: Internet
Author: User

1. The singleton model is divided into a hungry man mode and lazy mode;

A Hungry man type:

public class singleton{

private static Singleton Singleton = new Singleton ();

Private Singleton () {}

public static Singleton getinstance () {

return singletion;

}

}

Lazy Type:

public class singleton{

private static Singleton Singleton = null;

public static synchronized synchronized getinstance () {

if (singleton==null) {

Singleton = new Singleton ();

}
return singleton;

}

}

A hungry man is thread-safe, creating a static object at the same time as the class is created for use by the system, and not changing the lazy type. If you create an instance object without adding synchronized, the access to the object is not thread-safe. It is recommended to use the first type.

2. Factory model is divided into simple Factory mode and factory method mode

(1) Simple Factory mode is a specific class to create instances of other classes, the parent class is the same, the parent class is specific.

(2) The factory method pattern is that there is an abstract parent class that defines the public interface, and the subclass is responsible for generating the concrete object, which is intended to defer the instantiation of the class to the child class.

(3) Abstract Factory mode provides an interface to create a series of related or interdependent objects without specifying their specific classes. It is designed for hierarchical structures with multiple products. And the factory method model is for a product grade

Structure.

Singleton mode and Factory mode

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: 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.