首页  »   编程

生成器方式

网友分享于:2013-11-19  浏览:2次
生成器模式

它是一种对象构建模式,它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。

 

(1)创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式;
(2)构造过程必须允许被构造的对象有不同的表示。

 

 

package org.fanzone.pattern.builder;


/** 

 * Title: base<br> 

 * Description: Final Product<br> 

 * Copyright: Copyright (c) 2011 <br> 

 * Create DateTime: Jul 6, 2011 8:10:55 PM <br> 

 * @author wangmeng

 */

public class Person {


@SuppressWarnings("unused")

private String looks;

@SuppressWarnings("unused")

private String figure;

@SuppressWarnings("unused")

private String sex;

public void setLooks(String looks){

this.looks  = looks;

}

public void setFigure(String figure){

this.figure = figure;

}

public void setSex(String sex){

this.sex = sex;

}

public String getLooks(){

return this.looks;

}

public String getFigure(){

return this.figure;

}

public String getSex(){

return this.sex;

}

}


---------------------------------

package org.mars.pattern.builder;

/** 
 * Title: base<br> 
 * Description: Abstract Builder<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jul 6, 2011 8:28:07 PM <br> 
 * @author wangmeng
 */
public abstract class PersonBuilder {
protected Person person;
public Person getPerson(){
return this.person;
}
public void buildPerson(){
person = new Person();
}
public abstract void buildLooks();
public abstract void buildFigure();
public abstract void buildSex();
}

-------------------------------------------------------

package org.mars.pattern.builder;

/** 
 * Title: base<br> 
 * Description: Concrete Builder<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jul 6, 2011 8:43:04 PM <br> 
 * @author wangmeng
 */
public class FrogPrinceBuilder extends PersonBuilder{

@Override
public void buildFigure() {
this.person.setFigure("strong");
}

@Override
public void buildLooks() {
this.person.setLooks("handsome");
}

@Override
public void buildSex() {
this.person.setSex("male");
}

}

------------------------------------------------------

package org.mars.pattern.builder;

/** 
 * Title: base<br> 
 * Description: Concrete Builder<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jul 6, 2011 8:42:04 PM <br> 
 * @author wangmeng
 */
public class SnowWhiteBuilder extends PersonBuilder{

@Override
public void buildFigure() {
this.person.setFigure("slim");
}

@Override
public void buildLooks() {
this.person.setLooks("pretty");
}

@Override
public void buildSex() {
this.person.setSex("female");
}

}

---------------------

package org.mars.pattern.builder;

/** 
 * Title: base<br> 
 * Description: Director<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jul 6, 2011 8:54:02 PM <br> 
 * @author wangmeng
 */
public class NuWa {

private PersonBuilder personBuilder;
public void setPersonBuilder(PersonBuilder personBuilder){
this.personBuilder = personBuilder;
}
public Person getPerson(){
return personBuilder.getPerson();
}
public void constructPerson(){
personBuilder.buildPerson();
personBuilder.buildLooks();
personBuilder.buildFigure();
personBuilder.buildSex();
}
}

---------------------

package org.mars.pattern.builder;

/** 
 * Title: base<br> 
 * Description: Just for a test.<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jul 6, 2011 8:58:53 PM <br> 
 * @author wangmeng
 */
public class Commission {

public static void main(String[] args){
NuWa nuWa = new NuWa();
FrogPrinceBuilder frogPrinceBuilder = new FrogPrinceBuilder();
SnowWhiteBuilder snowWhiteBuilder = new SnowWhiteBuilder();
/**
* Build the specified product.
*/
nuWa.setPersonBuilder(frogPrinceBuilder);
nuWa.constructPerson();
Person person = nuWa.getPerson();
System.out.println("Frog Prince: I am a " + person.getSex() + " who looks " + person.getLooks() + " and " + person.getFigure() + ".");

nuWa.setPersonBuilder(snowWhiteBuilder);
nuWa.constructPerson();
person = nuWa.getPerson();
System.out.println("Snow White: I am a " + person.getSex() + " who looks " + person.getLooks() + " and " + person.getFigure() + ".");
}
}

 

 

相关解决方案

最新解决方案