下載下傳 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("just test").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;
}
}
捐助開發者
在興趣的驅動下,寫一個
免費
的東西,有欣喜,也還有汗水,希望你喜歡我的作品,同時也能支援一下。 當然,有錢捧個錢場(右上角的愛心标志,支援支付寶和PayPal捐助),沒錢捧個人場,謝謝各位。

謝謝您的贊助,我會做的更好!