vjame

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

          抽象工廠


          女媧造物,她可以造人和動物,用陰繩造女人和雌性動物,用陽繩造男人和雄性動物,人要吃東西,會說話。動物也要吃東西,還會睡覺.
          package com.strongit.factory;

          interface Person{
              
          public void eat();
              
          public void talk();
          }

          class Man implements Person{

              
          public void eat() {
                  
          // TODO Auto-generated method stub
                  System.out.println("男人在吃東西。。。。");
              }

              
          public void talk() {
                  
          // TODO Auto-generated method stub
                  System.out.println("男人在說話。。。。");
              }
              
          }

          interface Animal{
              
          public void eat();
              
          public void sleep();
          }

          class Bull implements Animal{

              
          public void eat() {
                  
          // TODO Auto-generated method stub
                  System.out.println("公牛在吃東西。。。。");
              }

              
          public void sleep() {
                  
          // TODO Auto-generated method stub
                  System.out.println("公牛在睡覺。。。。");
              }
              
          }
          class Cow implements Animal{

              
          public void eat() {
                  
          // TODO Auto-generated method stub
                  System.out.println("母牛在吃東西。。。。");
              }

              
          public void sleep() {
                  
          // TODO Auto-generated method stub
                  System.out.println("母牛在睡覺。。。。");
              }
              
          }

          class Woman implements Person{

              
          public void eat() {
                  
          // TODO Auto-generated method stub
                  System.out.println("女人在吃東西。。。。");
              }

              
          public void talk() {
                  
          // TODO Auto-generated method stub
                  System.out.println("女人在說話。。。。");
              }
              
          }

          //表示女媧
          interface NWFactory{
              
          public Person createPerson(String type);
              
          public Animal createAnimal(String type);
          }

          //陽繩-->用來造男人和雄性動物(Bull)
          class YangSheng implements NWFactory{

              Man man 
          = null;
              Bull bull 
          = null;
              
          public Bull createAnimal(String type) {
                  
          try {
                      bull 
          = (Bull)Class.forName("com.strongit.factory."+type).newInstance();
                  } 
          catch (InstantiationException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          return bull;
              }

              
          public Man createPerson(String type) {
                  
          try {
                      man 
          = (Man)Class.forName("com.strongit.factory."+type).newInstance();
                  } 
          catch (InstantiationException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          return man;
              }
          }

          //陰繩-->用來造女人和雌性動物(Cow)
          class YinSheng implements NWFactory{

              Woman woman 
          = null;
              Cow cow 
          = null;
              
          public Cow createAnimal(String type) {
                  
          try {
                      cow 
          = (Cow)Class.forName("com.strongit.factory."+type).newInstance();
                  } 
          catch (InstantiationException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          return cow;
              }

              
          public Woman createPerson(String type) {
                  
          try {
                      woman 
          = (Woman)Class.forName("com.strongit.factory."+type).newInstance();
                  } 
          catch (InstantiationException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (IllegalAccessException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  
          return woman;
              }
              
          }


          public class FactoryDemo1 {

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub
                  
          //實例化一個陽繩,ys
                  YangSheng ys=new YangSheng();
                  
          //實例化一個陰繩,ys1
                  YinSheng ys1=new YinSheng();
                  
          //造男人和女人,p1是男人,p2是女人
                  Person p1=ys.createPerson("Man");
                  Person p2
          =ys1.createPerson("Woman");
                  
          //造動物,a1是公牛(Bull),a2是母牛(Cow)
                  Animal a1=ys.createAnimal("Bull");
                  Animal a2
          =ys1.createAnimal("Cow");
                  a1.eat();
                  a1.sleep();
                  a2.eat();
                  a2.sleep();
                  p1.eat();
                  p1.talk();
                  p2.eat();
                  p2.talk();

              }

          }

          源代碼下載: http://www.aygfsteel.com/Files/vjame/FactoryDemo03.rar

          posted on 2008-11-26 15:59 lanjh 閱讀(233) 評論(0)  編輯  收藏 所屬分類: 設計模式


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


          網站導航:
           
          主站蜘蛛池模板: 上高县| 简阳市| 且末县| 八宿县| 松江区| 齐河县| 九寨沟县| 南木林县| 庆阳市| 静海县| 伊通| 大渡口区| 长寿区| 静宁县| 南川市| 烟台市| 伊通| 曲阜市| 仲巴县| 米脂县| 涞源县| 镇平县| 邻水| 盱眙县| 沂源县| 开阳县| 德钦县| 大兴区| 贡山| 房山区| 天峻县| 巴楚县| 贵德县| 开远市| 合江县| 徐闻县| 阜新市| 贵南县| 盐源县| 兴城市| 宜兰县|