??xml version="1.0" encoding="utf-8" standalone="yes"?>激情视频一区二区三区,亚洲精选久久,色综合成人avhttp://www.aygfsteel.com/Swing/category/23464.html<font size="3"><br><font color="#ff6600">天行? 君子以自Z?lt;/font><br><br></font>zh-cnThu, 21 Jun 2007 19:54:38 GMTThu, 21 Jun 2007 19:54:38 GMT60设计模式Q结构模式之Bridgehttp://www.aygfsteel.com/Swing/articles/121850.htmlSunSunMon, 04 Jun 2007 07:33:00 GMThttp://www.aygfsteel.com/Swing/articles/121850.htmlhttp://www.aygfsteel.com/Swing/comments/121850.htmlhttp://www.aygfsteel.com/Swing/articles/121850.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/121850.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/121850.html功能的类层次QA实现了一些功?要扩展Aq承生成一个a q种Z新增功能而徏立的cdơ称为功能的cd?br>实现的类层次Q抽象类AB  abcd现ABc?q里不是Z新增功能 q种UCؓ实现的类层次
例如,一杯咖啡ؓ?有中杯和大杯之分,同时q有加奶 不加奶之? 如果用单U的l承,q四个具体实?中杯 大杯 加奶 不加?之间有概念重?因ؓ有中杯加?也有中杯不加? 如果再在中杯q一层再实现两个l承,很显然؜?扩展性极?那我们用Bridge模式来实现它. 
如何实现?
以上面提到的咖啡 Z. 我们原来打算只设计一个接?抽象c?,使用Bridge模式?我们需要将抽象和行为分开,加奶和不加奶属于行ؓ,我们它们抽象成一个专门的行ؓ接口.
E序举例Q?br>

先看看抽象部分的接口代码:

public abstract class Coffee
{
  CoffeeImp coffeeImp;

  public void setCoffeeImp() {
    this.CoffeeImp = CoffeeImpSingleton.getTheCoffeImp();
  }

  public CoffeeImp getCoffeeImp() {return this.CoffeeImp;}

  public abstract void pourCoffee();
}

其中CoffeeImp 是加不加奶的行ؓ接口,看其代码如下:

public abstract class CoffeeImp
{
  public abstract void pourCoffeeImp();
}

现在我们有了两个抽象c?span lang=EN-US>,下面我们分别对其q行l承,实现concrete class:

//中杯
public class MediumCoffee extends Coffee
{
  public MediumCoffee() {setCoffeeImp();}

  public void pourCoffee()
  {
    CoffeeImp coffeeImp = this.getCoffeeImp();
    //我们以重复次数来说明是冲中杯q是大杯 ,重复2ơ是中杯
    for (int i = 0; i < 2; i++)
    {

      coffeeImp.pourCoffeeImp();
    }
  
  }
}

//大杯
public class SuperSizeCoffee extends Coffee
{
  public SuperSizeCoffee() {setCoffeeImp();}

  public void pourCoffee()
  {
    CoffeeImp coffeeImp = this.getCoffeeImp();
    //我们以重复次数来说明是冲中杯q是大杯 ,重复5ơ是大杯
    for (int i = 0; i < 5; i++)
    {

      coffeeImp.pourCoffeeImp();
    }
  
  }
}

上面分别是中杯和大杯的具体实?span lang=EN-US>.下面再对行ؓCoffeeImpq行l承:

//加奶
public class MilkCoffeeImp extends CoffeeImp
{
  MilkCoffeeImp() {}

  public void pourCoffeeImp()
  {
    System.out.println("加了味的牛?);
  }
}

//不加?br>public class FragrantCoffeeImp extends CoffeeImp
{
  FragrantCoffeeImp() {}

  public void pourCoffeeImp()
  {
    System.out.println("什么也没加,清香");
  }
}

Bridge模式的基本框架我们已l搭好了,别忘记定义中q有一?动态结?我们现在可以喝到臛_四种咖啡:
1.中杯加奶
2.中杯不加?br>3.大杯加奶
4.大杯不加?o:p>

看看是如何动态结合的,在用之?我们做个准备工作,设计一个单态类(Singleton)用来hold当前的CoffeeImp:

public class CoffeeImpSingleton
{
  private static CoffeeImp coffeeImp;

  public CoffeeImpSingleton(CoffeeImp coffeeImpIn)
   {this.coffeeImp = coffeeImpIn;}

  public static CoffeeImp getTheCoffeeImp()
  {
    return coffeeImp;
  }
}

看看中杯加奶 和大杯加?是怎么出来?span lang=EN-US>:

//拿出牛奶
CoffeeImpSingleton coffeeImpSingleton = new CoffeeImpSingleton(new MilkCoffeeImp());

//中杯加奶
MediumCoffee mediumCoffee = new MediumCoffee();
mediumCoffee.pourCoffee();

//大杯加奶
SuperSizeCoffee superSizeCoffee = new SuperSizeCoffee();
superSizeCoffee.pourCoffee();

注意: Bridge模式的执行类如CoffeeImp和Coffee是一对一的关p? 正确创徏CoffeeImp是该模式的关?



Sun 2007-06-04 15:33 发表评论
]]>
设计模式Q结构模式之Decorator(Ҏ?http://www.aygfsteel.com/Swing/articles/121844.htmlSunSunMon, 04 Jun 2007 07:09:00 GMThttp://www.aygfsteel.com/Swing/articles/121844.htmlhttp://www.aygfsteel.com/Swing/comments/121844.htmlhttp://www.aygfsteel.com/Swing/articles/121844.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/121844.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/121844.htmlDecorator定义:
动态给一个对象添加一些额外的职责,p在墙上刷Ҏ.使用Decorator模式相比用生成子cL式达到功能的扩充昑־更ؓ灉|.

Z么?span lang=EN-US>Decorator?
我们通常可以使用l承来实现功能的拓展,如果q些需要拓展的功能的种cdJ多,那么势必生成很多子类,增加pȝ的复杂?同时,使用l承实现功能拓展,我们必须可预见这些拓展功?q些功能是编译时q定了,是静态的.

使用Decorator的理由是:q些功能需要由用户动态决定加入的方式和时?Decorator提供?x即用"的方?在运行期间决定何时增加何U功?

E序举例Q?br>Display.java:
public abstract class Display {
 public abstract int getColumns(); // 取得横向的字?/p>

 public abstract int getRows(); // 取得直向的行?/p>

 public abstract String getRowText(int row); // 取得Wrow个字?/p>

 public final void show() { // 打印所有內?br>  for (int i = 0; i < getRows(); i++) {
   System.out.println(getRowText(i));
  }
 }
}
Border .java:
public abstract class Border extends Display {
    protected Display display;          // 指装饰外框里面的「內宏V??
    protected Border(Display display) { // 在生对象实例时Q以参数指定「內宏V?br>        this.display = display;
    }
}

StringDisplay .java:
public class StringDisplay extends Display {
 private String string; // 打印的字?

 public StringDisplay(String string) { // 以参数指定打印的字串
  this.string = string;
 }

 public int getColumns() { // 字数
  return string.getBytes().length;
 }

 public int getRows() { // 行数?
  return 1;
 }

 public String getRowText(int row) { // 仅在row?时才q回
  if (row == 0) {
   return string;
  } else {
   return null;
  }
 }
}

SideBorder.java:
public class SideBorder extends Border {
 private char borderChar; // 装饰字符

 public SideBorder(Display display, char ch) { // 以构造子指定Display和装饰字W?
  super(display);
  this.borderChar = ch;
 }

 public int getColumns() { // 字数要再加上內容两边的装饰字W?br>  return 1 + display.getColumns() + 1;
 }

 public int getRows() { // 行数同內容的行数
  return display.getRows();
 }

 public String getRowText(int row) { // 指定该行的內容即为在內容之指定行的两?br>  // 加上装饰字符

  return borderChar + display.getRowText(row) + borderChar;
 }
}

UpDownBorder .java:
public class UpDownBorder extends Border {
 private char borderChar; // 装饰字符

 public UpDownBorder(Display display, char ch) { // 以构造子指定Display和装饰字W?br>  super(display);
  this.borderChar = ch;
 }

 public int getColumns() { // 字数同內容的字数
  return display.getColumns();
 }

 public int getRows() { // 行数要再加上內容上下的装饰字W的行数
  return 1 + display.getRows() + 1;
 }

 public String getRowText(int row) { // 指定该行的內?br>  if (row == 0 || row == getRows() - 1) {
   return makeLine(borderChar, getColumns());
  } else {
   return display.getRowText(row - 1);
  }
 }

 private String makeLine(char ch, int count) { // 以字WchQ徏立重复countơ的q箋字串
  StringBuffer buf = new StringBuffer();
  for (int i = 0; i < count; i++) {
   buf.append(ch);
  }
  return buf.toString();
 }
}


Main.java:
public class Main {
    public static void main(String[] args) {
        Display b1 = new StringDisplay("Hello, world.");
        Display b2 = new UpDownBorder(b1, '-');
        Display b3 = new SideBorder(b2, '*');
        b1.show();
        b2.show();
        b3.show();
        Display b4 =
                    new FullBorder(
                        new UpDownBorder(
                            new SideBorder(
                                new UpDownBorder(
                                    new SideBorder(
                                        new StringDisplay("您好?),
                                        '*'
                                    ),
                                    '='
                                ),
                                '|'
                            ),
                            '/'
                        )
                    );
        b4.show();
    }
}

注意Qdisplayl承cM的getRowTextҎ
q个Ҏ把所有的用法都集成C?

Sun 2007-06-04 15:09 发表评论
]]>
设计模式Q结构模式之Composite(l合)http://www.aygfsteel.com/Swing/articles/121789.htmlSunSunMon, 04 Jun 2007 03:41:00 GMThttp://www.aygfsteel.com/Swing/articles/121789.htmlhttp://www.aygfsteel.com/Swing/comments/121789.htmlhttp://www.aygfsteel.com/Swing/articles/121789.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/121789.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/121789.htmlComposite定义:
对象以树Şl构l织h,以达?#8220;部分Q整?#8221; 的层ơ结构,使得客户端对单个对象和组合对象的使用h一致?

使用Composite
首先定义一个接口或抽象c,q是设计模式通用方式了,其他设计模式Ҏ口内部定义限制不多,Composite却有个规定,那就是要在接口内部定义一个用于访问和理Compositel合体的对象们(或称部gComponentQ?

下面的代码是以抽象类定义Q一般尽量用接口interface,

public abstract class Equipment
{
  private String name;
  //|络h
  public abstract double netPrice();
  //折扣h
  public abstract double discountPrice();
  //增加部gҎ  
  public boolean add(Equipment equipment) { return false; }
  //删除部gҎ
  public boolean remove(Equipment equipment) { return false; }
  //注意q里Q这里就提供一U用于访问组合体cȝ部gҎ?br>  public Iterator iter() { return null; }
  
  public Equipment(final String name) { this.name=name; }
}

抽象c?span>Equipment是Component定义Q代表着l合体类的对象们,Equipment中定义几个共同的Ҏ?/span>

public class Disk extends Equipment
{
  public Disk(String name) { super(name); }
  //定义Disk|络h?
  public double netPrice() { return 1.; }
  //定义了disk折扣h?.5 Ҏ?br>  public double discountPrice() { return .5; }
}

Disk是组合体内的一个对象,或称一个部Ӟq个部g是个单独元素( Primitive)?br>q有一U可能是Q一个部件也是一个组合体Q就是说q个部g下面q有'儿子'Q这是树形结构中通常的情况,应该比较Ҏ理解。现在我们先要定义这个组合体Q?/span>

abstract class CompositeEquipment extends Equipment
{
  private int i=0;
  //定义一个Vector 用来存放'儿子'
  private Lsit equipment=new ArrayList();

  public CompositeEquipment(String name) { super(name); }

  public boolean add(Equipment equipment) {
     this.equipment.add(equipment);
     return true;
   }

  public double netPrice()
  {
    double netPrice=0.;
    Iterator iter=equipment.iterator();
    for(iter.hasNext())
      netPrice+=((Equipment)iter.next()).netPrice();
    return netPrice;
  }

  public double discountPrice()
  {
    double discountPrice=0.;
    Iterator iter=equipment.iterator();
    for(iter.hasNext())
      discountPrice+=((Equipment)iter.next()).discountPrice();
    return discountPrice;
  }
  

  //注意q里Q这里就提供用于讉K自己l合体内的部件方法?br>  //上面dIsk 之所以没有,是因为Disk是个单独(Primitive)的元?
  public Iterator iter()
  {
    return equipment.iterator() ;
  {
  //重蝲IteratorҎ
   public boolean hasNext() { return i<equipment.size(); }
  //重蝲IteratorҎ
   public Object next()
   {
    if(hasNext())
       return equipment.elementAt(i++);
    else
        throw new NoSuchElementException();
   }
  

}

上面CompositeEquipmentl承了Equipment,同时己里面的对象们提供了外部讉K的方?重蝲了Iterator,Iterator是Java的Collection的一个接口,是Iterator模式的实?

我们再看?span>CompositeEquipment的两个具体类:盘盒Chassis和箱子CabinetQ箱子里面可以放很多东西Q如底板Q电源盒Q硬盘盒{;盘盒里面可以放一些小讑֤Q如盘 软驱{。无疑这两个都是属于l合体性质的?/span>

public class Chassis extends CompositeEquipment
{
   public Chassis(String name) { super(name); }
   public double netPrice() { return 1.+super.netPrice(); }
   public double discountPrice() { return .5+super.discountPrice(); }
}

public class Cabinet extends CompositeEquipment
{
   public Cabinet(String name) { super(name); }
   public double netPrice() { return 1.+super.netPrice(); }
   public double discountPrice() { return .5+super.discountPrice(); }
}

x我们完成了整?span>Composite模式的架构?/span>

我们可以看看客户端调?span>Composote代码:

Cabinet cabinet=new Cabinet("Tower");

Chassis chassis=new Chassis("PC Chassis");
//PC Chassis装到Tower?(盘盒装到箱子里)
cabinet.add(chassis);
//一?0GB的硬盘装?PC Chassis (硬盘装到盘盒里)
chassis.add(new Disk("10 GB"));

//调用 netPrice()Ҏ;
System.out.println("netPrice="+cabinet.netPrice());
System.out.println("discountPrice="+cabinet.discountPrice());

上面调用的方?span>netPrice()或discountPrice()Q实际上Composite使用Iterator遍历了整个树形结?L同样包含q个Ҏ的对象ƈ实现调用执行.

Composite是个很y妙体现智慧的模式Q在实际应用中,如果到树Şl构Q我们就可以试是否可以使用q个模式?/span>



Sun 2007-06-04 11:41 发表评论
]]>
设计模式Q结构模式之Proxy(代理)http://www.aygfsteel.com/Swing/articles/121775.htmlSunSunMon, 04 Jun 2007 02:57:00 GMThttp://www.aygfsteel.com/Swing/articles/121775.htmlhttp://www.aygfsteel.com/Swing/comments/121775.htmlhttp://www.aygfsteel.com/Swing/articles/121775.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/121775.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/121775.htmlProxy是比较有用途的一U模?而且变种较多,应用场合覆盖从小l构到整个系l的大结?Proxy是代理的意?我们也许有代理服务器{概?代理概念可以解释?在出发点到目的地之间有一道中间层,意ؓ代理.

设计模式中定?/strong>: 为其他对象提供一U代理以控制对这个对象的讉K.要用再徏?br>E序举例1Q权限访?br>

public class ForumProxy implements Forum {

private ForumPermissions permissions;
private Forum forum;
this.authorization = authorization;

public ForumProxy(Forum forum, Authorization authorization,
ForumPermissions permissions)
{
this.forum = forum;
this.authorization = authorization;
this.permissions = permissions;
}

.....

public void setName(String name) throws UnauthorizedException,
ForumAlreadyExistsException
{
  //只有是系l或论坛理者才可以修改名称
  if (permissions.isSystemOrForumAdmin()) {
    forum.setName(name);
  }
  else {
    throw new UnauthorizedException();
  }
}

...

}

?span lang=EN-US>DbForum才是接口Forum的真正实?以修改论坛名UCؓ?

public class DbForum implements Forum, Cacheable {
...

public void setName(String name) throws ForumAlreadyExistsException {

  ....

  this.name = name;
  //q里真正新名称保存到数据库?
  saveToDb();

  ....
}


...

}


凡是涉及到对论坛名称修改q一事g,其他E序都首先得和ForumProxy打交?由ForumProxy军_是否有权限做某一样事?ForumProxy是个名副其实?|关","安全代理pȝ".
E序举例2Q用到才创徏
实际执行打印 Z模仿长时间操?在构建的时候gq?S
public class Printer implements Printable {
 private String name;

 public Printer() {
  heavyJob("正在产生Printer的对象实?);
 }

 public Printer(String name) { // 构造子
  this.name = name;
  heavyJob("正在产生Printer的对象实?" + name + ")");
 }

 public void setPrinterName(String name) { // 命名
  this.name = name;
 }

 public String getPrinterName() { // 取得名称
  return name;
 }

 public void print(String string) { // 输出名称
  System.out.println("=== " + name + " ===");
  System.out.println(string);
 }

 private void heavyJob(String msg) { // 较重的工作(假设Q?br>  System.out.print(msg);
  for (int i = 0; i < 5; i++) {
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
   }
   System.out.print(".");
  }
  System.out.println("完成?);
 }
}
代理c:真正要打C才去调用生成printerq且参数赋D行打印操?br>public class PrinterProxy implements Printable {
 private String name; // 名称

 private Printable real; // 「本人?/span>

 private String className; // 「本人」的cdU?/span>

 public PrinterProxy(String name, String className) { // 构造子
  this.name = name;
  this.className = className;
 }

 public synchronized void setPrinterName(String name) { // 命名
  if (real != null) {
   real.setPrinterName(name); //「本人」也要命?br>  }
  this.name = name;
 }

 public String getPrinterName() { // 取得名称
  return name;
 }

 public void print(String string) { // 输出到画面上
  realize();
  real.print(string);
 }

 private synchronized void realize() { // 产生「本人?br>  if (real == null) {
   try {
    real = (Printable) Class.forName("Proxy." + className)
      .newInstance();
    real.setPrinterName(name);
   } catch (ClassNotFoundException e) {
    System.err.println("找不到类 " + className + "?);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}
无论怎么调用setPrinterNameҎ都不会生real实例 只有真正需要生成本人的时候才会生?br>试ҎQ?br>public class Main {
 public static void main(String[] args) {
  Printable p = new PrinterProxy("Alice", "Printer");
  System.out.println("现在的名U是" + p.getPrinterName() + "?);
  p.setPrinterName("Bob");
//不会生成Printer
  System.out.println("现在的名U是" + p.getPrinterName() + "?);
//不会生成Printer
  p.print("Hello, world.");
//生成Printer
 }
}



Sun 2007-06-04 10:57 发表评论
]]>
设计模式Q创建模式之Singleton(单?http://www.aygfsteel.com/Swing/articles/120814.htmlSunSunWed, 30 May 2007 02:11:00 GMThttp://www.aygfsteel.com/Swing/articles/120814.htmlhttp://www.aygfsteel.com/Swing/comments/120814.htmlhttp://www.aygfsteel.com/Swing/articles/120814.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/120814.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/120814.htmlSingleton模式主要作用是保证在Java应用E序中,一个类Class只有一个实例存在?br>一般Singleton模式通常有几UŞ?

public class Singleton {

  private Singleton(){}

  //在自己内部定义自׃个实例,是不是很奇怪?
  //注意q是private 只供内部调用

  private static Singleton instance = new Singleton();

  //q里提供了一个供外部讉K本class的静态方法,可以直接讉K  
  public static Singleton getInstance() {
    return instance;   
   }
//当程序第一ơ调用getInstance的时候初始化Singleton c?同时初始化static字段 产生唯一的对象实?br>}

 

W二UŞ?span>:

public class Singleton {

  private static Singleton instance = null;

  public static synchronized Singleton getInstance() {

  //q个Ҏ比上面有所改进Q不用每ơ都q行生成对象Q只是第一ơ     
  //使用时生成实例,提高了效率!
  if (instance==null)
    instanceQnew Singleton();
  return instance;   }

}

 

使用Singleton.getInstance()可以讉K单态类?/span>

上面W二中Ş式是lazy initializationQ也是说第一ơ调用时初始SingletonQ以后就不用再生成了?/span>

注意?span>lazy initialization形式中的synchronizedQ这个synchronized很重要,如果没有synchronizedQ那么用getInstance()是有可能得到多个Singleton实例?/span>

一般认为第一UŞ式要更加安全些?/p>

Sun 2007-05-30 10:11 发表评论
]]>
设计模式Q创建模式之Builderhttp://www.aygfsteel.com/Swing/articles/120804.htmlSunSunWed, 30 May 2007 01:54:00 GMThttp://www.aygfsteel.com/Swing/articles/120804.htmlhttp://www.aygfsteel.com/Swing/comments/120804.htmlhttp://www.aygfsteel.com/Swing/articles/120804.html#Feedback1http://www.aygfsteel.com/Swing/comments/commentRss/120804.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/120804.htmlBuilder模式定义:
一个复杂对象的构徏与它的表C分?使得同样的构E可以创Z同的表示.

Builder模式是一步一步创Z个复杂的对象,它允许用户可以只通过指定复杂对象的类型和内容可以构建它?用户不知道内部的具体构徏l节.Builder模式是非常类似抽象工厂模?l微的区别大概只有在反复使用中才能体会到.


Z构建复杂对象的q程和它?strong>部g
解?注意: 是解?strong>q程?strong>部g.

因ؓ一个复杂的对象,不但有很多大量组成部?如汽?有很多部?车轮 方向?发动有各U小零g{等,部g很多,但远不止q些,如何这些部件装配成一辆汽?q个装配q程也很复杂(需要很好的l装技?,Builder模式是Z部件和l装q程分开.

首先,需要一个接?它定义如何创建复杂对象的各个部g:

public interface Builder {

  //创徏部gA  比如创徏汽R车轮
  void buildPartA();
  //创徏部gB 比如创徏汽R方向?br>  void buildPartB();
  //创徏部gC 比如创徏汽R发动?br>  void buildPartC();

  //q回最后组装成品结?(q回最后装配好的汽?
  //成品的组装过E不在这里进?而是转移C面的DirectorcMq行.
  //从而实C解?strong>q程?strong>部g
  Product getResult();

}

?span lang=EN-US>Director构徏最后的复杂对象,而在上面Builder接口中封装的是如何创Z个个部g(复杂对象是由q些部gl成?,也就是说Director的内Ҏ如何部件最后组装成成品:

public class Director {

  private Builder builder;

  public Director( Builder builder ) {
    this.builder = builder;
  }
  // 部件partA partB partC最后组成复杂对?br>  //q里是将车轮 方向盘和发动机组装成汽R的过E?br>  public void construct() {
    builder.buildPartA();
    builder.buildPartB();
    builder.buildPartC();

  }

}

Builder的具体实现ConcreteBuilder:
通过具体完成接口Builder来构建或装配产品的部?
定义q明它所要创建的是什么具体东?
提供一个可以重新获取品的接口:

public class ConcreteBuilder implements Builder {

  Part partA, partB, partC;
  public void buildPartA() {
    //q里是具体如何构建partA的代?br>
  };
  public void buildPartB() {
    //q里是具体如何构建partB的代?br>  };
   public void buildPartC() {
    //q里是具体如何构建partB的代?br>  };
   public Product getResult() {
    //q回最后组装成品结?br>  };

}


ConcreteBuilder builder = new ConcreteBuilder();
Director director = new Director( builder );

director.construct();
Product product = builder.getResult();

buildercdQ?br>


Sun 2007-05-30 09:54 发表评论
]]>设计模式Q创建模式之Prototype(原型)http://www.aygfsteel.com/Swing/articles/120699.htmlSunSunTue, 29 May 2007 07:43:00 GMThttp://www.aygfsteel.com/Swing/articles/120699.htmlhttp://www.aygfsteel.com/Swing/comments/120699.htmlhttp://www.aygfsteel.com/Swing/articles/120699.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/120699.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/120699.html复制产生对象实例Q?br>使用Prototype模式可以理解?nbsp; 创造出一个和已有对象一L对象
ex)指着面包店橱H里的面包告诉老板 我就要这?虽然不知道名?也不知道做法 但是能买到和所指的相同的东ѝ?br>实例Q?br>1、定义一个接?实现Cloneable
public interface Product extends Cloneable {
    public abstract void use(String s);
    public abstract Product createClone();
}
2、声明一个managecLҎProduct的createClone来进行复?br>public class Manager {
       private Hashtable showcase = new Hashtable();
    public void register(String name, Product proto) {
        showcase.put(name, proto);
    }
    public Product create(String protoname) {
        Product p = (Product)showcase.get(protoname);
        return p.createClone();
    }
}
3、Productcȝ一个具体实?br>public class UnderlinePen implements Product {
    private char ulchar;
    public UnderlinePen(char ulchar) {
        this.ulchar = ulchar;
    }
    public void use(String s) {
        int length = s.getBytes().length;
        System.out.println("\""  + s + "\"");
        System.out.print(" ");
        for (int i = 0; i < length; i++) {
            System.out.print(ulchar);
        }
        System.out.println("");
    }
    public Product createClone() {
        Product p = null;
        try {
            p = (Product)clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return p;
    }
}
4、具体?br>     // 预备阶段
        Manager manager = new Manager();
        UnderlinePen upen = new UnderlinePen('~');
        MessageBox mbox = new MessageBox('*');
        MessageBox sbox = new MessageBox('/');
        manager.register("strong message", upen);
        manager.register("warning box", mbox);
        manager.register("slash box", sbox);

        // 实现产生
        Product p1 = manager.create("strong message");
        p1.use("Hello, world.");
        Product p2 = manager.create("warning box");
        p2.use("Hello, world.");
        Product p3 = manager.create("slash box");
        p3.use("Hello, world.");
    }

也可以将product声明成抽象类实现Cloneable接口
q且实现createCloneҎ
q样子类中就不用再声明creatCloneҎ?化了代码

cloneҎ在Object中定?因此所有类都会l承clone()Ҏ
Cloneableq个接口表示 可用clone()Ҏq行复制
clone()Ҏ做的是浅拯 所做的操作是直接复制字D内?q不该字段对应的对象实例内?假定有一个数l?当用cloneҎq行拯以后 复制的结果,只是对应到该数组的参?x向该数组的内存地址 如果惛_深拷?必须重写cloneҎ 记得要加上super.clone()



Sun 2007-05-29 15:43 发表评论
]]>
设计模式Q创建对象之Factoryhttp://www.aygfsteel.com/Swing/articles/120690.htmlSunSunTue, 29 May 2007 07:12:00 GMThttp://www.aygfsteel.com/Swing/articles/120690.htmlhttp://www.aygfsteel.com/Swing/comments/120690.htmlhttp://www.aygfsteel.com/Swing/articles/120690.html#Feedback0http://www.aygfsteel.com/Swing/comments/commentRss/120690.htmlhttp://www.aygfsteel.com/Swing/services/trackbacks/120690.html1、引入Factory  Pattern的目的是Z装cȝ实例化操?br>

public class Factory{

  public static Sample creator(int which){
         
//getClass 产生Sample 一般可使用动态类装蝲装入cR?br>  if (which==1)
    return new SampleA();
  else if (which==2)
    return new SampleB();

  }

}

那么在你的程序中,如果要实例化Sample?׃?o:p>

Sample sampleA=Factory.creator(1);

q样,在整个就不涉及到Sample的具体子c?辑ֈ装效果,也就减少错误修改的机?q个原理可以用很通俗的话来比?是具体事情做得多,容易范错误

2、Factory Pattern在父c规定对象的创徏ҎQ但不深入到具体的类名,所有具体的实现都放在了子类Q大致可以分Z生对象实例的大纲(框架)和实际生对象实例的cM斚w

实例Q?br>
framework中的Factory是实现creat的抽象类
 public final Product creat(String owner) {
  Product p = creadProduct(owner);
  registerProduct(p);
  return p;
 }

 public abstract Product creadProduct(String owner);
famework中的Product是仅实现useҎ的抽象类

具体的生成和使用Ҏ都用idcard中的cd体实?br> IDcardFactoryQ?br> IDcardFactory extends Factory 
 public synchronized Product creadProduct(String owner) {
  IDcard ic = new IDcard(owner);
  return ic;
 }

IDcarrdQ?br>class IDcard extends Product
IDcard(String owner) {
  System.out.println("建立" + owner +"的卡");
  this.owner = owner;
 }
此处构造函C是public 只有通过同一个包中的factory才可以生这个对象实?/p>

 public void use() {
  System.out.println("使用"  + owner + "的卡");
 }

具体使用Q?br> public static void main(String[] args) {
  Factory fc = new IDcardFactory();
  Product p1 = fc.creat("card1");
  Product p2 = fc.creat("card2");
  Product p3 = fc.creat("card3");
  p1.use();
  p2.use();
  p3.use();
 }
q样不必修改framework包中的内容就能够创徏Z同的产品和工?/p>

Sun 2007-05-29 15:12 发表评论
]]>
վ֩ģ壺 Զ| DZɽ| ¡| | | | | ˮ| ұ| | | ʲ| | Ʊ| | ƽ| | ͬ| | ¡| ˮ| | | ɽ| | | ̫| | ʡ| | Ӫ| | | ˮ| | | | | | | Ρɽ|