使用enum的责任链

package enumerated;

import java.util.Iterator;

class Mail {
	enum GeneralDelivery{
		YES, NO1, NO2, NO3, NO4, NO5
	}
	enum Scannability{
		UNSCANNABLE, YES1, YES2, YES3, YES4
	}
	enum Readability{
		ILLEGIBLE, YES1, YES2, YES3, YES4
	}
	enum Address{
		INCORRECT, OK1, OK2, OK3, OK4, OK5, OK6
	}
	enum ReturnAddress{
		MISSING, OK1, OK2, OK3, OK4, OK5, OK6
	}
	GeneralDelivery generalDelivery;
	Scannability scannability;
	Readability readability;
	Address address;
	ReturnAddress returnAddress;
	static long counter = 0;
	long id = counter++;
	public String toString(){
		return "Mail " + id;
	}
	public String details(){
		return toString() +
			". General Delivery: " + generalDelivery +
			". Address Scanability: " + scannability +
			". Address Readability: " + readability +
			". Address Address: " + address +
			". return address: " + returnAddress;
	}
	public static Mail randomMail() {
		Mail m = new Mail();
		m.generalDelivery = Enums.random(GeneralDelivery.class);
		m.scannability = Enums.random(Scannability.class);
		m.readability = Enums.random(Readability.class);
		m.address = Enums.random(Address.class);
		m.returnAddress = Enums.random(ReturnAddress.class);
		return m;
	}
	
	public static Iterable<Mail> generator(final int count){
		return new Iterable<Mail>(){
			int n = count;
			public Iterator<Mail> iterator() {
				return new Iterator<Mail>(){
					public boolean hasNext() {
						return n-->0;
					}
					public Mail next() {
						return randomMail();
					}
					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}
}
public class PostOffice{
	enum MailHandler{
		GENERAL_DELIVERY{
			boolean handle(Mail m){
				switch(m.generalDelivery){
					case YES:
						System.out.println("Using general delivery for "+ m);
						return true;
					default:
						return false;
				}
			}
		},
		MACHINE_SCAN{
			boolean handle(Mail m){
				switch(m.scannability){
					case UNSCANNABLE: return false;
					default:
						switch(m.address){
						case INCORRECT : return false;
						default:
							System.out.println("Delivering"+m+" automatically");
							return true;
						}
				}
			}
		},
		VISUAL_INSPECTION{
			boolean handle(Mail m){
				switch(m.readability){
				case ILLEGIBLE: return false;
				default:
					switch(m.address){
					case INCORRECT:  return false;
					default:
						System.out.println("Delivering "+m+" normally");
						return true;
					}
				}
			}
		},
		RETURN_TO_SENDER{
			boolean handle(Mail m){
				switch(m.returnAddress){
				case MISSING: return false;
				default:
					System.out.println("Returning "+m+" to sender");
					return true;
				}
			}
		};
		abstract boolean handle(Mail m);
	}
	static void handle(Mail m){
		for(MailHandler handler : MailHandler.values())
			if(handler.handle(m))
				return;
		System.out.println(m+ " is a deat letter");
	}
	public static void main(String[] args) {
		for(Mail mail : Mail.generator(10)){
			System.out.println(mail.details());
			handle(mail);
			System.out.println("*****");
		}
	}
}

 

 

责任链有enum Mailhandler实现,而enum定义的次序决定了各个解决策略

在应用时的次序。对每一封邮件, 都要按照此顺序尝试每个解决策略,

直到其中一个能够成功处理该邮件,如果所有策略都失败,那么

该邮件将被判定为一封死邮件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值