码迷,mamicode.com
首页 > 其他好文 > 详细

秒杀Servlce接口设计

时间:2018-02-19 22:25:02      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:使用   ring   owa   oid   super   结束时间   继承   led   exec   

秒杀Servlce接口设计

1、创建service包,创建SecKillServlce业务接口

SecKillServlce.Java

package org.secKill.service;
/*
* 业务接口:站在“开发者”的角度设计接口
* 三个方面:方法定义粒度,参数,返回类型(return 类型/异常)*/

import org.secKill.dto.Exposer;
import org.secKill.dto.SecKillExecution;
import org.secKill.entity.SecKill;
import org.secKill.exception.RepeatKillException;
import org.secKill.exception.SecKillCloseException;
import org.secKill.exception.SecKillException;

import java.util.List;

/**
 * Created by 谭雪娇 on 2017/5/5.
 */
public interface SecKillService {
    /*
    * 查询所有秒杀记录
    * @return
    * */
    List<SecKill> getSecKillList();
    /*
    * c查询单个秒杀记录
    * @Param SecKillId
    * @return
    * */
    SecKill getById(long secKillId);
    /*
    * 秒杀开启时输出秒杀接口地址,
    * 否则输出系统时间
    * @param secKillId
    * */
    Exposer exportSecKillUrl(long secKillId);
    /*
    * 执行秒杀操作
    * @param secKillId
    * @param userPhone
    * @param md5*/
    SecKillExecution executeZSecKill(long secKillId, long userPhone, String md5) throws SecKillException,RepeatKillException,SecKillCloseException;
}

 

 

建立dto包,创建暴露秒杀接口dto(数据传输层)

Exposer.java

package org.secKill.dto;

/**
 * Created by 谭雪娇 on 2017/5/5.
 * 暴露秒杀接口DTO
 */
public class Exposer {
    //是否开启秒杀
    private boolean exposed;
    //一种加密措施
    private String md5;
    //id
    private long secKillId;
    //开始时间
    private long start;
    //结束时间
    private long end;

    public Exposer(boolean exposed, String md5, long secKillId) {
        this.exposed = exposed;
        this.md5 = md5;
        this.secKillId = secKillId;
    }

    public Exposer(boolean exposed, String md5, long start, long secKillId, long end) {
        this.exposed = exposed;
        this.md5 = md5;
        this.start = start;
        this.secKillId = secKillId;
        this.end = end;
    }

    public Exposer(boolean exposed, long secKillId) {
        this.exposed = exposed;
        this.secKillId = secKillId;
    }

    public boolean isExposed() {
        return exposed;
    }

    public void setExposed(boolean exposed) {
        this.exposed = exposed;
    }

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    public long getSecKillId() {
        return secKillId;
    }

    public void setSecKillId(long secKillId) {
        this.secKillId = secKillId;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "Exposer{" +
                "exposed=" + exposed +
                ", md5=‘" + md5 + \‘+
                ", secKillId=" + secKillId +
                ", start=" + start +
                ", end=" + end +
                ‘}‘;
    }
}

 

创建包enums,创建枚举类型SecKillStateEnum.java使用枚举表述数据常量字典

SecKillStateEnum.java

package org.secKill.enums;

/**
 * 使用枚举表述常量数据字典
 *
 * @author yan
 */
public enum SecKillStateEnum {

    SUCCESS(1, "秒杀成功"), END(0, "秒杀结束"),

    REPEAT_KILL(-1, "重复秒杀"),

    INNER_ERROR(-2, "系统异常"),

    DATA_REWRITE(-3, "数据篡改");

    private int state;

    private String stateInfo;

    private SecKillStateEnum(int state, String stateInfo) {
        this.state = state;
        this.stateInfo = stateInfo;
    }

    public int getState() {
        return state;
    }

    public String getStateInfo() {
        return stateInfo;
    }

    public static SecKillStateEnum stateOf(int index) {
        for (SecKillStateEnum state : values()) {
            if (state.getState() == index) {
                return state;
            }
        }
        return null;
    }

}

 

 

dto包内创建封装秒杀执行后结果类SecKillExecution.java

package org.secKill.dto;

import org.secKill.entity.SuccessKilled;
import org.secKill.enums.SecKillStateEnum;

/**
 * Created by 谭雪娇 on 2017/5/5.
 * 封装秒杀执行后结果
 */
public class SecKillExecution {
    private long secKillId;
    //秒杀执行结果状态
    private int state;
    //状态标识
    private String stateInfo;
    //秒杀成功对象
    private SuccessKilled successKilled;

    public SecKillExecution(long secKillId, SecKillStateEnum stateEnum , SuccessKilled successKilled) {
        this.secKillId = secKillId;
        this.state = stateEnum.getState();
        this.stateInfo=stateEnum.getStateInfo();
        this.successKilled=successKilled;

    }

    public SecKillExecution(long secKillId,SecKillStateEnum stateEnum) {
        this.secKillId = secKillId;
        this.state =stateEnum.getState();
        this.stateInfo = stateEnum.getStateInfo();
    }

    public long getSecKillId() {
        return secKillId;
    }

    public void setSecKillId(long secKillId) {
        this.secKillId = secKillId;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getStateInfo() {
        return stateInfo;
    }

    public void setStateInfo(String stateInfo) {
        this.stateInfo = stateInfo;
    }

    public SuccessKilled getSuccessKilled() {
        return successKilled;
    }

    public void setSuccessKilled(SuccessKilled successKilled) {
        this.successKilled = successKilled;
    }
}

 

创建包exception创建三个异常

创建SecKillException.java 秒杀执行异常

package org.secKill.exception;

/**
 * Created by 谭雪娇 on 2017/5/5.
 */
public class SecKillException extends RuntimeException{
    public SecKillException(String message){super(message);}
    public SecKillException(String message,Throwable cause){super(message,cause);}
}

 

 

重复秒杀异常RepeatKillException.继承SecKillException.java

package org.secKill.exception;

import org.secKill.dto.SecKillExecution;
import org.secKill.entity.SuccessKilled;
import org.secKill.enums.SecKillStateEnum;

/**
 * Created by 谭雪娇 on 2017/5/5.
 */
public class RepeatKillException extends SecKillException{
    public RepeatKillException(String message) {
        super(message);
    }

    public RepeatKillException(String message, Throwable cause) {
        super(message, cause);
    }
}

 

创建SecKillException.java,表示秒杀关闭异常

package org.secKill.exception;

/**
 * Created by 谭雪娇 on 2017/5/5.
 * 秒杀关闭异常
 */
public class SecKillCloseException extends SecKillException {
    public SecKillCloseException(String message) {
        super(message);
    }

    public SecKillCloseException(String message, Throwable cause) {
        super(message, cause);
    }
}

 

 

 

 

 

秒杀Servlce接口设计

标签:使用   ring   owa   oid   super   结束时间   继承   led   exec   

原文地址:https://www.cnblogs.com/sinceForever/p/8454484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!