




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
简单工厂模式农场(FruitGardener)生产的水果(Fruit)有三种:葡萄(Grape)、苹果(Apple)和草莓(Strawberry)public interface Fruit public void plan();public void grow();public void harvest();public class Apple implements Fruit public void grow() System.out.println(苹果在生长.);public class Grape implements Fruit public void grow() System.out.println(葡萄在生长.);public class Strawberry implements Fruit public void grow() System.out.println(草莓在生长.);public class FruitGardener public static Fruit createFruit(String name)if(apple.equals(name)return new Apple();if(grape.equals(name)return new Grape();if(strawberry.equals(name)return new Strawberry();return new Apple();public class Test public static void main(String args) FruitGardener.createFruit(apple).plan();FruitGardener.createFruit(grape).grow();FruitGardener.createFruit(strawberry).grow();随着业务的扩大,农场生产的果品种类越来越多,按照原有的模式,由一个农场生产所有种类的水果,导致任务过重、判断过多,逻辑过于复杂,容易出错,难于管理,牵一发而动全身,增加新品种困难,需要改进设计模式,适应于新的情况。工厂方法模式将农场分为若干种:苹果农场负责生产苹果、草莓农场负责生产草莓、葡萄农场负责生产葡萄public interface Fruit /抽象的产品public void grow();public interface FruitFactoy /抽象的工厂public Fruit createFruit();public class Apple implements Fruit /具体的产品public void grow() System.out.println(苹果在生长.);public class Grape implements Fruit /具体的产品public void grow() System.out.println(葡萄在生长.);public class Strawberry implements Fruit /具体的产品public void grow() System.out.println(草莓在生长.);public class AppleFactory implements FruitFactoy /具体的工厂public Fruit createFruit() return new Apple();public class GrapeFactory implements FruitFactoy /具体的工厂public Fruit createFruit() return new Grape();public class StrawberryFactory implements FruitFactoy /具体的工厂public Fruit createFruit() return new Strawberry();public class Test public static void main(String args) new AppleFactory().createFruit().grow();new GrapeFactory().createFruit().grow();new StrawberryFactory().createFruit().grow();随着市场和企业定位变化,决定只生产苹果、葡萄和草莓等少数果品,但每一果品呈系列化,如:普通水果、绿色水果等等,原设计模式不能很好地适应这种需要,需要改进设计模式。抽象工厂模式农场分为两种:普通农场生产各种普通水果,绿色农场生产各种绿色水果。public interface Fruit /抽象的水果产品public void grow();public interface FruitFactoy /抽象的工厂类public Fruit createApple();/生产苹果public Fruit createGrape();/生产葡萄public Fruit createStrawberry();/生产草莓public class Apple implements Fruit /具体的产品public void grow() System.out.println(苹果在生长.);public class Grape implements Fruit /具体的产品public void grow() System.out.println(葡萄在生长.);public class Strawberry implements Fruit /具体的产品public void grow() System.out.println(草莓在生长.);public class GreenApple implements Fruit /具体的产品public void grow() System.out.println(绿色苹果在生长.);public class GreenGrape implements Fruit /具体的产品public void grow() System.out.println(绿色葡萄在生长.);public class GreenStrawberry implements Fruit /具体的产品public void grow() System.out.println(绿色草莓在生长.);public class GeneralFruitFactory implements FruitFactory/具体的工厂-普通农场public class Test public static void main(String args) FruitFactory factory=null;factory=new GreenFruitFactory();factory.createApple().grow();factory.createGrape().grow();factory.createStrawberry().grow();factory=new GeneralFruitFactory();factory.createApple().grow();factory.createGrape().grow();factory.createStrawberry().grow();public Fruit createApple() return new Apple();public Fruit createGrape() return new Grape();public Fruit createStrawberry() return new Strawberry();public class GreenFruitFactory implements FruitFactory/具体的工厂-绿色农场public Fruit createApple() return new GreenApple();public Fruit createGrape() return new GreenGrape();public Fruit createStrawberry() return new GreenStrawberry();public class Test public static void main(String args) FruitFactory factory=null;factory=new GreenFruitFactory();factory.createApple().grow();factory.createGrape().grow();factory.createStrawberry().grow();factory=new GeneralFruitFactory();factory.createApple().grow();factory.createGrape().grow();factory.createStrawberry().grow();单例模式饿汉式public class Apple implements Fruit private static Apple instance=new Apple();private Apple()public static Apple getInstance()return instance;public void grow() System.out.println(苹果在生长.);懒汉式public class Grape implements Fruit private static Grape instance;private Grape()public synchronized static Grape getInstance()if(instance=null)instance=new Grape();return instance;public void grow() System.out.println(葡萄在生长.);优化后的懒汉式public class Strawberry implements Fruit private static class StrawberrySingletonprivate static Strawberry instance=new Strawberry();public static Strawberry getInstance()return StrawberrySingleton.instance;private Strawberry()public void grow() System.out.println(草莓在生长.);登记式public class Appleprivate static final Map map=new HashMap();public static Apple getInstance(String appleClassName) throws Exceptionif(appleClassName=null)appleClassName=Apple.class.getName();Apple instance=map.get(appleClassName);if(instance=null)instance=(Apple) Class.forName(appleClassName).newInstance();map.put(appleClassName, instance);return instance;public static Apple getInstance()try return Apple.getInstance(null); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();return null;protected Apple()public void grow() System.out.println(苹果在生长.)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 七册教案全册
- 重庆考级速写8级课件
- 含碘对比剂静脉外渗的临床防治与护理规范指南
- 新解读《GB-T 18867-2014电子工业用气体 六氟化硫》
- 建筑施工-安全培训课件-安全生产双重预防机制解读与班组安全标准化建设
- 人教版高中生物选择性必修1《稳态与调节》必背知识考点提纲填空练习版(含答案)
- 热点作家:陈忠实(原卷版)-2026年中考语文复习之现代文阅读热点作家作品
- 数据分析-列联表与独立性检验专练-全国高考数学一轮复习(提高版)
- 老年人保养课件
- 人教版八年级英语下册阅读理解专练(含答案)
- GJB135B-2021合成航空发动机润滑油规范
- 2025年医疗质量管理质控培训考核试题(含答案)
- 深圳2025年重大项目计划申报
- 【轻质减负】第2课《中国人首次进入自己的空间站》情境化教学课件
- 学生不住校申请书
- 2025年边境巡逻机群无人机协同战术应用
- 老年病科试题及答案
- 中药饮片养护技术
- 八上必背文言文解析(全册)
- 特种设备作业人员Q1起重机指挥模拟考试题及答案2025
- 小学科学新教科版二年级上册第一单元 造房子教案(共6课)(2025秋)
评论
0/150
提交评论