已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026国网福建省高校毕业生提前批招聘(约450人)笔试模拟试题浓缩500题含答案详解(a卷)
- 2026秋季国家管网集团华南公司(广东省管网公司)高校毕业生招聘笔试备考试题(浓缩500题)及答案详解【新】
- 2026年大庆市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)带答案详解
- 国家管网集团湖南公司2026届秋季高校毕业生招聘考试备考试题(浓缩500题)带答案详解(基础题)
- 2025国网内蒙古电力公司高校毕业生提前批招聘笔试模拟试题浓缩500题及答案详解(全优)
- 2025国网广西电力校园招聘(提前批)笔试模拟试题浓缩500题附答案详解(基础题)
- 2026秋季国家管网集团建设项目管理公司高校毕业生招聘笔试参考题库(浓缩500题)含答案详解(b卷)
- 2026国网上海市高校毕业生提前批招聘(约450人)笔试模拟试题浓缩500题附答案详解(精练)
- 2026秋季国家管网集团湖南公司高校毕业生招聘4人考试备考试题(浓缩500题)附参考答案详解(精练)
- 国家管网集团湖南公司2026届秋季高校毕业生招聘笔试参考题库(浓缩500题)带答案详解(夺分金卷)
- 热力公司客服培训
- 2026届新高考语文冲刺复习2025年高考全国2卷作文讲解
- 2024-2025学年广东省广州市增城区七年级(上)期中语文试卷
- 抗日英雄王二小
- 2025湖南岳阳市盛佳荣新投资集团招聘10人考试参考题库及答案解析
- 2025年河南省高考生物真题(含答案解析)
- 单晶叶片定向凝固技术-第1篇-洞察与解读
- 海南省海口市美兰区2024-2025学年七年级上学期第一次月考语文考题及答案
- 酒驾复议申请书
- 2025年中国低氘水行业市场全景分析及前景机遇研判报告
- 招江西省交通投资集团有限责任公司招聘笔试真题2024
评论
0/150
提交评论