Java Design Patterns-singleton mode and template mode

Source: Internet
Author: User

1. Singleton mode method: Class construction method Private, the class has a static class object to hold the class object , and this class provides a static method to get the class object.

/** * Single case design mode * @author Admin * */public class Design1 {private String name;private static Design1 d;private Design1 (String Name) {this.name = name;} public static Design1 Getdesign () {if (d==null) {System.out.println ("fabric");d = new Design1 ("ddd");} return D;} @Overridepublic String toString () {return name+ "singleton";} public static void Main (string[] args) {Design1 d = design1.getdesign (); System.out.println (d);}}

Example results:



2. Template design mode :

It is described that when you define a parent class, you should implement as much of the common functionality of each subclass as possible. Different functions are implemented by subclasses. That is, the invariant part is implemented by the parent class, and the variable part is implemented by the subclass . but it limits the randomness of programming.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Design Patterns-singleton mode and template 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.