设计模式三—抽象工厂模式
发布日期:2021-06-24 18:10:57 浏览次数:3 分类:技术文章

本文共 3490 字,大约阅读时间需要 11 分钟。

设计模式三—抽象工厂模式

一、定义

抽象工厂模式是工厂方法模式的进一步抽象。如果产品簇中只有一种产品,则退化为工厂方法模式。

 

二、原理图

 

 

三、代码实例

* 苹果和土豆是园丁1的杰作

* 葡萄和西红柿是园丁2的杰作

1、Fruit.java

public interface Fruit {	/*	 * 生长	 * 收获	 * 栽种	 */	public void grow();	public void harvest();	public void plant();}

  

2、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;    }        }

 

3、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;    }        }

 

4、Vegetable.java

public interface Vegetable {    /*     * 生长     * 收获     * 栽种     */    public void grow();    public void harvest();    public void plant();}

 

5、Potato.java

public class Potato implements Vegetable {    public void grow() {        System.out.println("土豆正在生长,,,");    }        public void harvest() {        System.out.println("收获土豆");    }        public void plant() {        System.out.println("栽种土豆");    }}

 

6、Tomato.java

public class Tomato implements Vegetable {    public void grow() {        System.out.println("西红柿正在生长,,,");    }        public void harvest() {        System.out.println("收获西红柿");    }        public void plant() {        System.out.println("栽种西红柿");    }}

 

7、Gardener.java

public interface Gardener {    /*     * 水果园丁     * 蔬菜园丁     * 水果蔬菜各两种     * 建立水果工厂的方法     */        public Fruit factoryFruit();    public Vegetable factoryVegetable();        }

 

8、ConcreteGardener1.java

public class ConcreteGardener11 implements Gardener {    //等级为1的园丁生产的水果和蔬菜    /*     * 苹果和土豆是园丁1的杰作,或者说是一等产品     * 葡萄和西红柿是园丁2的杰作,或者说是二等产品     */    public Fruit factoryFruit() {        return new Apple();    }    public Vegetable factoryVegetable() {        return new Potato();    }}

 

9、ConcreteGardener2.java

public class ConcreteGardener12 implements Gardener {    //等级为2的园丁生产的水果和蔬菜    /*     * 苹果和土豆是园丁1的杰作,或者说是一等产品     * 葡萄和西红柿是园丁2的杰作,或者说是二等产品     */    public Fruit factoryFruit() {        return new Grape();    }    public Vegetable factoryVegetable() {        return new Tomato();    }}

 

10、ClientDemo

public class ClientDemo {    public static void main(String[] args) {            /*         * 苹果和土豆是园丁1的杰作,或者说是一等产品         * 葡萄和西红柿是园丁2的杰作,或者说是二等产品         */        Gardener gardener1=new ConcreteGardener11();        Gardener gardener2=new ConcreteGardener12();        Fruit apple = gardener1.factoryFruit();        Vegetable potato=gardener1.factoryVegetable();        Fruit grape = gardener2.factoryFruit();        Vegetable tomato=gardener2.factoryVegetable();                apple.plant();        apple.grow();        apple.harvest();        potato.plant();        potato.grow();        potato.harvest();        grape.plant();        grape.grow();        grape.harvest();        tomato.plant();        tomato.grow();        tomato.harvest();            }}

 

 

四、运行结果

栽种苹果

苹果正在生长,,,

收获苹果

栽种土豆

土豆正在生长,,,

收获土豆

栽种葡萄。

葡萄正在生长。。。

收获葡萄。

栽种西红柿

西红柿正在生长,,,

收获西红柿

 

转载地址:https://blog.csdn.net/weixin_34248258/article/details/85893987 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java 线程 错失的信号、notify() 与notifyAll的使用
下一篇:cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第六步--金币&岩石加入而且管理

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月03日 10时06分44秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Scratch螺旋多边形小游戏 蓝桥杯Scratch国赛真题答案和解析 2019-04-28
初学python100例-案例31 输入星期几的第一个字母判断是星期几 少儿编程案例讲解 2019-04-28
初学python100例-案例32 男孩女孩小孩人数 少儿编程案例讲解 2019-04-28
【蓝桥杯真题12】Scratch角色装扮 少儿编程scratch蓝桥杯选拔赛真题讲解 2019-04-28
毕业设计 C#开发实现影院信息管理商城系统 毕业论文【论文+源程序+数据库】 2019-04-28
电子学会图形化scratch编程等级考试二级真题答案解析(判断)2020-9 2019-04-28
scratch别碰红块小游戏 电子学会图形化编程scratch等级考试二级真题和答案解析2020-9 2019-04-28
scratch打棒球游戏 电子学会图形化编程scratch等级考试四级真题和答案解析2019 2019-04-28
scratch列表移位 电子学会图形化编程scratch等级考试四级真题和答案解析2019 2019-04-28
scratch动物拥抱游戏 电子学会图形化编程scratch等级考试一级真题答案2020-9 2019-04-28
spring aop之对象内部方法间的嵌套失效 2019-04-28
SecureCRT工具登录跳板机,直接连接目标机器 2019-04-28
Google首席工程师Joshua Bloch谈如何设计优秀的API 2019-04-28
java SPI 与cooma(dubbo 微容器改良品)--1 2019-04-28
Java后端--3--Springboot基础开发规范 2019-04-28
安全合规--45--基于国内法律法规的企业数据合规体系建设经验总结(四) 2019-04-28
安全合规--46--基于国内法律法规的企业数据合规体系建设经验总结(五) 2019-04-28
安全合规--47--基于国内法律法规的企业数据合规体系建设经验总结(六) 2019-04-28
安全合规--48--基于国内法律法规的企业数据合规体系建设经验总结(七) 2019-04-28
安全合规--49--基于国内法律法规的企业数据合规体系建设经验总结(八) 2019-04-28