亚洲第一页在线播放,亚洲伦理中文字幕,亚洲精品自偷自拍无码http://www.tkk7.com/zhaochengming/category/24907.htmlzh-cnMon, 13 Aug 2007 13:37:37 GMTMon, 13 Aug 2007 13:37:37 GMT60建造模式--JavaMail發(fā)送郵件(多語言無亂碼)http://www.tkk7.com/zhaochengming/archive/2007/08/13/136481.htmlhelloworld2008helloworld2008Mon, 13 Aug 2007 10:58:00 GMThttp://www.tkk7.com/zhaochengming/archive/2007/08/13/136481.htmlhttp://www.tkk7.com/zhaochengming/comments/136481.htmlhttp://www.tkk7.com/zhaochengming/archive/2007/08/13/136481.html#Feedback0http://www.tkk7.com/zhaochengming/comments/commentRss/136481.htmlhttp://www.tkk7.com/zhaochengming/services/trackbacks/136481.html<1>IMail 接口

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發(fā)件人(Email地址 發(fā)件人);
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發(fā)件人();
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地址 發(fā)件人;
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發(fā)件人() {
    return this.發(fā)件人;
}

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發(fā)件人(Email地址 發(fā)件人) {
    this.發(fā)件人 = 發(fā)件人;
}

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郵件發(fā)送服務(wù)器
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郵件發(fā)送服務(wù)器 {
protected IMail 郵件;
protected Session session;
protected String send方式;

public Abstract郵件發(fā)送服務(wù)器() {
  
}

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 發(fā)送郵件() {
    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());

     //添加發(fā)件人
     message.setFrom(郵件.get發(fā)件人().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發(fā)送郵件建造者
import javax.mail.Session;


public abstract class Abstract發(fā)送郵件建造者 {
protected Abstract郵件發(fā)送服務(wù)器 mailServer;

public Abstract發(fā)送郵件建造者() {
  
}

public abstract void 建造郵件(IMail mail);

public abstract void 建造郵件session(Session session);

public void 建造發(fā)送郵件() {
    this.mailServer.發(fā)送郵件();
}
}

<5>Smtp郵件發(fā)送服務(wù)器
public class Smtp郵件發(fā)送服務(wù)器 extends Abstract郵件發(fā)送服務(wù)器 {
public Smtp郵件發(fā)送服務(wù)器() {
    this.send方式 = "smtp";
}
}

<6>Smtp發(fā)送郵件建造者
import java.util.Properties;

import javax.mail.Session;


public class Smtp發(fā)送郵件建造者 extends Abstract發(fā)送郵件建造者 {
public Smtp發(fā)送郵件建造者() {
    this.mailServer = new Smtp郵件發(fā)送服務(wù)器();
}

@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>發(fā)送郵件Director
import javax.mail.Session;

public class 發(fā)送郵件Director {
Abstract發(fā)送郵件建造者 build;

public 發(fā)送郵件Director(Abstract發(fā)送郵件建造者 build) {
    this.build = build;
}

public void 發(fā)送郵件建造(IMail mail, Session session) {
    this.build.建造郵件(mail);
    this.build.建造郵件session(session);
    this.build.建造發(fā)送郵件();
}
}

<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發(fā)件人(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發(fā)送郵件建造者 smtp = new Smtp發(fā)送郵件建造者();
    Properties 屬性配置    = new Properties();
    屬性配置.put("mail.smtp.host", "smtp.163.com");
    屬性配置.put("mail.smtp.auth", "true");
    Session session = Session.getInstance(屬性配置,new PasswordAuthenticator("xxxxx", "xxxxx"));
    發(fā)送郵件Director director = new 發(fā)送郵件Director(smtp);
    director.發(fā)送郵件建造(mail, session);
}
}



]]>
模式 簡單工廠模式http://www.tkk7.com/zhaochengming/archive/2007/08/13/136479.htmlhelloworld2008helloworld2008Mon, 13 Aug 2007 10:57:00 GMThttp://www.tkk7.com/zhaochengming/archive/2007/08/13/136479.htmlhttp://www.tkk7.com/zhaochengming/comments/136479.htmlhttp://www.tkk7.com/zhaochengming/archive/2007/08/13/136479.html#Feedback0http://www.tkk7.com/zhaochengming/comments/commentRss/136479.htmlhttp://www.tkk7.com/zhaochengming/services/trackbacks/136479.html簡單工廠模式是類的創(chuàng)建模式,又叫做靜態(tài)工廠方法模式(Static Factory Method)。

簡單工廠模式是由一個工廠對象來決定創(chuàng)建出哪一種產(chǎn)品類的對象。

簡單工廠模式就是由一個工廠類根據(jù)傳入的參數(shù)決定創(chuàng)建出哪一種產(chǎn)品類的對象。

/*******************************************/

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);

}

}



]]>
關(guān)于java抽象工廠模式的一點想法http://www.tkk7.com/zhaochengming/archive/2007/08/13/136480.htmlhelloworld2008helloworld2008Mon, 13 Aug 2007 10:57:00 GMThttp://www.tkk7.com/zhaochengming/archive/2007/08/13/136480.htmlhttp://www.tkk7.com/zhaochengming/comments/136480.htmlhttp://www.tkk7.com/zhaochengming/archive/2007/08/13/136480.html#Feedback0http://www.tkk7.com/zhaochengming/comments/commentRss/136480.htmlhttp://www.tkk7.com/zhaochengming/services/trackbacks/136480.html以下為java抽象工廠模式的一個例子,都是個人的一點想法,有什么不足的地方,懇求大家予以指出,歡迎討論

我們使用的界面上有

文本 {Linux文本,Windows文本}

標(biāo)簽 {Linux標(biāo)簽,Windows標(biāo)簽}

等等.

第一部分 抽象工廠的實現(xiàn)

interface 文本 {}

interface 標(biāo)簽 {}

class Linux文本 implements 文本{

     public String toString() {

         return "Linux文本";

     }

}

class Linux標(biāo)簽 implements 標(biāo)簽{

     public String toString() {

         return "Linux標(biāo)簽";

     }

}

class Windows文本 implements 文本{

     public String toString() {

         return "Windows文本";

     }

}

class Windows標(biāo)簽 implements 標(biāo)簽{

     public String toString() {

         return "Windows標(biāo)簽";

     }

}

interface 組件工廠 {

     public 文本 生產(chǎn)文本組件();

     public 標(biāo)簽 生產(chǎn)標(biāo)簽組件();

}

class Linux組件工廠 implements 組件工廠 {    

     public 文本 生產(chǎn)文本組件() {

         return new Linux文本();

     }

     public 標(biāo)簽 生產(chǎn)標(biāo)簽組件() {

         return new Linux標(biāo)簽();

     }

   

}

class Windows標(biāo)簽組件工廠 implements 組件工廠 {    

     public 文本 生產(chǎn)文本組件() {

         return new Windows文本();

     }

     public 標(biāo)簽 生產(chǎn)標(biāo)簽組件() {

         return new Windows標(biāo)簽();

     }

   

}

class 客戶系統(tǒng)顯示 {

     private 文本 text;

     private 標(biāo)簽 label;

     public static void main(String args[]) {

         客戶系統(tǒng)顯示 clientOS = new 客戶系統(tǒng)顯示();

         組件工廠 factory = new Linux組件工廠();

         clientOS.label = factory.生產(chǎn)標(biāo)簽組件();

         clientOS.text = factory.生產(chǎn)文本組件();    

         System.out.println(clientOS.label);

         System.out.println(clientOS.text);

     }

}

如果按照上面的標(biāo)準(zhǔn),我們要添加一個新的組件 下拉框

A.需要修改的地方有

1.組件工廠

2.Linux組件工廠

3.Windows標(biāo)簽組件工廠

B.需要增加的有

1.interface 下拉框 {}

2.class Linux下拉框 implements 下拉框

3.class Windows下拉框 implements 下拉框

C.調(diào)用的地方也會多出一個factory.生產(chǎn)下拉框組件();    

第二部分 改革抽象工廠

有沒有覺得要改動的地方有點多呢,下面我們來改革一下

1.把 組件工廠中的

生產(chǎn)文本組件();

生產(chǎn)標(biāo)簽組件();

...

都改為

生產(chǎn)組件(組件標(biāo)識);

這樣帶來的好處就是前面提到的,以下的修改就免去了

/**************************/

......

A.需要修改的地方有

1.組件工廠

2.Linux組件工廠

3.Windows標(biāo)簽組件工廠

......

/**************************/

要做到上面的,需要做以下幾件事情

1.增加一個Annotation來說明后面增加的 組件注冊表

@interface 組件描述 {

     Class 組件類();

   

}

2.增加一個Enum

enum 組件注冊表 {

     /**

      * Linux_文本 的對應(yīng)實體類為 Linux文本

      */

     @組件描述(組件類 = Linux文本.class)

     Linux_文本,

   

     @組件描述(組件類 = Linux標(biāo)簽.class)

     Linux_標(biāo)簽,

   

     @組件描述(組件類 = Windows文本.class)

     Windows_文本,

   

     @組件描述(組件類 = Windows標(biāo)簽.class)

     Windows_標(biāo)簽,

}

3.我們不再需要

interface 組件工廠,class Windows標(biāo)簽組件工廠,class Linux組件工廠

我們把 接口 組件工廠改為實體類

為了保持可以擴展和維護

我們定義了一個 接口 工廠

interface 工廠 {

}

class 組件工廠 implements 工廠 {

     public 組件 生產(chǎn)組件(組件注冊表 ID) throws Exception {

         try {

             Field f = 組件注冊表.class.getField(ID.toString());

             組件描述 描述 = f.getAnnotation(組件描述.class);

             Class 組件類 = 描述.組件類();

             return (組件) 組件類.newInstance();

             // 注意,組件類.newInstance();的調(diào)用的時候要確保這個組件類有個不帶參數(shù)的構(gòu)造函數(shù)

             // 如果要使用帶參數(shù)的構(gòu)造函數(shù),可以在@interface 組件描述 中增加一個成員

             // 構(gòu)造函數(shù)[] 構(gòu)造函數(shù)參數(shù)() default{};

             // @interface 構(gòu)造函數(shù) {Class[] 構(gòu)造函數(shù)的參數(shù)();}

             // 通過 組件類.getConstructors(); 來得到這個類的不同構(gòu)造方法

             // 這樣就可以根據(jù)用戶提供的信息用不同的構(gòu)造函數(shù)實例話對象

             // 帶不同的構(gòu)造函數(shù),這里先不討論,后面我會給出代碼

         } catch (Exception e) {

             throw new Exception ("沒有找到對應(yīng)的組件");

         }

     }

}

經(jīng)過上面的修改,代碼如下

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標(biāo)簽.class)

     Linux_標(biāo)簽,

   

     @組件描述(組件類 = Windows文本.class)

     Windows_文本,

   

     @組件描述(組件類 = Windows標(biāo)簽.class)

     Windows_標(biāo)簽,

}

interface 組件 {}

interface 文本 extends 組件 {}

interface 標(biāo)簽 extends 組件 {}

class Linux文本 implements 文本{

     public String toString() {

         return "Linux文本";

     }

}

class Linux標(biāo)簽 implements 標(biāo)簽{

     public String toString() {

         return "Linux標(biāo)簽";

     }

}

class Windows文本 implements 文本{

     public String toString() {

         return "Windows文本";

     }

}

class Windows標(biāo)簽 implements 標(biāo)簽{

     public String toString() {

         return "Windows標(biāo)簽";

     }

}

interface 工廠 {}

class 組件工廠 implements 工廠{

     public 組件 生產(chǎn)組件(組件注冊表 ID) throws Exception {

         try {

             Field f = 組件注冊表.class.getField(ID.toString());

             組件描述 描述 = f.getAnnotation(組件描述.class);

             Class 組件類 = 描述.組件類();

             return (組件) 組件類.newInstance();

         } catch (Exception e) {

             throw new Exception ("沒有找到對應(yīng)的組件");

         }

     }

}

class 客戶系統(tǒng)顯示 {

     private 文本 text;

     private 標(biāo)簽 label;

     public static void main(String args[]) {

         客戶系統(tǒng)顯示 clientOS = new 客戶系統(tǒng)顯示();

         組件工廠 factory = new 組件工廠();

         try {

             clientOS.text = (文本) factory.生產(chǎn)組件(組件注冊表.Linux_文本);

             clientOS.label = (標(biāo)簽) factory.生產(chǎn)組件(組件注冊表.Linux_標(biāo)簽);

         } catch (Exception e) {

             e.printStackTrace();

         }

         System.out.println(clientOS.label);

         System.out.println(clientOS.text);

     }

}

這個時候我們增加一個 下拉框

需要改動的地方

1.增加一個 interface 下拉框 extends 組件 {}

2.增加2個實現(xiàn)類

class Windows下拉框 implements 下拉框{}

class Linux下拉框implements 下拉框{}

3.組件注冊表 增加2個成員

@組件描述(組件類 = Linux下拉框.class)

Linux_下拉框,    

@組件描述(組件類 = Windows下拉框.class)

Windows_下拉框,

和上面的比起來我們只需要在 組件注冊表中增加2個成員,而不需要去修改

1.組件工廠

2.Linux組件工廠

3.Windows標(biāo)簽組件工廠

因為這里要修改3個地方,是不是覺得麻煩,反正我覺得麻煩了點

還有一點就是用戶調(diào)用的時候不需要再使用factory.生產(chǎn)標(biāo)簽組件();等方法,只要一個factory.生產(chǎn)組件就可以了,這樣符合簡單工廠的模式

第三部分 帶參數(shù)的構(gòu)造函數(shù)代碼

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 構(gòu)造函數(shù) {

     /**

      * 構(gòu)造函數(shù)的參數(shù)類型

      * @return

      */

     Class[] 構(gòu)造函數(shù)的參數(shù)();

}

@Retention(RetentionPolicy.RUNTIME)

@interface 組件描述 {

     Class 組件類();

   

     /**

      * 返回組件的構(gòu)造函數(shù) <br>

      * 如果長度為0,則調(diào)用沒有參數(shù)的構(gòu)造函數(shù) <br>

      * @return 構(gòu)造函數(shù)[]

      */

     構(gòu)造函數(shù)[] 構(gòu)造函數(shù)參數(shù)() default{};

   

}

enum 組件注冊表 {

     /**

      * Linux_文本 的對應(yīng)實體類為 Linux文本 <br>

      * Linux的構(gòu)造函數(shù)有 <br>

      * 1. Linux文本(String 顯示的文字) ; <br>

      * 2. Linux文本(String 顯示的文字, Integer 文本字體大小);

      */

     @組件描述(組件類 = Linux文本.class,

             構(gòu)造函數(shù)參數(shù) = {@構(gòu)造函數(shù)(構(gòu)造函數(shù)的參數(shù)={String.class}) ,

         @構(gòu)造函數(shù)(構(gòu)造函數(shù)的參數(shù)={String.class, Integer.class}) } )

     Linux_文本,

   

     @組件描述(組件類 = Linux標(biāo)簽.class)

     Linux_標(biāo)簽,

   

     @組件描述(組件類 = Windows文本.class)

     Windows_文本,

   

     @組件描述(組件類 = Windows標(biāo)簽.class)

     Windows_標(biāo)簽,

}

interface 組件 {}

interface 文本 extends 組件 {}

interface 標(biāo)簽 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 ? "":",文本內(nèi)容為:"+text) + (size == null ? "":",文本字體大小為:"+size);

     }

}

class Linux標(biāo)簽 implements 標(biāo)簽{

     public String toString() {

         return "Linux標(biāo)簽";

     }

}

class Windows文本 implements 文本{

     public String toString() {

         return "Windows文本";

     }

}

class Windows標(biāo)簽 implements 標(biāo)簽{

     public String toString() {

         return "Windows標(biāo)簽";

     }

}

interface 工廠 {}

class 組件工廠 implements 工廠{

     public 組件 生產(chǎn)組件(組件注冊表 ID, Object[] 參數(shù)) throws Exception {

         try {

             Field f = 組件注冊表.class.getField(ID.toString());

             組件描述 描述 = f.getAnnotation(組件描述.class);

             Class 組件類 = 描述.組件類();

             構(gòu)造函數(shù)[] ano = 描述.構(gòu)造函數(shù)參數(shù)();

             if (參數(shù) != null) {

                 for (int i = 0; i < ano.length; i++) {

                     構(gòu)造函數(shù) temp = ano;

                     Class[] 構(gòu)造函數(shù)S = temp.構(gòu)造函數(shù)的參數(shù)();                    

                     if (參數(shù).length == 構(gòu)造函數(shù)S.length) {

                         for (int j = 0; j < 參數(shù).length; j++) {

                             if (參數(shù)[j].getClass().toString().equals(構(gòu)造函數(shù)S[j].toString())) {

                                 if ( j == 參數(shù).length - 1) {

                                     Constructor cons = 組件類.getConstructor(構(gòu)造函數(shù)S);

                                     return (組件) cons.newInstance(參數(shù));

                                 }

                             } else break;

                         }

                     }

                     continue;

                 }

                 throw new Exception ("沒有找到對應(yīng)的組件");

             } else

                 return (組件) 組件類.newInstance();

         } catch (Exception e) {

             e.printStackTrace();

             throw new Exception ("沒有找到對應(yīng)的組件");

         }

     }

}

class 客戶系統(tǒng)顯示 {

     private 文本 text;

     private 標(biāo)簽 label;

     public static void main(String args[]) {

         客戶系統(tǒng)顯示 clientOS = new 客戶系統(tǒng)顯示();

         組件工廠 factory = new 組件工廠();

         try {

             Object [] params = {"初始化文本", new Integer(20)};

             clientOS.text = (文本) factory.生產(chǎn)組件(組件注冊表.Linux_文本,params);

             clientOS.label = (標(biāo)簽) factory.生產(chǎn)組件(組件注冊表.Linux_標(biāo)簽,null);

             System.out.println(clientOS.label);

             System.out.println(clientOS.text);

             Object [] params2 = {"初始化"};

             clientOS.text = (文本) factory.生產(chǎn)組件(組件注冊表.Linux_文本,params2);

             System.out.println(clientOS.text);

         } catch (Exception e) {

             e.printStackTrace();

         }

     }

}



]]>
主站蜘蛛池模板: 中国亚洲女人69内射少妇| 国产老女人精品免费视频| 国产亚洲精品a在线无码| 人人鲁免费播放视频人人香蕉| 免费欧洲美女牲交视频| 亚洲日韩在线中文字幕综合| 亚洲国产成人片在线观看无码| 一级毛片免费全部播放| 国产精品亚洲精品日韩已方| 一区二区三区在线观看免费| 国产精品亚洲二区在线观看| 国产中文字幕在线免费观看| 成人免费无码大片a毛片软件| 67194在线午夜亚洲| 四虎永久在线精品免费网址| 欧美亚洲国产SUV| 国产亚洲精品无码专区| 亚洲国产精品网站在线播放| 国产精品无码素人福利免费| 免费人成视频在线观看免费| 久久精品国产亚洲精品| 无码人妻一区二区三区免费n鬼沢| 亚洲精品成人a在线观看| jizz在线免费观看| 日本久久久久亚洲中字幕| 国产片AV片永久免费观看| 亚洲中文字幕无码永久在线| 国精产品一区一区三区免费视频| 亚洲精品第一国产综合精品 | 成人爽a毛片免费| 亚洲最大成人网色| 最近中文字幕mv手机免费高清 | 亚洲午夜精品一区二区公牛电影院| 免费人成视频在线| h视频免费高清在线观看| 亚洲视频在线观看地址| 无码免费一区二区三区免费播放 | 成在线人免费无码高潮喷水| 亚洲Av无码乱码在线观看性色| 亚洲AV无码国产一区二区三区| 亚洲午夜福利在线观看|