Java设计模式

来源:互联网 发布:顽固软件卸载工具 编辑:程序博客网 时间:2024/06/08 07:06
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

1:单实例类

public class ConnectionPool {
 //only one instance is needed
 private static ConnectionPool instance;
 private ConnectionPool() {
  System.out.println("Createing the single instance of connection pool");
 
 }

 public static ConnectionPool getInstance() {
  if(instance != null) {
   System.out.println("Getting existing instance of connection pool ");
   return instance;
  } else {
   instance = new ConnectionPool();
   return instance;
  }

 }
 public static void main(String args[]) {
  System.out.println("Get connection pool first time");
  ConnectionPool cPool = ConnectionPool.getInstance();
  System.out.println("Get Connection pool second time ");
  ConnectionPool annotherPool = ConnectionPool.getInstance();
 
 }


}

适合于数据库连接池的使用,但是单实例很难扩展,,应该呗设计为并行执行。因为多线程将访问同一个单实例对象。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击