设计模式 之 建造者

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

 

下载 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;
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

目录
相关文章
|
6月前
|
设计模式 Java 数据库连接
解锁设计模式的神秘面纱:编写无懈可击的代码之建造者设计模式
解锁设计模式的神秘面纱:编写无懈可击的代码之建造者设计模式
31 0
|
8月前
|
设计模式
设计模式-创建型模式:建造者
设计模式-创建型模式:建造者
|
12月前
|
设计模式 Java C++
设计模式-建造者设计模式
建造者模式(Builder Pattern),又叫做生成器模式,是一种对象构建模式。它可以将复杂对象的建造过程抽象出来,使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。 建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容可构建它,用户不需要知道内部的具体构建细节。
71 0
|
设计模式 算法 Java
【设计模式 】| 建造者源码学习与实践
为什么要用建造者模式?在我们看来他和工厂模式的目的是一样的,就是为了获取对象。
|
设计模式
设计模式之建造者
设计模式之建造者
93 0
设计模式之建造者
|
设计模式
设计模式是什么鬼(建造者)
设计模式是什么鬼(建造者)
设计模式是什么鬼(建造者)
|
设计模式 缓存 Dart
dart设计模式之建造者和原型模式
这是我参与8月更文挑战的第 13 天,活动详情查看:8月更文挑战。为应掘金的八月更文挑战, 建造者模式 模式分析 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。 模式难点 如何抽象出不同特性需要很强的抽象思维
239 0
|
算法 设计模式
设计模式--生成器(建造者)
生成器模式(Builder) 生成器模式最初的定义出现于《设计模式》(Addison-Wesley,1994) 生成器模式:将一个复杂对象的构建与它的表现分离,使得同样的构建过程可以创建不同的表现。
769 0
设计模式--建造者设计模式
BUILDER?MM最爱听的就是“我爱你”这句话了,见到不同地方的MM,要能够用她们的方言跟她说这句话哦,我有一个多种语言翻译机,上面每种语言都有一个按键,见到MM我只要按对应的键,它就能够用相应的语言说出“我爱你”这句话了,国外的MM也可以轻松搞掂,这就是我的“我爱你”builder。
783 0