posts - 51, comments - 17, trackbacks - 0, articles - 9
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java模式之工廠模式

          Posted on 2007-03-29 13:18 chenweicai 閱讀(287) 評(píng)論(0)  編輯  收藏

          簡(jiǎn)單工廠模式(缺點(diǎn):每增加一個(gè)具體產(chǎn)品時(shí) ,就要修改工廠方法,工廠方法負(fù)責(zé)了所有具體產(chǎn)品的創(chuàng)建)

          舉個(gè)例子:
          ------------------
          public interface Fruit {

          ?void grow();
          ?
          ?void harvest();
          ?
          ?void plant();
          }
          -------------------
          public class Apple implements Fruit {

          ?private int treeAge;
          ?
          ?public void grow() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Apple is growing...");
          ??
          ?}

          ?public void harvest() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Apple has been harvested.");
          ?}

          ?public void plant() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Apple has been planted.");
          ??
          ?}

          ?public int getTreeAge() {
          ??return treeAge;
          ?}

          ?public void setTreeAge(int treeAge) {
          ??this.treeAge = treeAge;
          ?}
          ?
          }
          -------------------
          public class Grape implements Fruit {

          ?private boolean seedless;
          ?
          ?
          ?
          ?public boolean isSeedless() {
          ??return seedless;
          ?}

          ?public void setSeedless(boolean seedless) {
          ??this.seedless = seedless;
          ?}

          ?public void grow() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Grape is growing...");
          ?}

          ?public void harvest() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Grape has been harvested.");
          ?}

          ?public void plant() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Grape has been planted.");
          ??
          ?}

          ?
          }


          ---------------------------
          public class Strawberry implements Fruit {

          ?public void grow() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Strawberry is growing...");
          ?}

          ?public void harvest() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Strawberry has been harvested.");
          ?}

          ?public void plant() {
          ??// TODO Auto-generated method stub
          ??System.out.println("Strawberry has been planted.");
          ?}
          }

          -------------------------
          public class FruitGardener {

          ?//靜態(tài)工廠方法
          ?public static Fruit factory(String which) throws BadFruitException{
          ??if(which.equalsIgnoreCase("apple")){
          ???return new Apple();
          ??}
          ??else if(which.equalsIgnoreCase("strawberry")){
          ???return new Strawberry();
          ??}
          ??else if(which.equalsIgnoreCase("grape")){
          ???return new Grape();
          ??}
          ??else{
          ???throw new BadFruitException("Bad fruit request");
          ??}
          ?}
          }

          ---------------------------
          public class BadFruitException extends Exception {

          ?public BadFruitException(String msg){
          ??super(msg);
          ?}
          }


          --------------------------
          public class Client {

          ?public static void main(String[] args){
          ??
          ??try{
          ???Fruit apple = (Fruit)FruitGardener.factory("Apple");
          ???System.out.println("apple is class: " + apple.getClass().getName());
          ???apple.plant();
          ???apple.grow();
          ???apple.harvest();
          ???System.out.println();
          ???
          ???Fruit grape = (Fruit)FruitGardener.factory("grape");
          ???System.out.println("grape is class: " + grape.getClass().getName());
          ???grape.plant();
          ???grape.grow();
          ???grape.harvest();
          ???System.out.println();
          ???
          ???Fruit strawberry = (Fruit)FruitGardener.factory("strawberry");
          ???System.out.println("strawberry is class: " + strawberry.getClass().getName());
          ???strawberry.plant();
          ???strawberry.grow();
          ???strawberry.harvest();
          ???
          ??}catch(BadFruitException e){
          ???e.printStackTrace();
          ??}
          ?}
          }


          2 工廠方法模式:解決了簡(jiǎn)單工廠模式的缺點(diǎn), 將每一個(gè)具體產(chǎn)品的創(chuàng)建工作交給對(duì)應(yīng)的具體工廠角色去解決

          舉個(gè)例子:
          ---------------------
          public interface FruitGardener {

          ?public Fruit factory();
          }


          ---------------------

          public class AppleGardener implements FruitGardener{

          ?public Fruit factory() {
          ??return new Apple();
          ?}
          }

          -----------------

          public class GrapeGardener implements FruitGardener{

          ?public Fruit factory() {
          ??return new Grape();
          ?}
          }


          ----------------------

          public class StrawGardener implements FruitGardener{

          ?public Fruit factory() {
          ??return new Strawberry();
          ?}
          }

          -----------------
          public class Client {

          ?private static FruitGardener applegardener, grapegardener, strawgardener;
          ?private static Fruit apple, grape, strawberry;
          ?
          ?public static void main(String[] args){
          ??applegardener = new AppleGardener();
          ??apple = applegardener.factory();
          ??System.out.println("apple is class: " + apple.getClass().getName());
          ??apple.plant();
          ??apple.grow();
          ??apple.harvest();
          ??System.out.println();
          ??
          ??grapegardener = new GrapeGardener();
          ??grape = grapegardener.factory();
          ??System.out.println("grape is class: " + grape.getClass().getName());
          ??grape.plant();
          ??grape.grow();
          ??grape.harvest();
          ??System.out.println();

          ??strawgardener = new StrawGardener();
          ??strawberry = strawgardener.factory();
          ??System.out.println("strawberry is class: " + strawberry.getClass().getName());
          ??strawberry.plant();
          ??strawberry.grow();
          ??strawberry.harvest();
          ?}
          }


          3 抽象工廠模式:解決多產(chǎn)品簇問(wèn)題

          舉個(gè)例子:

          -------------------
          public interface Fruit {

          //?public String getName();
          }

          public class NorthernFruit implements Fruit{

          ?private String name;

          ?public String getName() {
          ??return name;
          ?}

          ?public void setName(String name) {
          ??this.name = name;
          ?}

          ?public NorthernFruit(String name) {
          ??super();
          ??// TODO Auto-generated constructor stub
          ??this.name = name;
          ?}
          ?
          ?
          }



          public class TropicalFruit implements Fruit{

          ?private String name;

          ?public String getName() {
          ??return name;
          ?}

          ?public void setName(String name) {
          ??this.name = name;
          ?}

          ?public TropicalFruit(String name) {
          ??super();
          ??// TODO Auto-generated constructor stub
          ??this.name = name;
          ?}
          ?
          ?
          }



          public interface Veggie {

          }


          public class TropicalVeggie implements Veggie{
          ?
          ?private String name;

          ?public String getName() {
          ??return name;
          ?}

          ?public void setName(String name) {
          ??this.name = name;
          ?}

          ?public TropicalVeggie(String name) {
          ??super();
          ??// TODO Auto-generated constructor stub
          ??this.name = name;
          ?}
          ?
          ?

          }


          public class NorthernVeggie implements Veggie{

          ?private String name;

          ?public String getName() {
          ??return name;
          ?}

          ?public void setName(String name) {
          ??this.name = name;
          ?}

          ?public NorthernVeggie(String name) {
          ??super();
          ??// TODO Auto-generated constructor stub
          ??this.name = name;
          ?}
          ?
          ?
          }

          -------------------------

          public interface Gardener {

          ?public Fruit createFruit(String name);
          ?
          ?public Veggie createVeggie(String name);
          }

          public class NorthernGardener implements Gardener {

          ?public Fruit createFruit(String name) {
          ??return new NorthernFruit(name);
          ?}

          ?public Veggie createVeggie(String name) {
          ??return new NorthernVeggie(name);
          ?}

          ?
          }



          public class TropicalGardener implements Gardener {

          ?public Fruit createFruit(String name) {
          ??return new TropicalFruit(name);
          ?}

          ?public Veggie createVeggie(String name) {
          ??return new TropicalVeggie(name);
          ?}

          ?
          }


          public class Client {

          ?private static Gardener tropicalgardener ,northerngardener;
          ?private static Fruit northernfruit, tropicalfruit;
          ?private static Veggie northernveggie, tropicalveggie;
          ?
          ?public static void main(String[] args){
          ??tropicalgardener = new TropicalGardener();
          ??tropicalfruit = tropicalgardener.createFruit("tropicalfruit");
          ??//System.out.println(tropicalfruit.getName());
          ??tropicalveggie = tropicalgardener.createVeggie("tropicalveggie");
          ??
          ??northerngardener = new NorthernGardener();
          ??northernfruit = northerngardener.createFruit("northernfruit");
          ??northernveggie = northerngardener.createVeggie("northernveggie");
          ?}
          ?
          }


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 龙泉市| 西乌珠穆沁旗| 新宁县| 赤水市| 比如县| 铁岭县| 佳木斯市| 临颍县| 阳东县| 马边| 海晏县| 博湖县| 美姑县| 保靖县| 萝北县| 石林| 连平县| 怀来县| 黑水县| 泰顺县| 旬阳县| 潞城市| 始兴县| 营口市| 揭东县| 安西县| 阿合奇县| 宜阳县| 黄冈市| 砚山县| 中卫市| 泰顺县| 鹤岗市| 土默特右旗| 桂林市| 泉州市| 拜泉县| 宝坻区| 泸定县| 沅江市| 阿巴嘎旗|