Java Design Mode Creation Mode - Singleton (DEMO)

xiaoxiao2021-03-06  38

-------------------------------------------------- ----------------- Singletona.java ----------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- "

/ ** * email: Meconsea@hotmail.com *

Title: *

description: a Singleton We can Only Create One Instance *

Copyright: CopyRight (C) 2005 *

company: * @Author meConsea * @version 1.0 * /

Public class singletona {

Public static boolean instanceflag = false;

Private INT i = 0;

/ ** * set constructor private and do nothing * can not a new instance outside class * / private singleletona () {}

Public static singleletona instance = null;

Public int getnum () {returnim.

Public void setnum (INT i) {this.i = i;}

Public Static Singletona getInstance () {if (! instanceflag) {instanceflag = true; instance = new singleletona ();} instance.setnum (instance.getnum () 1); return instance;}

Public void finalize () {instanceflag = false;

} ------------------------------------- -------------------------------- Testa.java -------------- -------------------------------------------------- ------------------ Package CreatePattern;

/ ** * email: Meconsea@hotmail.com *

Title: *

description: *

Copyright: CopyRight (C) 2005 *

Company: * @Author meConsea * @version 1.0 * /

Public class testa {public static void main (String args [] {

Singletona Singletona = Singletona.GetInstance (); int i = singletona.getnum (); System.out.Println ("i ==" i);

Singletona Singletona2 = singletona.getinstance (); int Ii = singletona2.getnum (); system.out.println ("ii ==" ii);}} --------------- -------------------------------------------------- -Nsingletonb.java ---------------------------------------------- -------------------- Package createpattern; / ** * email: Meconsea@hotmail.com *

title: *

Description: A New Singleton Use Registry *

Copyright: Copyright (C) 2005 *

Company: * @Version 1.0 * / Import java.util.hashmap Public class singleletonb {

Static private hashmap registrymap = new hashmap ();

Public Static Void Registry (String Name, Singletonb Ainstance) {registrymap.put (name, ainstance);

Public Static Singletonb GetInstance (String Name) {Return Lookup (Name);

Protected static singleletonb look (string name) {return (Singletonb) registryMap.get (name);}} ------------------------------------------------------------------------------------------------------------------------ --------------------------------------- Singletonexception.java -------- -------------------------------------------------- ---------- Package CreatePattern;

/ ** * email: Meconsea@hotmail.com *

title: *

Description: a user defined exception *

Copyright: CopyRight (c) 2005 *

Company: * @Author meconsea * @version 1.0 * /

Public Class Singletonexception Extends RuntimeException {PUBLIC SINGLETONEXCEPTION () {super ();

Public Singletonexception (String S) {SUPER (s);

} ------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------ package createpattern;

/ ** * email: Meconsea@hotmail.com *

title: *

Description: a real singleton we want to have *

Copyright: CopyRight (C) 2005 < / p> *

Company: * @Author not attributable * @version 1.0 * / public class subsingletonb extends singleletonb {

// True IF 1 Instance

Public static boolean instanceflag = false;

public SubSingletonB () throws SingletonException {if (instanceFlag) {throw new SingletonException ( "Only can create a instance");} else {instanceFlag = true; super.registry ( "sub", this);}}

// finalize public void finalize () {instanceflag = false;

} ------------------------------------- ----------------- Testb.java ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------ Package CreatePattern;

/ ** * email: Meconsea@hotmail.com *

Title: *

description: *

Copyright: CopyRight (C) 2005 *

Company: * @Author meConsea * @version 1.0 * /

Public class testb {public testb () {}

Public static void main (string args []) {/ ** * first we get a instance from start1 = singletonb.getinstance ("sub"); if (instance1 == null) {system.out.println "There is no claim instance in registry!");} Else {system.out.println (" instance1.getclass ());}

/ ** * THEN We register a new instance * / try {singletonb instance2 = new subsisingletonb (); system.out.println ("We Had created a new instance name /" sub / "now.");} Catch (Singletonexception SE) {system.out.println ("When Create Singletonb Occur The Exception" Se.getMessage ());} / ** * to get instance again * / instance1 = singletonb.getinstance ("sub"); if (instance1 == NULL) {System.out.println ("there is no claim instance in registry!");} else {system.out.println ("" instance1.getclass ());}

/ ** * finally we create a new instance again * / try {singletonb instance3 = new subsingletonb (); system.out.println ("We Had Create a new instance name /" number ");} catch Singletonexception se) {system.out.println (" se.getMessage ());}}} --------------------------- ---------------- End ----------------------------------------------------------------------------- ------------

转载请注明原文地址:https://www.9cbs.com/read-66773.html

New Post(0)