简单工厂设计模式的小case.

工厂设计模式是23中GOF设计模式中最常用的的一个设计模式。spring架构中也有很多接口的设计用到此种设计模式:今天就总结一下这个设计模式并手写一个小案例:

图形画的那叫一个丑。帮助理解。意思意思。

FactoryProvier:提供一个静态方法,接受一个EnumFactoryType的参数,返回具体的工厂实例。代码如下:

package top.lrshuai.helloword.controller.factory;

import top.lrshuai.helloword.controller.common.EnumFactoryType;

/**
 * : 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:20
 */
public class FactoryProvider {

    public static GraphicsFactory getFactory(EnumFactoryType factoryType){
        if(factoryType==EnumFactoryType.TWOD_FACTORY){
            return new TwoDGraphicsFactroy();
        }else if(factoryType==EnumFactoryType.THREED_FACTORY){
            return new ThreeDGraphicsFactory();
        }
        return null;
    }

}

GraphicsFactory :工厂接口,提供一个抽象的方法,接受一个EnumGraphicsType类型参数。返回一个Graphics对象,需要具体的实现,代码如下:

package top.lrshuai.helloword.controller.factory;

import top.lrshuai.helloword.controller.common.EnumGraphicsType;
import top.lrshuai.helloword.controller.entity.Graphics;

/**
 * : 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:14
 */
public interface GraphicsFactory {

    public Graphics getGraphics(EnumGraphicsType graphicsType);

}

TwoDGraphicsFactory    ThreeDGraphicsFactory分别实现GraphicsFactory接口,并实现具体的逻辑代码:

package top.lrshuai.helloword.controller.factory;

import top.lrshuai.helloword.controller.common.EnumGraphicsType;
import top.lrshuai.helloword.controller.entity.Circle;
import top.lrshuai.helloword.controller.entity.Graphics;
import top.lrshuai.helloword.controller.entity.Line;

/**
 * : 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:18
 */
public class TwoDGraphicsFactroy implements GraphicsFactory {

    @Override
    public Graphics getGraphics(EnumGraphicsType graphicsType) {
        if(graphicsType==EnumGraphicsType.LINE){
            return new Line();
        }else if(graphicsType== EnumGraphicsType.CIRCLE){
            return new Circle();
        }
        return null;
    }
}

package top.lrshuai.helloword.controller.factory;

import top.lrshuai.helloword.controller.common.EnumGraphicsType;
import top.lrshuai.helloword.controller.entity.Circle;
import top.lrshuai.helloword.controller.entity.Graphics;
import top.lrshuai.helloword.controller.entity.Line;
import top.lrshuai.helloword.controller.entity.PersonPhoto;

/**
 * GraphicsFactory: 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:18
 */
public class ThreeDGraphicsFactory implements GraphicsFactory {
    @Override
    public Graphics getGraphics(EnumGraphicsType graphicsType) {
        if(graphicsType==EnumGraphicsType.PERSONPHOTO){
            return new PersonPhoto();
        }
        return null;
    }
}

Graphics:抽象类图形,提供一个抽象的方法draw()方法。

package top.lrshuai.helloword.controller.entity;

/**
 * 图形: 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:10
 */
public abstract class Graphics {

    public abstract void draw();
}

Line  Circle  PersonPhoto子类继承Graphics,并重写父类的方法实现具体的逻辑代码:

package top.lrshuai.helloword.controller.entity;

/**
 * 圆形图: 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:07
 */
public class Circle extends Graphics{

    @Override
    public void draw(){
        System.out.println("draw circle....");
    }
}


package top.lrshuai.helloword.controller.entity;

/**
 * 线性图: 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:06
 */
public class Line extends Graphics {

    @Override
    public void draw(){
        System.out.println("down line......");
    }

}


package top.lrshuai.helloword.controller.entity;

/**
 * 人形图: 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:08
 */
public class PersonPhoto extends Graphics {

    @Override
    public void draw(){
        System.out.println("draw PersonPhoto....");
    }
}

下面是测试类:

package top.lrshuai.helloword.controller.test;

import top.lrshuai.helloword.controller.common.EnumFactoryType;
import top.lrshuai.helloword.controller.common.EnumGraphicsType;
import top.lrshuai.helloword.controller.entity.Graphics;
import top.lrshuai.helloword.controller.factory.FactoryProvider;
import top.lrshuai.helloword.controller.factory.GraphicsFactory;

/**
 * : 描述信息
 *
 * @author liyy
 * @date 2018-11-19 10:32
 */
public class Test {

    public static void main(String[] args){
        GraphicsFactory factory = FactoryProvider.getFactory(EnumFactoryType.TWOD_FACTORY);
        if(factory!=null){
            Graphics graphics = factory.getGraphics(EnumGraphicsType.LINE);
            if(graphics!=null){
                graphics.draw();
            }else{
                System.out.println("没有获取到对应的画图对象");
            }
        }else{
            System.out.println("没有获取到对应的工厂对象");
        }
    }

}

共涉及到一个接口。一个抽象类  接口的两个实现。抽象类的三个继承。一个静态工厂提供入口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值