Java: Two common design patterns (singleton mode and Factory mode)

Source: Internet
Author: User

One, Singleton mode: That is, a class from beginning to end only one instance. There are two ways to implement
(1) defines a class whose construction method is private and has a private static variable that is instantiated at initialization time and obtains the object through a public static method.
Java code
1.//The first form of the singleton pattern
2. public class Singleton01 {
3.
4.//private static properties
5. private static Singleton01 instance = new Singleton01 ();
6.
7.//Private method of construction
8. Private Singleton01 () {
9.}
10.
11.//public static method
public static Singleton01 getinstance () {
return instance;
14.}
15.}
(2) The improved
Java code
1. public class Singleton02 {
2.
3.//private Static class variable
4. private static SINGLETON02 instance = NULL;
5.
6.//Private method of construction
7. Private Singleton02 () {
8.}
9.
10.//Static Public method
public static Singleton02 getinstance () {
if (instance = = null) {
Instance = new Singleton02 ();
14.}
. return instance;
16.}
17.}

second, the factory model: the production of different objects of the public interface
(1) Product interface
Java code
1.//Product interface
2. Public interface Product {
3.
4.}
(2) Product Interface implementation class
Java code
1.//washing machine
2. Public class Washer implements PRODUCT {
3.
4. Public washer () {
5. SYSTEM.OUT.PRINTLN ("The washing machine was made!" ");
6.}
7.}
Java code
1.//Air Conditioning
2. Public class Aircondition implements Product {
3.
4. Public aircondition () {
5. SYSTEM.OUT.PRINTLN ("Air conditioner is manufactured! ");
6.}
7.}
(3) Factory interface
Java code
1.//Factory interface
2. Public interface Factory {
3.
4.//Production of products
5. Product Produce (String productName);
6.
7.}
(4) The implementation class of the factory
Java code
1.//Implement Factory
2. Public class Testfactory implements Factory {
3.
4.//Production of products
5. Public Product Produce (String productName) {
6. if (Productname.equals ("washer")) {
7. Return new washer ();
8.}
9. if (Productname.equals ("aircondition")) {
return new Aircondition ();
11.}
return null;
13.}
14.
public static void Main (string[] args) {
Testfactory testfactory = new Testfactory ();
Testfactory.produce ("washer");
Testfactory.produce ("aircondition");
19.}
20.}

Java: Two common design patterns (singleton mode and Factory mode)

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.