01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //
09: //
10:
11: package de.uka.ilkd.key.casetool.patternimplementor.patterns;
12:
13: import de.uka.ilkd.key.casetool.patternimplementor.*;
14:
15: public class Singleton implements AbstractPatternImplementor {
16: private PIParameterGroup singleton;
17: private ConstraintMechanism constraintMechanism;
18:
19: public String getName() {
20: return "Creational Patterns:Singleton";
21: }
22:
23: public PIParameterGroup getParameters() {
24: if (singleton == null) {
25: createParameters();
26: }
27:
28: return singleton;
29: }
30:
31: protected void createParameters() {
32: singleton = new PIParameterGroup("SingletonPattern",
33: "Singleton Pattern");
34:
35: PIParameterString s_class = new PIParameterString(
36: "Singleton_class", "Singleton", "Singleton");
37: PIParameterString s_instance = new PIParameterString(
38: "unique_instance_attribute",
39: "Unique instance attribute", "instance");
40: PIParameterString s_getInstance = new PIParameterString(
41: "s_getInstance", "Instance method", "getInstance");
42:
43: singleton.add(s_class);
44: singleton.add(s_instance);
45: singleton.add(s_getInstance);
46: constraintMechanism = new ConstraintMechanism(
47: "Singleton.constraints", singleton, this );
48:
49: /*System.out.println("Singleton {");
50: System.out.println(singleton);
51: System.out.println("}");*/
52: }
53:
54: public SourceCode getImplementation() {
55: String s_class = singleton.get("Singleton_class").getValue();
56: String s_instance = singleton.get("unique_instance_attribute")
57: .getValue();
58: String s_getInstance = singleton.get("s_getInstance")
59: .getValue();
60:
61: SourceCode sc = new SourceCode();
62: sc.beginClass(s_class);
63: sc.add("/**");
64: sc.add(constraintMechanism.getConstraints(" * ", "Singleton",
65: s_class));
66: sc.add(" */");
67: sc.add("class " + s_class + "{");
68: sc.add("\t/**");
69: sc.add("\t * @supplierRole " + s_instance);
70: sc.add("\t */");
71: sc.add("\tprivate static " + s_class + " " + s_instance + ";");
72: sc.add("\t/*");
73: sc.add(constraintMechanism.getConstraints("\t * ",
74: "Singleton::create", s_class + "::" + s_class));
75: sc.add("\t */");
76: sc.add("\tprotected " + s_class + "() {}");
77: sc.add("\t/*");
78: sc.add(constraintMechanism.getConstraints("\t * ",
79: "Singleton::getInstance", s_getInstance));
80: sc.add("\t */");
81: sc.add("\tpublic static " + s_class + " " + s_getInstance
82: + "() {");
83: sc.add("\t\tif(" + s_instance + " == null) {");
84: sc.add("\t\t\t" + s_instance + " = new " + s_class + "();");
85: sc.add("\t\t}");
86: sc.add("\t\treturn " + s_instance + ";");
87: sc.add("\t}");
88: sc.add("}");
89:
90: return sc;
91: }
92: }
|