本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7594131
建立时间:2006年5月29日




[设计模式]『蚂蚁学模式』之实例化简单工厂、工厂方法模式
软件技术

lhwork 发表于 2007/1/10 9:34:04

我想“一切从实例开始”是我最易接受的方式。网上有太多的理论和经验总结,于已而言,吸收亦成乐事。先看实例,很简单。UML图也懒得画了,更喜欢CRC卡片的方式。一、简单工厂模式1. com.cleversoft.designpatterns.simplefactory.Motor.java 500)this.width=500'>package com.cleversoft.designpatterns.simplefactory;500)this.width=500'>public interface Motor 500)this.width=500'>{    public void run();}2. com.cleversoft.designpatterns.simplefactory.Bus.java 500)this.width=500'>package com.cleversoft.designpatterns.simplefactory;500)this.width=500'>public class Bus implements Motor 500)this.width=500'>{    public Bus() 500)this.width=500'>{    }    public void run() 500)this.width=500'>{        System.out.println("This is a Bus!");    }}3. com.cleversoft.designpatterns.simplefactory.Car.java 500)this.width=500'>package com.cleversoft.designpatterns.simplefactory;500)this.width=500'>public class Car implements Motor500)this.width=500'>{    public Car()500)this.width=500'>{            }    public void run()500)this.width=500'>{        System.out.println("This is a Car!");    }}4. com.cleversoft.designpatterns.simplefactory.MotorFactory.java 500)this.width=500'>package com.cleversoft.designpatterns.simplefactory;500)this.width=500'>public class MotorFactory 500)this.width=500'>{    public static Motor factory(String which) 500)this.width=500'>{        if (which.equalsIgnoreCase("bus")) 500)this.width=500'>{            return new Bus();        } else if (which.equalsIgnoreCase("car")) 500)this.width=500'>{            return new Car();        }        return null;    }}5. com.cleversoft.designpatterns.simplefactory.Main.java 500)this.width=500'>package com.cleversoft.designpatterns.simplefactory;500)this.width=500'>public class Main 500)this.width=500'>{    /** *//**     * @param args     */    public static void main(String[] args) 500)this.width=500'>{        // TODO Auto-generated method stub        MotorFactory mf = new MotorFactory();        Motor m = mf.factory("bus");        m.run();    }}二、工厂方法模式1. com.cleversoft.designpatterns.factorymethod.MotorFactory.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public interface MotorFactory 500)this.width=500'>{    public Motor factory();}2. com.cleversoft.designpatterns.factorymethod.Motor.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public interface Motor 500)this.width=500'>{    public void run();}3. com.cleversoft.designpatterns.factorymethod.BusFactory.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public class BusFactory implements MotorFactory500)this.width=500'>{    public Motor factory()500)this.width=500'>{        return new Bus();    }}4. com.cleversoft.designpatterns.factorymethod.CarFactory.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public class CarFactory implements MotorFactory500)this.width=500'>{    public Motor factory()500)this.width=500'>{        return new Car();    }}5. com.cleversoft.designpatterns.factorymethod.Bus.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public class Bus implements Motor 500)this.width=500'>{    public Bus() 500)this.width=500'>{    }    public void run() 500)this.width=500'>{        System.out.println("This is a Bus!");    }}6. com.cleversoft.designpatterns.factorymethod.Car.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public class Car implements Motor 500)this.width=500'>{    public Car() 500)this.width=500'>{    }    public void run() 500)this.width=500'>{        System.out.println("This is a Car!");    }}7. com.cleversoft.designpatterns.factorymethod.Main.java 500)this.width=500'>package com.cleversoft.designpatterns.factorymethod;500)this.width=500'>public class Main 500)this.width=500'>{    /** *//**     * @param args     */    public static void main(String[] args) 500)this.width=500'>{        // TODO Auto-generated method stub        MotorFactory mf = new BusFactory();        Motor m = mf.factory();        m.run();    }}参考资料:1. http://terrylee.cnblogs.com/archive/2006/01/04/310716.html2. 《Java与模式》


阅读全文(2813) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.328 second(s), page refreshed 144757994 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号