//加薪代码初步
/*class Request{
private String requestType;
private String requestContent;
private int num;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
class Manager{
protected String name;
public Manager(String name) {
super();
this.name = name;
}
public void GetResult(String managerLevel,Request request) {
System.out.println(request.getRequestType()+":"+request.getNum());
if(managerLevel == "经理") {
if(request.getRequestType() == "请假" && request.getNum()<=2) {
System.out.println("数量<=2,被批准");
}
else {
System.out.println("数量>2,无权批准");
}
}
else if(managerLevel == "总监") {
if(request.getRequestType() == "请假" && request.getNum()<=5) {
System.out.println("数量<=5,被批准");
}
else {
System.out.println("数量>5,无权批准");
}
}
else if(managerLevel == "总经理") {
if(request.getRequestType() == "请假") {
System.out.println("请假,被批准");
}
else if(request.getRequestType() == "加薪" && request.getNum()<=500) {
System.out.println("加薪<=500,被批准");
}
else if(request.getRequestType() == "加薪" && request.getNum()>500) {
System.out.println("加薪>500,无权批准");
}
}
}
}
public class Main {
public static void main(String args[])
{
Manager m1 = new Manager("经理");
Manager m2 = new Manager("总监");
Manager m3 = new Manager("总经理");
Request r = new Request();
r.setRequestType("加薪");
r.setRequestContent("小菜请求加薪");
r.setNum(1000);
m1.GetResult("经理", r);
m2.GetResult("总监", r);
m3.GetResult("总经理", r);
System.out.println("=====");
}
}*/
//重构加薪代码
/*class Request{
private String requestType;
private String requestContent;
private int num;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
abstract class Manager{
protected String name;
protected Manager superior;
public Manager(String name) {
super();
this.name = name;
}
public void setSuperior(Manager superior) {
this.superior = superior;
}
abstract public void RequestApplications(Request request);
}
class CommonManager extends Manager{
public CommonManager(String name) {
super(name);
}
public void RequestApplications(Request request) {
// TODO Auto-generated method stub
if(request.getRequestType() == "请假" && request.getNum()<=2) {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNum()+"被批准");
}
else {
if(superior !=null) {
superior.RequestApplications(request);
}
}
}
}
class Majordomo extends Manager{
public Majordomo(String name) {
super(name);
}
public void RequestApplications(Request request) {
// TODO Auto-generated method stub
if(request.getRequestType() == "请假" && request.getNum()<=5) {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNum()+"被批准");
}
else {
if(superior !=null) {
superior.RequestApplications(request);
}
}
}
}
class GeneralManager extends Manager{
public GeneralManager(String name) {
super(name);
}
public void RequestApplications(Request request) {
// TODO Auto-generated method stub
if(request.getRequestType() == "请假") {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNum()+"被批准");
}
else if(request.getRequestType() == "加薪" && request.getNum()<=500) {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNum()+"被批准");
}
else {
System.out.println(name+":"+request.getRequestContent()+"数量"+request.getNum()+"再说吧");
}
}
}
public class Main {
public static void main(String args[])
{
CommonManager cm = new CommonManager("经理");
Majordomo m = new Majordomo("总监");
GeneralManager gm = new GeneralManager("总经理");
cm.setSuperior(m);
m.setSuperior(gm);
Request r = new Request();
r.setRequestType("请假");
r.setRequestContent("小菜请求请假");
r.setNum(1);
cm.RequestApplications(r);
Request r2 = new Request();
r2.setRequestType("请假");
r2.setRequestContent("小菜请求请假");
r2.setNum(4);
cm.RequestApplications(r2);
Request r3 = new Request();
r3.setRequestType("加薪");
r3.setRequestContent("小菜请求加薪");
r3.setNum(1000);
cm.RequestApplications(r3);
}
}*/
//职责链模式
abstract class Handler{
protected String name;
protected Handler successor;
public void setSuccessor(Handler successor) {
this.successor = successor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract public void HandlRequest(int request);
}
class ConcreteHandler1 extends Handler{
public void HandlRequest(int request) {
// TODO Auto-generated method stub
if(request>=0&&request<10) {
System.out.println(this.getName()+"处理请求"+request);
}
else if(successor != null) {
successor.HandlRequest(request);
}
}
}
class ConcreteHandler2 extends Handler{
public void HandlRequest(int request) {
// TODO Auto-generated method stub
if(request>=10&&request<20) {
System.out.println(this.getName()+"处理请求"+request);
}
else if(successor != null) {
successor.HandlRequest(request);
}
}
}
class ConcreteHandler3 extends Handler{
public void HandlRequest(int request) {
// TODO Auto-generated method stub
if(request>=20) {
System.out.println(this.getName()+"处理请求"+request);
}
else if(successor != null) {
successor.HandlRequest(request);
}
}
}
public class Main {
public static void main(String args[])
{
Handler h1 = new ConcreteHandler1();
h1.setName("审批人1");
Handler h2 = new ConcreteHandler2();
h2.setName("审批人2");
Handler h3 = new ConcreteHandler3();
h3.setName("审批人3");
h1.setSuccessor(h2);
h2.setSuccessor(h3);
int[] requests = {2,5,14,22,18,3,27,20};
for(int i=0;i<requests.length;i++) {
h1.HandlRequest(requests[i]);
}
}
}
迭代器模式一步步实现
最新推荐文章于 2021-04-04 20:26:21 发布