多个构造器参数处理方法_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 多个构造器参数处理方法

多个构造器参数处理方法

 2013/8/27 0:58:16  wuhaocn  程序员俱乐部  我要评论(0)
  • 摘要:1.重叠构造器模式publicclassUserTC{/***重叠构造器模式*对于参数不算太多事可以使用,用户用不到的属性设置为默认值*一句话重叠构造器可行,但是当参数过多时客户端代码难以编写,并且仍难难以阅读(引用effectivejava2.2)*/privateStringname;privateStringuser;privateStringpass;privatecharsex;privateintage;privateStringemail;privateStringaddr
  • 标签:方法 构造 处理方法 构造器
1.重叠构造器模式
class="java" name="code">

public class UserTC {
/**
 * 重叠构造器模式
 * 对于参数不算太多事可以使用,用户用不到的属性设置 为默认值
 * 一句话重叠构造器可行,但是当参数过多时客户端代码难以编写,并且仍难难以阅读(引用effective java2.2)
 */
	
	private String name;
	private String user;
	private String pass;
	private char   sex;
	private int    age;
	private String email;
	private String addr;
	private String mes;
	
	/**
	 * 通过this方法逐步调用下层构造器
	 * */
	public UserTC(String name) {
		this(name, "");
	}
	public UserTC(String name, String user){
		this(name, user, "");
	}
	public UserTC(String name, String user, String pass){
		this(name, user, pass, 'F');
	}
	public UserTC(String name, String user, String pass, char sex){
		this(name, user, pass, sex, 0);
	}
	public UserTC(String name, String user, String pass, char sex, int age){
		this(name, user, pass, sex, age, "");
	}
	public UserTC(String name, String user, String pass, char sex, int age, String email){
		this(name, user, pass, sex, age, email, "");
	}
	public UserTC(String name, String user, String pass, char sex, int age, String email, String addr){
		this(name, user, pass, sex, age, email, addr, "");
	}
	
    public UserTC(String name, String user, String pass, char sex, int age, String email, String addr, String mes) {
		this.name = name;
		this.user = user;
		this.pass = pass;
		this.sex  = sex;
		this.age  = age;
		this.email = email;
		this.addr = addr;
		this.mes  = mes;
	}
}

2.javabeans模式
  public class UserBean {
	/**
	 * javabeans模式
	 * 优点:克服了重叠构造器模式的缺点,创建类的实例比较容易,代码也易于阅读
	 * 缺点:构造过程通过调用方法实现,可能造成javabean处于不一致状态,调试比较困难,线程安全要付出较大的工作量
	 * 但是此模式在构造数据库映射类时用的较为广泛。 
	 * */
	private String name;
	private String user;
	private String pass;
	private char   sex;
	private int    age;
	private String email;
	private String addr;
	private String mes;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public char getSex() {
		return sex;
	}
	public void setSex(char sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	public String getMes() {
		return mes;
	}
	public void setMes(String mes) {
		this.mes = mes;
	}
	
}



3.Builder模式(构建器)
public class UserBuilder {
	/**
	 * 拥有了javabeans模式的易读性,而且拥有重叠构造器模式安全性,而且使用起来十分灵活
	 * 但其在性能较之前两种方法有点不足,总之此方法还是比较不错的
	 */
	private final String name;
	private final String user;
	private final String pass;
	private final char   sex;
	private final int    age;
	private final String email;
	private final String addr;
	private final String mes;
	public static class Builder{
		private String name;
		private String user;
		private String pass;
		private char   sex = 'f';
		private int    age = 0;
		private String email = "";
		private String addr = "";
		private String mes = "";
		public Builder(String name){
			this.name = name;
		}
		public Builder user(String value){
			user = value;
			return this;
		}
		public Builder pass(String value){
			pass = value;
			return this;
		}
		public Builder sex(char value){
			sex = value;
			return this;
		}
		public Builder age(int value){
			age = value;
			return this;
		}
		public Builder email(String value){
			email = value;
			return this;
		}
		public Builder addr(String value){
			addr = value;
			return this;
		}
		public Builder mes(String value){
			mes = value;
			return this;
		}
		
		public UserBuilder builder(){
			return new UserBuilder(this);
		}
		
    }
	private UserBuilder(Builder builder){
		this.name = builder.name;
		this.user = builder.user;
		this.pass = builder.pass;
		this.sex = builder.sex;
		this.age = builder.age;
		this.email = builder.email;
		this.addr = builder.addr;
		this.mes = builder.mes;	
	}
}



4.三种方法的调用
 
   public class Main {
	 /**
	 * 多参数类的使用
	 * @param args
	 */
    public static void main(String args[]){
    	testUserBuilder();
    	testUserBean();
    	testUserTC();
    }
    public static void testUserBuilder(){
    	UserBuilder userBuilder = new UserBuilder.Builder("jack").user("jack").
    			                  pass("jack").sex('f').age(20).addr("china").
    			                  email("jack@126.com").mes("java").builder();
    	System.out.println(userBuilder);
    }
    public static void testUserBean(){
    	UserBean userBean = new UserBean();
    	userBean.setAddr("china");
    	userBean.setAge(20);
    	userBean.setEmail("jack@126.com");
    	userBean.setMes("java");
    	userBean.setName("jack");
    	userBean.setPass("jack");
    	userBean.setSex('f');
    	userBean.setUser("jack");
    	System.out.println(userBean);
    }
    public static void testUserTC(){
    	UserTC userTC = new UserTC("jack");
    	System.out.println(userTC);
    }
  



  总的来说builder拥有javabean和重叠构造器的优点但在性能上有些缺陷
  另外javabean模式在处理数据库映射时也是不可或缺的
  详见effectivejava(第二版)
上一篇: javaweb上传文件(sevlet) 下一篇: 没有下一篇了!
发表评论
用户名: 匿名