vjame

          優化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數據加載中……

          單態模式和簡單工廠模式

          單態模式
           Singleton模式主要作用是保證在Java應用程序中,一個類Class只有一個實例存在。
          在項目的很多地方都會用到它,比如說數據庫的鏈接。
          使用Singleton的好處還在于可以節省內存,因為它限制了實例的個數,有利于Java垃圾回收。

          1. 編寫代碼
          package com.strongit.singleton;


          class A {
              
          private static final A a = new A();
              
          public static A getSingleInstance(){
                  
          return a;
              }
              
          public void say(){
                  System.out.println(
          "hello");
              }
          }

          public class SingletonDemo {

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub
                      A a = A.getSingleInstance();
                      a.say();
                      System.out.println(A.getSingleInstance());

                      System.out.println(A.getSingleInstance());
                  
              }

          }

          2.運行結果
          hello...
          com.strongit.singleton.A@1c78e57
          com.strongit.singleton.A@1c78e57


          簡單工廠模式
          簡單工廠模式又叫靜態工廠模式,顧名思義,它是用來實例化目標類的靜態類。下面我主要通過一個簡單的實例說明簡單工廠及其優點。 

          package com.strongit.factory;


          //抽象汽車
          interface Car{
              
          public void run();
              
          public void stop();
          }

          //奔馳汽車
          class Benz implements Car{
              
          public void run() {
                  System.out.println(
          "BMW run ");
              }

              
          public void stop() {
                  System.out.println(
          "BMW stop  ");
              }
          }

          //福特汽車
          class Ford implements Car{

              
          public void run() {
                  System.out.println(
          "Ford run ");
              }
              
          public void stop() {
                  System.out.println(
          "Ford stop ");
              }
              
          }

          //工廠幫助客服返回實例化對象
          class Factory{
              
          public static Car getCarInstance(String type){
                  Car car 
          = null;
                  
          try {
                      
          //java反射機制
                      car = (Car)Class.forName("com.strongit.factory."+type).newInstance();
                  } 
          catch (InstantiationException e) {
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  }
                  
          return car;
              }
          }


          //客服端調用
          public class FactoryDemo {

              
          public static void main(String[] args) {
                  String type 
          = "Ford";
                  Car car 
          = Factory.getCarInstance(type);
                  
          if(car == null){
                      System.out.println(
          "error ");
                  }
          else{
                      car.run();
                  }
              }
          }

          posted on 2008-10-30 16:47 lanjh 閱讀(300) 評論(0)  編輯  收藏 所屬分類: 設計模式


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 屏东市| 禹城市| 江城| 台东市| 镇坪县| 云和县| 虎林市| 利川市| 江陵县| 贵定县| 加查县| 台北县| 碌曲县| 贵港市| 潜江市| 尤溪县| 顺义区| 墨竹工卡县| 湘乡市| 望江县| 交城县| 弥渡县| 遵化市| 长武县| 商城县| 故城县| 黄石市| 河曲县| 繁昌县| 古浪县| 五寨县| 凤台县| 保山市| 库尔勒市| 尉氏县| 竹溪县| 巫山县| 资源县| 白玉县| 越西县| 噶尔县|