Java design mode-singleton mode and template Mode

Source: Internet
Author: User

Java design mode-singleton mode and template Mode

1. Singleton mode method: the class construction method is private. A static class object in the class is used to save this class object, and this class provides a static method to obtain this class object.

 

/*** Singleton 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 (constructor); 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 result:

 

 

 

2. template design mode:

It describes how to implement the common functions of each subclass as much as possible when defining a parent class. Different functions are implemented by sub-classes. That is, the unchanged part is implemented by the parent class, and the variable part is implemented by the Child class, but it limits the randomness of the program design.


 

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.