设计模式 之 建造者

 

下载 23种设计模式源码 :http://download.csdn.net/download/knight_black_bob/8936043

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


创建型模式,共五种:
工厂方法模式 抽象工厂模式 单例模式 建造者模式 原型模式

结构型模式,共七种:
适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式

行为型模式,共十一种:
策略模式 模板方法模式 观察者模式 迭代子模式 责任链模式 命令模式

备忘录模式 状态模式 访问者模式 中介者模式 解释器模式

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

 

 

 

 

每次见 建造者 都是建造 一个 汽车  需要 一个 轮子 等 ,想不到怎么用 ,现在 拼 json  就可以 用得到啊!············ 

 

 

 

packag e 设计模式.建造者;


import java.util.Date;

public class Notification
{

	public Notification()
	{

	}

	int id;
	byte[] token; // 据官方的文档,当command是2时,这个必须是32 bytes的。是二进制格式的。
	byte[] payload; 
	Date expirationDate;
 
	static final int defaultPriority = 10;

	int priority = defaultPriority;

	public int getPriority()
	{
		return priority;
	}

	public void setPriority(int priority)
	{
		this.priority = priority;
	}

	public byte[] getToken()
	{
		return token;
	}

	public void setToken(byte[] token)
	{
		this.token = token;
	}

	public byte[] getPayload()
	{
		return payload;
	}

	public void setPayload(byte[] payload)
	{
		this.payload = payload;
	}

	public Date getExpirationDate()
	{
		return expirationDate;
	}

	public void setExpirationDate(Date expirationDate)
	{
		this.expirationDate = expirationDate;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{").append("\"expirationDate\":").append(expirationDate).append("\"id\":").append(id).append(
				",\"payload\":").append(payload.toString()).append(",\"priority\":").append(priority).append(
				",\"token\":").append(token.toString()).append("}");
		return sb.toString();
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj != null)
			if (obj instanceof Notification)
				return this.id == ((Notification) obj).getId();
		return false;
	}
}

 

 

 

package com.cmcc.apns.message;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import com.alibaba.fastjson.JSONObject;

/**
 * 辅助生成Notification的工具类。
 * 
 * <pre>
 * Notification notification = new NotificationBuilder().setToken(token).setBadge(1).setAlert(&quot;just test&quot;).build();
 * </pre>
 * 
 */
public class NotificationBuilder
{
	AtomicInteger ai = new AtomicInteger(0);

	public AtomicInteger getAi()
	{
		return ai;
	}

	public void setAi(AtomicInteger ai)
	{
		this.ai = ai;
	}

	byte[] token = null;
	int priority = Notification.defaultPriority;
	// new Date(3000, 1, 1).getTime() == 92464560000000L
	private Date expirationDate = new Date(92464560000000L);

	JSONObject payload = new JSONObject();
	JSONObject aps = new JSONObject();
	String alert = null;
	String content;
	JSONObject alertObject = new JSONObject();

	final HashMap<String, Object> customProperties = new HashMap<String, Object>();
	static final Charset utf8 = Charset.forName("utf-8");

	static final int MAX_PAYLOAD_SIZE = 256;

	public NotificationBuilder(AtomicInteger ai)
	{
		this.ai = ai;
	}

	public Notification build()
	{
		Notification notification = new Notification();
		notification.setId(ai.intValue());
		if (token != null)
		{
			notification.setToken(token);
		}
		else
		{
			throw new IllegalArgumentException("token is null!");
		}
		notification.setPriority(priority);
		// TODO 这里是否把没有设置过期时间的通知都设置成无限的?
		notification.setExpirationDate(expirationDate);

		/**
		 * <pre>
		 * 因为这里有两种格式,一种是:
		 *     "aps" : {
		 *         "alert" : "You got your emails.",
		 *         "badge" : 9,
		 *         "sound" : "bingbong.aiff"
		 *     },
		 * 
		 * 另一种是:
		 * "aps" : {
		 *    "alert" : {
		 *    "body" : "Bob wants to play poker",
		 *      "action-loc-key" : "PLAY"
		 *    },
		 *    "badge" : 5,
		 *  },
		 * </pre>
		 */
		if (alert != null)
		{
			aps.put("alert", alert);
		}
		else
		{
			aps.put("alert", alertObject);
		}
		aps.put("sound", "default");
		payload.put("aps", aps);
		//修改 apn 格式 content 放到 apns  外面
		if (content != null)
			payload.put("content", content);
		
		byte[] bytes = payload.toString().getBytes(utf8);
		if (bytes.length > MAX_PAYLOAD_SIZE)
		{
			throw new IllegalArgumentException("payload.length >" + MAX_PAYLOAD_SIZE);
		}
		notification.setPayload(bytes);
		return notification;
	}

	public NotificationBuilder setToken(byte[] token)
	{
		if (token.length != 32)
		{
			throw new IllegalArgumentException("token.length != 32");
		}
		this.token = token;
		return this;
	}

	// TODO 这里应该可以自动去掉中间的空白字符
	public NotificationBuilder setToken(String token)
	{
		try
		{
			byte[] hex = Hex.decodeHex(token.toCharArray());
			setToken(hex);
		}
		catch (DecoderException e)
		{
			throw new RuntimeException(e);
		}
		return this;
	}

	public NotificationBuilder setPriority(int priority)
	{
		this.priority = priority;
		return this;
	}

	public NotificationBuilder setExpirationDate(Date expirationDate)
	{
		this.expirationDate = expirationDate;
		return this;
	}

	public NotificationBuilder setBadge(int badge)
	{
		aps.put("badge", badge);
		return this;
	}

	public NotificationBuilder setSound(String sound)
	{
		aps.put("sound", sound);
		return this;
	}

	public NotificationBuilder setcontentAvailable(boolean contentAvilable)
	{
		aps.put("content-available", 1);
		return this;
	}

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public NotificationBuilder setAlert(String alert)
	{
		this.alert = alert;
		return this;
	}

	public NotificationBuilder setAlertBody(String alertBody)
	{
		alertObject.put("body", alertBody);
		return this;
	}

	public NotificationBuilder setAlertActionLocKey(String alertActionLocKey)
	{
		alertObject.put("action-loc-key", alertActionLocKey);
		return this;
	}

	public NotificationBuilder setAlertLocKey(String alertLocKey)
	{
		alertObject.put("loc-key", alertLocKey);
		return this;
	}

	public NotificationBuilder setAlertLocArgs(String... alertLocArgs)
	{
		alertObject.put("loc-args", alertLocArgs);
		return this;
	}

	public NotificationBuilder setAlertLocArgs(List<String> alertLocArgs)
	{
		alertObject.put("loc-args", alertLocArgs);
		return this;
	}

	public NotificationBuilder setAlertLunchImage(String alertLunchImage)
	{
		alertObject.put("launch-image", alertLunchImage);
		return this;
	}

	public NotificationBuilder setUserProperty(String key, Object value)
	{
		payload.put(key, value);
		return this;
	}
}

 

你可能感兴趣的:(设计模式,建造者)