为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

Java工厂模式

2017-09-21 7页 doc 19KB 13阅读

用户头像

is_650122

暂无简介

举报
Java工厂模式Java工厂模式 你是问简单工厂模式吗, 使用静态方法创建对象 class 类名{ public static 类名 xxx(){ return new 类名();} public static void main(String args[]){ 类名 对象名=类名.xxx(); } } 简单工厂模式(Simple Factory Pattern),又称静态工厂模式(Static Factory Pattern)。举两个例子以快速明白Java中的简单工厂模式: 有一个农场公司,专门向市场销售各类水果,在这个...
Java工厂模式
Java工厂模式 你是问简单工厂模式吗, 使用静态方法创建对象 class 类名{ public static 类名 xxx(){ return new 类名();} public static void main(String args[]){ 类名 对象名=类名.xxx(); } } 简单工厂模式(Simple Factory Pattern),又称静态工厂模式(Static Factory Pattern)。举两个例子以快速明白Java中的简单工厂模式: 有一个农场公司,专门向市场销售各类水果,在这个系统里需要描述下列水果: 葡萄 Grape 草莓 Strawberry 苹果 Apple 水果与其他植物不同,最终可以采摘食用,那么一个自然的做法是建立一个各种水果都适用的接口,以便与其他农场里的植物区分开来, 此时,则是为水果类声明了一个接口,现在代码上: public interface Fruit { //种植 public void plant(); //生长 public void grow(); //收获 public void harvest(); } 水果接口规定出所有的水果必须实现的接口,包括任何水果类必须具备的方法plant(),grow(),和harvest(); Apple类是水果类的一种,因此它实现了水果接口所声明的所有方法。另处,由于苹果是多年生植物,因此多出一个treeAge性质,描述苹果的树龄。代码如下所示: /** * * 类名称:Apple * 类描述:农场苹果类 * */ 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; } } 同理,葡萄 Grape: 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; } } 草莓 Stuawberry: public class Strawberry implements Fruit { public void grow() { System.out.println("草莓开始生长。。。"); } public void harvest() { System.out.println("草莓开始收获。。。"); } public void plant() { System.out.println("草莓种入农场。。。"); } } 农场园丁也是系统的一部分,由一个类来代表,FruitGardener类,代码如下: public class FruitGardener { public static Fruit factory(String which) throws Exception { if(which.equalsIgnoreCase("apple")) { return new Apple(); } else if(which.equalsIgnoreCase("grape")) { return new Grape(); } else if(which.equalsIgnoreCase("strawberry")) { return new Strawberry(); } else { throw new Exception("抱歉,本农场不生产此种水果"); } } } 这下,农场一片繁荣景象了。来年一定是个丰收年。 这时有客户来果园查看情况,和园丁说,给我们介绍下你的水果吧。于是园丁: 类描述:来农场订购水果,看水果情况的客户 public class Client { public static void main(String[] args) throws Exception { FruitGardener fruitGardener = new FruitGardener(); Fruit apple = fruitGardener.factory("apple"); apple.grow(); Fruit strawBerry = fruitGardener.factory("strawberry"); strawBerry.harvest(); Fruit ddd = fruitGardener.factory("ddd"); ddd.plant(); } }
/
本文档为【Java工厂模式】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索