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