我思故我強

          簡單的工廠模式

          工廠模式的思想主要為:多個類似的子類繼承同一個父類,對其父類中的變量進行操作;工廠類負責判斷、控制哪個子類被執行,而工廠類調用子類完成后,返回的結果是該子類的父類,該父類中的變量已經被操作過了,訪問該父類,得到我們想要的結果。
          public class Father??? {
          ??? protected static String one;
          ??? protected static String two;
          }

          ?class Son1 extends Father
          ?? {
          ??? public Son1()
          ?????? {
          ??????? one="son1";
          ??? }
          }

          ?class Son2 extends Father
          ?? {
          ??? public Son2()
          ?????? {
          ??????? one="son2";
          ??? }
          }

          ?class Factory
          ?? {
          ??? public Father getSon(String s)
          ?????? {
          ??????? if(s.equals("1"))
          ?????????? {
          ??????????? return new Son1();
          ??????? }
          ??????? else
          ?????????? {
          ??????????? return new Son2();
          ??????? }
          ??? }
          ???
          ??? //main
          ??? public static void main(String [] args)
          ?????? {
          ??????? Factory factory=new Factory();
          ??????? Father father=factory.getSon("2");
          ???????
          ??????? //print
          ??????? System.out.println(father.one);
          ??????? System.out.println(father.two);
          ??? }
          }


          ?

          posted on 2008-02-26 09:29 李云澤 閱讀(248) 評論(0)  編輯  收藏 所屬分類: 設計模式

          主站蜘蛛池模板: 旅游| 河北省| 金门县| 尉氏县| 普格县| 郓城县| 望江县| 乌鲁木齐县| 长春市| 德江县| 高平市| 海林市| 宜昌市| 巧家县| 泸水县| 津南区| 瑞丽市| 伽师县| 改则县| 偃师市| 连云港市| 田东县| 林口县| 根河市| 江达县| 潜江市| 夏津县| 遂昌县| 阿图什市| 安康市| 锡林浩特市| 甘南县| 裕民县| 长汀县| 威宁| 香河县| 界首市| 云阳县| 称多县| 曲周县| 方城县|