12
返回列表 发新帖
楼主: 臧圩人

[笔记] 《Head First设计模式》阅读笔记.第四章

[复制链接]
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
11#
 楼主| 发表于 2010-1-14 17:01 | 只看该作者

汽车零件制造厂

// 汽车零件制造工厂
public interface PartsFactory {
        public Engine createEngine();

        public Wheel createWheel();

        public Lamp createLamp();
}

public class BeijingPartsFactory implements PartsFactory {
        public Engine createEngine() {
                return new BeijingEngine();
        }

        public Lamp createLamp() {
                return new BeijingLamp();
        }

        public Wheel createWheel() {
                return new BeijingWheel();
        }
}

public class ShanghaiPartsFactory implements PartsFactory {
        public Engine createEngine() {
                return new ShanghaiEngine();
        }

        public Lamp createLamp() {
                return new ShanghaiLamp();
        }

        public Wheel createWheel() {
                return new ShanghaiWheel();
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
12#
 楼主| 发表于 2010-1-14 17:02 | 只看该作者

汽车类

public abstract class Car {
        protected String name;// 名称

        protected Engine[] engines;// 发动机

        protected Wheel[] wheels;// 车轮

        protected Lamp[] lamps;// 车灯

        public void prepare() {
                System.out.println("Preparing " + name);
                System.out.println("Get engines ready...");
                System.out.println("Get wheels ready...");
                System.out.println("Get lamps ready...");
        }

        // 组装
        public void fabricate() {
                System.out.println("Adding engines.");
                System.out.println("Adding wheels.");
                System.out.println("Adding lamps.");
        }

        // 检测
        public void detect() {
                System.out.println("Detect the car.");
        }

        public String getName() {
                return name;
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
13#
 楼主| 发表于 2010-1-14 17:02 | 只看该作者

汽车实现类

public class BmwX3Car extends Car {
        private PartsFactory factory;

        public BmwX3Car(PartsFactory factory) {
                this.factory = factory;

                name = "BMW X3";
                // 一个发动机
                engines = new Engine[] { this.factory.createEngine() };
                // 四个车轮
                wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),
                                this.factory.createWheel() };
                // 两个车灯
                lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp() };
        }
}

public class BmwX5Car extends Car {
        private PartsFactory factory;

        public BmwX5Car(PartsFactory factory) {
                this.factory = factory;

                name = "BMW X5";
                // 两个发动机
                engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine() };
                // 四个车轮
                wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),
                                this.factory.createWheel() };
                // 四个车灯
                lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),
                                this.factory.createLamp() };
        }
}

public class BmwX7Car extends Car {
        private PartsFactory factory;

        public BmwX7Car(PartsFactory factory) {
                this.factory = factory;

                name = "BMW X7";
                // 四个发动机
                engines = new Engine[] { this.factory.createEngine(), this.factory.createEngine(), this.factory.createEngine(),
                                this.factory.createEngine() };
                // 四个车轮和一个备用胎
                wheels = new Wheel[] { this.factory.createWheel(), this.factory.createWheel(), this.factory.createWheel(),
                                this.factory.createWheel(), this.factory.createWheel() };
                // 六个车灯
                lamps = new Lamp[] { this.factory.createLamp(), this.factory.createLamp(), this.factory.createLamp(),
                                this.factory.createLamp() };
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
14#
 楼主| 发表于 2010-1-14 17:02 | 只看该作者

销售店

public abstract class CarSalesShop {
        public Car orderCar(String type) {
                Car car = createCar(type);
                car.prepare();
                car.fabricate();
                car.detect();

                System.out.println("A " + car.getName() + " car is ready.");
                return car;
        }

        // 工厂方法接口
        public abstract Car createCar(String type);
}

public class BeijingCarSalesShop extends CarSalesShop {
        @Override
        public Car createCar(String type) {
                PartsFactory factory = new BeijingPartsFactory();
                if ("bmwx3".equals(type)) {
                        return new BmwX3Car(factory);
                } else if ("bmwx5".equals(type)) {
                        return new BmwX5Car(factory);
                } else if ("bmwx7".equals(type)) {
                        return new BmwX7Car(factory);
                } else
                        return null;
        }
}

public class ShanghaiCarSalesShop extends CarSalesShop {
        @Override
        public Car createCar(String type) {
                PartsFactory factory = new ShanghaiPartsFactory();
                if ("bmwx3".equals(type)) {
                        return new BmwX3Car(factory);
                } else if ("bmwx5".equals(type)) {
                        return new BmwX5Car(factory);
                } else if ("bmwx7".equals(type)) {
                        return new BmwX7Car(factory);
                } else
                        return null;
        }
}

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
15#
 楼主| 发表于 2010-1-14 17:03 | 只看该作者
8.结合策略模式优化抽象工厂实例

修改CarSalesShop类如下:
public class CarSalesShop {
        private PartsFactory factory;

        public CarSalesShop(PartsFactory factory) {
                this.factory = factory;
        }

        public Car orderCar(String type) {
                Car car = createCar(type);
                car.prepare();
                car.fabricate();
                car.detect();

                System.out.println("A " + car.getName() + " car is ready.");
                return car;
        }

        // 原有的工厂方法接口,现在实现了它
        public Car createCar(String type) {
                if ("bmwx3".equals(type)) {
                        return new BmwX3Car(factory);
                } else if ("bmwx5".equals(type)) {
                        return new BmwX5Car(factory);
                } else if ("bmwx7".equals(type)) {
                        return new BmwX7Car(factory);
                } else
                        return null;
        }
}

这样修改后,就不再需要BeijingCarSalesShop和ShanghaiCarSalesShop这两个类。使用方法如下:
public class Test {
        public static void main(String[] args) {
                PartsFactory factory = new BeijingPartsFactory();
                CarSalesShop shop = new CarSalesShop(factory);
                shop.orderCar("bmwx7");
        }
}

这样修改后,由于引入了策略模式,消除了两个两个子类,并且可以通过增加setPartsFactory()方法达到运行时改变零件生产工厂的目的。

--END--
:rose:

使用道具 举报

回复
论坛徽章:
56
2010年世界杯参赛球队:瑞士
日期:2010-02-26 11:04:012012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23版主2段
日期:2012-05-31 02:10:00版主2段
日期:2012-05-31 02:10:00ITPUB 11周年纪念徽章
日期:2012-10-09 18:05:07现任管理团队成员
日期:2012-10-18 18:22:36
16#
 楼主| 发表于 2010-1-14 17:05 | 只看该作者
这一章很长,看了大半天,写实例又是大半天。

不过工厂方法的确很实用

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
17#
发表于 2010-1-14 23:39 | 只看该作者
nice job

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表