一、工厂模式
来源:互联网 发布:中银淘宝校园卡0 额度 编辑:程序博客网 时间:2024/04/26 21:07
工厂模式:简单来说就像一个创界对象的工厂,需要什么对象,传参数给他,它返回需要的对象的实例。工厂模式属于创建型模式。
下面通过代码来展示普通的工厂模式:
1.创建一个抽象的Human类public abstract class Human {public abstract void eat();public abstract void sleep();public abstract void study();}2.Man继承抽象Human类public class Man extends Human{public void eat() {System.out.println("the man eat..");}public void sleep() {System.out.println("the man sleep..");}public void study() {System.out.println("the man study..");}}3.Female 继承抽象Human类public class Female extends Human{public void eat() {System.out.println("the Female eat...");}public void sleep() {System.out.println("the Female sleep..");}public void study() {System.out.println("the Female study..");}}下面就是我们的 HumanFactory工厂类public class HumanFactory { public static Human creatHuman(String obj){Human human=null;if("man".equals(obj)){human=new Man(); }else if("female".equals(obj)){ human=new Female(); }else{ return null; }return human;}}
抽象工厂模式:
抽象工厂模式又称之为工厂的工厂。也就是生产某类具的对象的工厂的工厂。比如有生产shape对象的工厂:shapeFactory,生产color对象的工厂:colorFactory;现在有一个工厂的工厂叫productFactory;传入要生对象的工厂给它,它返回要生成的对象。
package FactoryModel;interface shape{public void drawShape();}interface color{public void fillColor();}class squar implements shape{@Overridepublic void drawShape() {System.out.print("draw squar");}}class circle implements shape{@Overridepublic void drawShape() {System.out.print("draw circle");}}class red implements color{@Overridepublic void fillColor() {System.out.print("fill red");}}class green implements color{@Overridepublic void fillColor() {System.out.print("fill green");}}interface AbstractFactory { //抽象工厂public shape getShape(String name);public color getColor(String name);}class shapeFactory implements AbstractFactory{//形状工厂public shape getShape(String name){if(name=="circle")return new circle();if(name=="squar")return new squar();return null;}@Overridepublic color getColor(String name) {// TODO Auto-generated method stubreturn null;}}class colorFactory implements AbstractFactory{//颜色工厂,你要什么颜色?红色?绿色?public color getColor(String name){if(name=="red")return new red();if(name=="green")return new green();return null;}@Overridepublic shape getShape(String name) {// TODO Auto-generated method stubreturn null;}}class productFactory{//工厂的工厂public AbstractFactory getFactory(String name){//你要创建什么?形状还是颜色?if(name=="shape")return new shapeFactory();if(name=="color")return new colorFactory();//...return null;}}
0 0
- 一、简单工厂模式
- 一 简单工厂模式
- 工厂模式(一)
- 一、简单工厂模式
- (一)简单工厂模式
- 工厂模式(一)
- (一)简单工厂模式
- 一 简单工厂模式
- 一、简单工厂模式
- 一、工厂模式
- 一、简单工厂模式
- 一、简单工厂模式
- 简单工厂模式(一)
- 工厂模式(一)--简单工厂模式
- 工厂设计模式一 简单工厂模式
- 设计模式一:工厂模式
- 设计模式(一)----工厂模式
- 工厂模式(简单工厂模式, 工厂方法模式, 抽象工厂模式)一
- MUI - 新一代全面支持手机的UI框架
- 封装系统时删除pagefile.sys及hiberfil.sys文件
- 运维自动化之ansible saltstack使用对比分析
- 进程1的创建与轮转
- MyEclipse激活
- 一、工厂模式
- Open CV学习记录(十一)
- Android自定义View基础之onMeasure详解
- 第九周项目3—稀疏矩阵的三元组表示的实现及应用(1)
- MyBatis之主键自增——selectKey
- wireshark下载安装
- 原生JS发起http请求的一个例子,并且接收返回值
- Java 垃圾回收机制
- CountDownLatch 控制多个线程执行顺序