秒殺Servlce接口設計

秒殺Servlce接口設計

一、建立service包,建立SecKillServlce業務接口java

SecKillServlce.Javaide

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;
}this

 

 

創建dto包,建立暴露秒殺接口dto(數據傳輸層)加密

Exposer.javaspa

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.javamd5

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);    }}

相關文章
相關標籤/搜索