CU上一个使用linux和FreeBSD的用户BLOG
2008-03-18 23:15
471 查看
1. 简单工厂模式
工厂类处于决定实例化那个产品类的中心位置
使用简单工厂初始化一个类
2. 工厂方法模式
植物接口及两个实现类
水果接口及两个实现类
抽象工厂
工厂类A
工厂类B
工厂方法模式与简单工厂模式再结构上的不同不是很明显。工厂方法类的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。
工厂方法模式之所以有一个别名叫多态性工厂模式是因为具体工厂类都有共同的接口,或者有共同的抽象父类。
当系统扩展需要添加新的产品对象时,仅仅需要添加一个具体对象以及一个具体工厂对象,原有工厂对象不需要进行任何修改,也不需要修改客户端,很好的符合了"开放-封闭"原则。而简单工厂模式在添加新产品对象后不得不修改工厂方法,扩展性不好。
工厂方法模式退化后可以演变成简单工厂模式。
新增加一个类PlantC 和 FruitC
这时只需要增加一个工厂类C
publci class Sample{ .... } public class A extends Sample{ .... } public class B extends Sample{ ...... }
工厂类处于决定实例化那个产品类的中心位置
public class Factory{ //静态工厂方法,无必要再去实例化这个工厂类,增加没有必要的代码 public static Sample create(int which){ if(which==1) return new A(); if(which==2) return new B(); } }
使用简单工厂初始化一个类
Sample newSample=Factory.create(1);
2. 工厂方法模式
植物接口及两个实现类
public interface Plant{} public class PlantA implements Plant{ } public class PlantB implements Plant{ }
水果接口及两个实现类
public interface Fruit{} public class FruitA implements Fruit{ } public class FruitB implements Fruit{ }
抽象工厂
public interface AbstractFactory{ public Plant createPlant(); public Fruit creatFruit(); }
工厂类A
public Class FactoryA implements AbstractFactory{ public Plant createPlant(){ return new PlantA(); } public Fruit creatFruit(){ return new FruitA(); } }
工厂类B
public Class FactoryB implements AbstractFactory{ public Plant createPlant(){ return new PlantB(); } public Fruit creatFruit(){ return new FruitB(); } }
工厂方法模式与简单工厂模式再结构上的不同不是很明显。工厂方法类的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。
工厂方法模式之所以有一个别名叫多态性工厂模式是因为具体工厂类都有共同的接口,或者有共同的抽象父类。
当系统扩展需要添加新的产品对象时,仅仅需要添加一个具体对象以及一个具体工厂对象,原有工厂对象不需要进行任何修改,也不需要修改客户端,很好的符合了"开放-封闭"原则。而简单工厂模式在添加新产品对象后不得不修改工厂方法,扩展性不好。
工厂方法模式退化后可以演变成简单工厂模式。
新增加一个类PlantC 和 FruitC
public class PlantC implements Plant{ } public class FruitC implements Fruit{ }
这时只需要增加一个工厂类C
public Class FactoryC implements AbstractFactory{ public Plant createPlant(){ return new PlantC(); } public Fruit creatFruit(){ return new FruitC(); } }
相关文章推荐
- 一个八年的 Linux 老用户使用 Windows 10 的体验
- linux使用mysql给一个用户赋予一个权限
- linux下创建一个新用户,并能使用sudo命令
- (转载)一个简短的epoll服务器示例, 监听5000个端口, 使用线程池[safedead.cublog.cn]
- 一个简短的epoll服务器示例, 监听5000个端口, 使用线程池[safedead.cublog.cn]
- Linux 文件系统剖析: 使用 /proc 文件系统来访问 Linux 内核的内容 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口
- Linux 文件系统剖析: 使用 /proc 文件系统来访问 Linux 内核的内容 这个虚拟文件系统在内核空间和用户空间之间打开了一个通信窗口
- 怎样不使用Useradd在linux下创建一个新的用户
- FreeBSD添加一个新用户并允许其使用ssh通过公私钥的方式登录
- linux 新建一个管理员用户admin,需要使用的参数()
- linux下如何添加一个用户并且让用户获得root权限
- [推荐] 得到一个给定用户使用了的权限脚本
- 使用事务与锁,实现一个用户取过的数据不被其他用户取到
- ASP.NET MVC5+MySql使用ASP.NET 身份验证实现用户和角色功能 1 概述 目标:使用MySql数据库,建立一个使用ASP.NET 身份验证的应用,并实现角色功能,身份
- Linux硬件资源管理与外设设备使用、系统运行机制及用户管理
- (原創) 如何让一个thread在背景不断的执行? (使用semaphore) (OS) (Linux) (C/C++) (C)
- linux 内核与用户空间通信之netlink使用方法
- 使用sqlite3的接口函数完成一个用户登录验证功能模块设计 要封装成独立函数,在独立的main中调用测试;
- linux 内核和用户空间通信之netlink使用方法
- 【Linux命令】新建一个用户useradd