singlespoon.java

来自「仅学习使用」· Java 代码 · 共 36 行

JAVA
36
字号
package singleton;

public class SingleSpoon {
	private Soup soupLastUsedWith;
	private static SingleSpoon theSpoon;
	private static boolean theSpoonIsAvailable = true;

	private SingleSpoon() {}

	public static SingleSpoon getTheSpoon() {
		if (theSpoonIsAvailable) {
			if (theSpoon == null){
				theSpoon = new SingleSpoon();
			}
			theSpoonIsAvailable = false;
			return theSpoon;
		}else {
			//spoon not available, could throw error or return null (as shown)
			return null;
		}
	}
	public String toString() {
		return "Behold the glorious single spoon!";
	}

	public static void returnTheSpoon() {
		theSpoon.cleanSpoon();
		theSpoonIsAvailable = true;
	}
	
	public Soup getSoupLastUsedWith() { return this.soupLastUsedWith; }
	
	public void setSoupLastUsedWith(Soup soup) { this.soupLastUsedWith = soup; }
	public void cleanSpoon() { this.setSoupLastUsedWith(null); }
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?