import java.io.File;
import java.util.Date;
import java.util.List;
import javax.mail.BodyPart;
public interface IMail {
public void set主題(String 主題);
public void setText(String Text);
public void setCotnent(BodyPart content);
public void set發件人(Email地址 發件人);
public void set收件人(List<Email地址> 收件人);
public void set抄送人(List<Email地址> 抄送人);
public void set暗送人(List<Email地址> 暗送人);
public void set郵件時間(Date time);
public void set附件(List<File> files);
public Email地址 get發件人();
public List<Email地址> get收件人();
public List<Email地址> get抄送人();
public List<Email地址> get暗送人();
public List<File> get附件();
public Date get郵件時間();
public String get主題();
public String getText();
public BodyPart getContent();
}
<2>Mail類
import java.io.File;
import java.util.Date;
import java.util.List;
import javax.mail.BodyPart;
public class Mail implements IMail{
private Email地址 發件人;
private String 主題;
private String text;
private BodyPart content;
private List<Email地址> 抄送人;
private List<Email地址> 收件人;
private List<Email地址> 暗送人;
private Date 郵件時間 ;
private List<File> 附件;
public Email地址 get發件人() {
return this.發件人;
}
public List<Email地址> get抄送人() {
return this.抄送人;
}
public List<Email地址> get收件人() {
return this.收件人;
}
public List<Email地址> get暗送人() {
return this.暗送人;
}
public Date get郵件時間() {
return this.郵件時間;
}
public List<File> get附件() {
return this.附件;
}
public void set發件人(Email地址 發件人) {
this.發件人 = 發件人;
}
public void set抄送人(List<Email地址> 抄送人) {
this.抄送人 = 抄送人;
}
public void set收件人(List<Email地址> 收件人) {
this.收件人 = 收件人;
}
public void set暗送人(List<Email地址> 暗送人) {
this.暗送人 = 暗送人;
}
public void set郵件時間(Date time) {
this.郵件時間 = time;
}
public void set附件(List<File> files) {
this.附件 = files;
}
public BodyPart getContent() {
return this.content;
}
public String getText() {
return this.text;
}
public String get主題() {
return this.主題;
}
public void setCotnent(BodyPart content) {
this.content = content;
}
public void setText(String text) {
this.text = text;
}
public void set主題(String 主題) {
this.主題 = 主題;
}
}
<3>Abstract郵件發送服務器
import java.io.File;
import java.util.Date;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public abstract class Abstract郵件發送服務器 {
protected IMail 郵件;
protected Session session;
protected String send方式;
public Abstract郵件發送服務器() {
}
protected InternetAddress[] getAddressByType(List<Email地址> list) throws Exception {
if (list != null) {
InternetAddress address[] = new InternetAddress[list.size()];
for (int i = 0 ; i < list.size(); i++) {
address[i] = list.get(i).toInternetAddress();
}
return address;
}
return null;
}
public void 發送郵件() {
try {
MimeMessage message = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
message.setSubject(郵件.get主題(),"utf-8");
if(郵件.getText() != null)
message.setText(郵件.getText(),"utf-8");
message.setSentDate(郵件.get郵件時間()==null?new Date():this.郵件.get郵件時間());
multipart.addBodyPart(this.郵件.getContent());
//添加發件人
message.setFrom(郵件.get發件人().toInternetAddress());
//添加收件人
InternetAddress address[] = this.getAddressByType(this.郵件.get收件人());
if (address != null)
message.addRecipients(Message.RecipientType.TO,address);
//添加抄送人
address = this.getAddressByType(this.郵件.get抄送人());
if (address != null)
message.addRecipients(Message.RecipientType.CC,address);
//添加暗送人
address = this.getAddressByType(this.郵件.get暗送人());
if (address != null)
message.addRecipients(Message.RecipientType.BCC,address);
//添加附件
if (this.郵件.get附件() != null) {
for (int i = 0; i < this.郵件.get附件().size(); i++) {
File file = this.郵件.get附件().get(i);
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(MimeUtility.encodeWord(file.getName(),"UTF-8","Q"));
multipart.addBodyPart(messageBodyPart);
}
}
message.setContent(multipart);
if(send方式 == null)
send方式 = "smtp";
session.getTransport(send方式).send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public IMail get郵件() {
return 郵件;
}
public void set郵件(IMail 郵件) {
this.郵件 = 郵件;
}
}
<4>Abstract發送郵件建造者
import javax.mail.Session;
public abstract class Abstract發送郵件建造者 {
protected Abstract郵件發送服務器 mailServer;
public Abstract發送郵件建造者() {
}
public abstract void 建造郵件(IMail mail);
public abstract void 建造郵件session(Session session);
public void 建造發送郵件() {
this.mailServer.發送郵件();
}
}
<5>Smtp郵件發送服務器
public class Smtp郵件發送服務器 extends Abstract郵件發送服務器 {
public Smtp郵件發送服務器() {
this.send方式 = "smtp";
}
}
<6>Smtp發送郵件建造者
import java.util.Properties;
import javax.mail.Session;
public class Smtp發送郵件建造者 extends Abstract發送郵件建造者 {
public Smtp發送郵件建造者() {
this.mailServer = new Smtp郵件發送服務器();
}
@Override
public void 建造郵件(IMail mail) {
this.mailServer.set郵件(mail);
}
@Override
public void 建造郵件session(Session session) {
this.mailServer.setSession(session);
}
}
<7>PasswordAuthenticator
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
<8>Email地址
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
public class Email地址 {
protected String 地址;
protected String 地址顯示別名;
public Email地址() {
}
public Email地址(String 地址, String 地址顯示別名){
this.地址 = 地址;
this.地址顯示別名 = 地址顯示別名;
}
public String get地址() {
return 地址;
}
public void set地址(String 地址) {
this.地址 = 地址;
}
public String get地址顯示別名() {
return 地址顯示別名;
}
public void set地址顯示別名(String 地址顯示別名) {
this.地址顯示別名 = 地址顯示別名;
}
public InternetAddress toInternetAddress() throws Exception {
if (地址顯示別名 != null && !地址顯示別名.trim().equals("")) {
return new InternetAddress(地址, MimeUtility.encodeWord(地址顯示別名,
"utf-8", "Q"));
}
return new InternetAddress(地址);
}
}
<9>發送郵件Director
import javax.mail.Session;
public class 發送郵件Director {
Abstract發送郵件建造者 build;
public 發送郵件Director(Abstract發送郵件建造者 build) {
this.build = build;
}
public void 發送郵件建造(IMail mail, Session session) {
this.build.建造郵件(mail);
this.build.建造郵件session(session);
this.build.建造發送郵件();
}
}
<10>Client
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
public class Client {
public static void main(String args[]) throws Exception {
IMail mail = new Mail();
mail.set主題("????");
mail.set發件人(new Email地址("james_zhao_abm@163.com","趙成明"));
List<Email地址> list = new ArrayList<Email地址>();
list.add(new Email地址("jszhaochengming@hotmail.com","劉德華"));
mail.set收件人(list);
list = new ArrayList<Email地址>();
list.add(new Email地址("james_zhao_abm@163.com","純凈水"));
list.add(new Email地址("jszhaochengming@hotmail.com","劉德華"));
mail.set抄送人(list);
BodyPart contentPart = new MimeBodyPart();
contentPart.setHeader("Content-Transfer-Encoding", "base64");
contentPart.setContent("測試喲見呵呵","text/html;charset=utf-8");
mail.setCotnent(contentPart);
List<File> listF = new ArrayList<File>();
listF.add(new File("d:/??????2.rar"));
mail.set附件(listF);
Smtp發送郵件建造者 smtp = new Smtp發送郵件建造者();
Properties 屬性配置 = new Properties();
屬性配置.put("mail.smtp.host", "smtp.163.com");
屬性配置.put("mail.smtp.auth", "true");
Session session = Session.getInstance(屬性配置,new PasswordAuthenticator("xxxxx", "xxxxx"));
發送郵件Director director = new 發送郵件Director(smtp);
director.發送郵件建造(mail, session);
}
}
簡單工廠模式是由一個工廠對象來決定創建出哪一種產品類的對象。
簡單工廠模式就是由一個工廠類根據傳入的參數決定創建出哪一種產品類的對象。
/*******************************************/
package 工廠.簡單工廠模式;
public interface 汽車 {
public void 開車();
public void 停車();
public void 年檢();
public void 鳴笛();
//...等等
}
/*******************************************/
package 工廠.簡單工廠模式;
public class 寶馬汽車 implements 汽車 {
public static final String ;
public static final String BRAND = "寶馬汽車";
public void 開車() {
System.out.println(BRAND+".開車");
}
public void 停車() {
System.out.println(BRAND+".停車");
}
public void 年檢() {
System.out.println(BRAND+".年檢");
}
public void 鳴笛() {
System.out.println(BRAND+".鳴笛");
}
}
/*******************************************/
package 工廠.簡單工廠模式;
public class 奔馳汽車 implements 汽車 {
public static final String ;
public static final String BRAND = "奔馳汽車";
public void 開車() {
System.out.println(BRAND+".開車");
}
public void 停車() {
System.out.println(BRAND+".停車");
}
public void 年檢() {
System.out.println(BRAND+".年檢");
}
public void 鳴笛() {
System.out.println(BRAND+".鳴笛");
}
}
/*******************************************/
package 工廠.簡單工廠模式;
public class 汽車銷售工廠 {
public static 汽車 試車(String ID) throws Exception {
if (ID.equals(寶馬汽車.ID)) {
return new 寶馬汽車();
} else if (ID.equals(奔馳汽車.ID)) {
return new 奔馳汽車();
}
throw new Exception("沒有這個品牌的汽車!");
}
}
/*******************************************/
package 工廠.簡單工廠模式;
public class 消費者 {
public void 試試汽車(String ID) {
try {
汽車 car = 汽車銷售工廠.試車(ID);
car.開車();
car.停車();
car.鳴笛();
} catch (Exception e) {
System.out.println("連寶馬都沒有,換個銷售公司!");
e.printStackTrace();
}
}
public static void main(String args[]) {
消費者 王二 = new 消費者();
王二.試試汽車(寶馬汽車.ID);
王二.試試汽車(奔馳汽車.ID);
}
}
我們使用的界面上有
文本 {Linux文本,Windows文本}
標簽 {Linux標簽,Windows標簽}
等等.
第一部分 抽象工廠的實現
interface 文本 {}
interface 標簽 {}
class Linux文本 implements 文本{
public String toString() {
return "Linux文本";
}
}
class Linux標簽 implements 標簽{
public String toString() {
return "Linux標簽";
}
}
class Windows文本 implements 文本{
public String toString() {
return "Windows文本";
}
}
class Windows標簽 implements 標簽{
public String toString() {
return "Windows標簽";
}
}
interface 組件工廠 {
public 文本 生產文本組件();
public 標簽 生產標簽組件();
}
class Linux組件工廠 implements 組件工廠 {
public 文本 生產文本組件() {
return new Linux文本();
}
public 標簽 生產標簽組件() {
return new Linux標簽();
}
}
class Windows標簽組件工廠 implements 組件工廠 {
public 文本 生產文本組件() {
return new Windows文本();
}
public 標簽 生產標簽組件() {
return new Windows標簽();
}
}
class 客戶系統顯示 {
private 文本 text;
private 標簽 label;
public static void main(String args[]) {
客戶系統顯示 clientOS = new 客戶系統顯示();
組件工廠 factory = new Linux組件工廠();
clientOS.label = factory.生產標簽組件();
clientOS.text = factory.生產文本組件();
System.out.println(clientOS.label);
System.out.println(clientOS.text);
}
}
如果按照上面的標準,我們要添加一個新的組件 下拉框
A.需要修改的地方有
1.組件工廠
2.Linux組件工廠
3.Windows標簽組件工廠
B.需要增加的有
1.interface 下拉框 {}
2.class Linux下拉框 implements 下拉框
3.class Windows下拉框 implements 下拉框
C.調用的地方也會多出一個factory.生產下拉框組件();
第二部分 改革抽象工廠
有沒有覺得要改動的地方有點多呢,下面我們來改革一下
1.把 組件工廠中的
生產文本組件();
生產標簽組件();
...
都改為
生產組件(組件標識);
這樣帶來的好處就是前面提到的,以下的修改就免去了
/**************************/
......
A.需要修改的地方有
1.組件工廠
2.Linux組件工廠
3.Windows標簽組件工廠
......
/**************************/
要做到上面的,需要做以下幾件事情
1.增加一個Annotation來說明后面增加的 組件注冊表
@interface 組件描述 {
Class 組件類();
}
2.增加一個Enum
enum 組件注冊表 {
/**
* Linux_文本 的對應實體類為 Linux文本
*/
@組件描述(組件類 = Linux文本.class)
Linux_文本,
@組件描述(組件類 = Linux標簽.class)
Linux_標簽,
@組件描述(組件類 = Windows文本.class)
Windows_文本,
@組件描述(組件類 = Windows標簽.class)
Windows_標簽,
}
3.我們不再需要
interface 組件工廠,class Windows標簽組件工廠,class Linux組件工廠
我們把 接口 組件工廠改為實體類
為了保持可以擴展和維護
我們定義了一個 接口 工廠
interface 工廠 {
}
class 組件工廠 implements 工廠 {
public 組件 生產組件(組件注冊表 ID) throws Exception {
try {
Field f = 組件注冊表.class.getField(ID.toString());
組件描述 描述 = f.getAnnotation(組件描述.class);
Class 組件類 = 描述.組件類();
return (組件) 組件類.newInstance();
// 注意,組件類.newInstance();的調用的時候要確保這個組件類有個不帶參數的構造函數
// 如果要使用帶參數的構造函數,可以在@interface 組件描述 中增加一個成員
// 構造函數[] 構造函數參數() default{};
// @interface 構造函數 {Class[] 構造函數的參數();}
// 通過 組件類.getConstructors(); 來得到這個類的不同構造方法
// 這樣就可以根據用戶提供的信息用不同的構造函數實例話對象
// 帶不同的構造函數,這里先不討論,后面我會給出代碼
} catch (Exception e) {
throw new Exception ("沒有找到對應的組件");
}
}
}
經過上面的修改,代碼如下
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
@Retention(RetentionPolicy.RUNTIME)
@interface 組件描述 {
Class 組件類();
}
enum 組件注冊表 {
@組件描述(組件類 = Linux文本.class)
Linux_文本,
@組件描述(組件類 = Linux標簽.class)
Linux_標簽,
@組件描述(組件類 = Windows文本.class)
Windows_文本,
@組件描述(組件類 = Windows標簽.class)
Windows_標簽,
}
interface 組件 {}
interface 文本 extends 組件 {}
interface 標簽 extends 組件 {}
class Linux文本 implements 文本{
public String toString() {
return "Linux文本";
}
}
class Linux標簽 implements 標簽{
public String toString() {
return "Linux標簽";
}
}
class Windows文本 implements 文本{
public String toString() {
return "Windows文本";
}
}
class Windows標簽 implements 標簽{
public String toString() {
return "Windows標簽";
}
}
interface 工廠 {}
class 組件工廠 implements 工廠{
public 組件 生產組件(組件注冊表 ID) throws Exception {
try {
Field f = 組件注冊表.class.getField(ID.toString());
組件描述 描述 = f.getAnnotation(組件描述.class);
Class 組件類 = 描述.組件類();
return (組件) 組件類.newInstance();
} catch (Exception e) {
throw new Exception ("沒有找到對應的組件");
}
}
}
class 客戶系統顯示 {
private 文本 text;
private 標簽 label;
public static void main(String args[]) {
客戶系統顯示 clientOS = new 客戶系統顯示();
組件工廠 factory = new 組件工廠();
try {
clientOS.text = (文本) factory.生產組件(組件注冊表.Linux_文本);
clientOS.label = (標簽) factory.生產組件(組件注冊表.Linux_標簽);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(clientOS.label);
System.out.println(clientOS.text);
}
}
這個時候我們增加一個 下拉框
需要改動的地方
1.增加一個 interface 下拉框 extends 組件 {}
2.增加2個實現類
class Windows下拉框 implements 下拉框{}
class Linux下拉框implements 下拉框{}
3.組件注冊表 增加2個成員
@組件描述(組件類 = Linux下拉框.class)
Linux_下拉框,
@組件描述(組件類 = Windows下拉框.class)
Windows_下拉框,
和上面的比起來我們只需要在 組件注冊表中增加2個成員,而不需要去修改
1.組件工廠
2.Linux組件工廠
3.Windows標簽組件工廠
因為這里要修改3個地方,是不是覺得麻煩,反正我覺得麻煩了點
還有一點就是用戶調用的時候不需要再使用factory.生產標簽組件();等方法,只要一個factory.生產組件就可以了,這樣符合簡單工廠的模式
第三部分 帶參數的構造函數代碼
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@Retention(RetentionPolicy.RUNTIME)
@interface 構造函數 {
/**
* 構造函數的參數類型
* @return
*/
Class[] 構造函數的參數();
}
@Retention(RetentionPolicy.RUNTIME)
@interface 組件描述 {
Class 組件類();
/**
* 返回組件的構造函數 <br>
* 如果長度為0,則調用沒有參數的構造函數 <br>
* @return 構造函數[]
*/
構造函數[] 構造函數參數() default{};
}
enum 組件注冊表 {
/**
* Linux_文本 的對應實體類為 Linux文本 <br>
* Linux的構造函數有 <br>
* 1. Linux文本(String 顯示的文字) ; <br>
* 2. Linux文本(String 顯示的文字, Integer 文本字體大小);
*/
@組件描述(組件類 = Linux文本.class,
構造函數參數 = {@構造函數(構造函數的參數={String.class}) ,
@構造函數(構造函數的參數={String.class, Integer.class}) } )
Linux_文本,
@組件描述(組件類 = Linux標簽.class)
Linux_標簽,
@組件描述(組件類 = Windows文本.class)
Windows_文本,
@組件描述(組件類 = Windows標簽.class)
Windows_標簽,
}
interface 組件 {}
interface 文本 extends 組件 {}
interface 標簽 extends 組件 {}
class Linux文本 implements 文本{
private String text;
private Integer size;
public Linux文本(String text) {
this.text = text;
}
public Linux文本(String text, Integer size) {
this.text = text;
this.size = size;
}
public String toString() {
return "Linux文本" + (text == null ? "":",文本內容為:"+text) + (size == null ? "":",文本字體大小為:"+size);
}
}
class Linux標簽 implements 標簽{
public String toString() {
return "Linux標簽";
}
}
class Windows文本 implements 文本{
public String toString() {
return "Windows文本";
}
}
class Windows標簽 implements 標簽{
public String toString() {
return "Windows標簽";
}
}
interface 工廠 {}
class 組件工廠 implements 工廠{
public 組件 生產組件(組件注冊表 ID, Object[] 參數) throws Exception {
try {
Field f = 組件注冊表.class.getField(ID.toString());
組件描述 描述 = f.getAnnotation(組件描述.class);
Class 組件類 = 描述.組件類();
構造函數[] ano = 描述.構造函數參數();
if (參數 != null) {
for (int i = 0; i < ano.length; i++) {
構造函數 temp = ano;
Class[] 構造函數S = temp.構造函數的參數();
if (參數.length == 構造函數S.length) {
for (int j = 0; j < 參數.length; j++) {
if (參數[j].getClass().toString().equals(構造函數S[j].toString())) {
if ( j == 參數.length - 1) {
Constructor cons = 組件類.getConstructor(構造函數S);
return (組件) cons.newInstance(參數);
}
} else break;
}
}
continue;
}
throw new Exception ("沒有找到對應的組件");
} else
return (組件) 組件類.newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new Exception ("沒有找到對應的組件");
}
}
}
class 客戶系統顯示 {
private 文本 text;
private 標簽 label;
public static void main(String args[]) {
客戶系統顯示 clientOS = new 客戶系統顯示();
組件工廠 factory = new 組件工廠();
try {
Object [] params = {"初始化文本", new Integer(20)};
clientOS.text = (文本) factory.生產組件(組件注冊表.Linux_文本,params);
clientOS.label = (標簽) factory.生產組件(組件注冊表.Linux_標簽,null);
System.out.println(clientOS.label);
System.out.println(clientOS.text);
Object [] params2 = {"初始化"};
clientOS.text = (文本) factory.生產組件(組件注冊表.Linux_文本,params2);
System.out.println(clientOS.text);
} catch (Exception e) {
e.printStackTrace();
}
}
}