当前位置:首页 > 设计模式 > 正文内容

单例模式

canca19年前 (2007-03-26)设计模式475

//CopyRight CAnca Software Office. 2006
//Created by CAnca.

import java.util.*;

public class singletonMode{
 public static void main(String[] args){
  singleton s1 = singleton.init();
  System.out.println(s1.getElement(0));
  System.out.println(s1.getElement(1));
  
  s1.setElement(0,"日本人");
  
  singleton s2 = singleton.init();
  System.out.println(s2.getElement(0));
  System.out.println(s2.getElement(1));
 }
}

class singleton{
 private ArrayList al;
 private static singleton single;
 protected singleton(){
  al = new ArrayList();
  al.add("中国人");
  al.add("美国人");
 }
 
 public static singleton init(){
  if(single == null){
   single = new singleton();
  }
  return single;
 }
 
 public String getElement(int id){
  if(id >= al.size()) - 1;
  return ((String)al.get(id));
 }
 
 public void setElement(int id,String value){
  if(id >= al.size()) - 1;
  this.al.set(id,value);
 }
}

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/668.html

标签: 设计模式
分享给朋友:

“单例模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ public static void main(String[] args){  ...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]...

适配器模式

//适配器模式//CopyRight(C)CAnca Software Office.//Created by CAnca. public class AdapterMode{ public static void main(String[] args){  Adapt...

外观模式

//外观模式//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. public class FacadeMode{ public static void main(String[] args){  Fa...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。