zoukankan      html  css  js  c++  java
  • 设计模式二—工厂方法模式

    设计模式二—工厂方法模式

    一、工厂方法模式优点

    良好的封装性

    优秀的可扩展性

    屏蔽产品类

    典型的解耦架构

    二、实例

    三、实例代码

    1、Fruit.java

    public interface Fruit {

       /*

        * 生长

        * 收获

        * 栽种

        */

       public void grow();

       public void harvest();

       public void plant();

    }

    2、FruitGardener.java

    public interface FruitGardener {

       /*

        * 水果园丁

        * 建立水果工厂的方法

        */

      

       public Fruit factory();

      

      

    }

    3、Apple.java

    public class Apple implements Fruit {

     

       private int treeAge;

      

       public void grow() {

         System.out.println("苹果正在生长,,,");

       }

     

      

       public void harvest() {

         System.out.println("收获苹果");

       }

     

      

       public void plant() {

         System.out.println("栽种苹果");

       }

     

     

       public int getTreeAge() {

         return treeAge;

       }

     

     

       public void setTreeAge(int treeAge) {

         this.treeAge = treeAge;

       }

      

      

     

    }

    4、Grape.java

    public class Grape implements Fruit {

       private boolean seedless;

      

       public void grow() {

         System.out.println("葡萄正在生长。。。");

       }

     

       public void harvest() {

         System.out.println("收获葡萄。");

       }

     

       public void plant() {

         System.out.println("栽种葡萄。");

       }

     

       public boolean isSeedless() {

         return seedless;

       }

     

       public void setSeedless(boolean seedless) {

         this.seedless = seedless;

       }

      

      

     

    }

    5、AppleGardener.java

    public class AppleGardener implements FruitGardener {

     

       public Fruit factory() {

         return new Apple();

       }

     

    }

    6、GrapeGardener.java

    public class GrapeGardener implements FruitGardener {

     

       public Fruit factory() {

         return new Grape();

       }

     

    }

    7、ClientDemo.java

    public class ClientDemo {

     

       public static void main(String[] args) {

         //苹果园丁工厂

         FruitGardener fruitGardener=new AppleGardener();

         //通过工厂生产苹果

         Fruit apple = fruitGardener.factory();//用接口代替了实现实例

         apple.plant();

         apple.grow();

         apple.harvest();

     

         //葡萄园丁工厂

         fruitGardener=new AppleGardener();

         //通过工厂生产葡萄

         Fruit grape = fruitGardener.factory();//用接口代替了实现实例

         grape.plant();

         grape.grow();

         grape.harvest();

     

       }

     

    }

    四、实例结果

    栽种苹果

    苹果正在生长,,,

    收获苹果

    栽种苹果

    苹果正在生长,,,

    收获苹果

  • 相关阅读:
    Java知识系统回顾整理01基础02面向对象03方法
    可插件化拓展改造器 slot-maven-plugin
    数据治理框架
    Java读写hdfs上的avro文件
    FTPClient的使用
    Java读写hdfs上的avro文件
    图片上传预览
    css hack 用法注意
    获取get请求后面的参数
    js全局变量污染
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/6892387.html
Copyright © 2011-2022 走看看