Android学习笔记--设计模式之建造者
建立一个javaBean用来存放数据信息
1 package chouxianglianxi; 2 3 public class DuanXinBao { 4 private int qian; 5 private int shuliang; 6 public int getQian() { 7 return qian; 8 } 9 public void setQian(int qian) { 10 this.qian = qian; 11 } 12 public int getShuliang() { 13 return shuliang; 14 } 15 public void setShuliang(int shuliang) { 16 this.shuliang = shuliang; 17 } 18 19 20 }
建立一个接口
1 package chouxianglianxi; 2 3 public interface IJieKou { 4 //为金额赋值 5 void booqian(); 6 //为数量赋值 7 void booshuliang(); 8 //返回有数据的类 9 DuanXinBao dduanxin(); 10 }
建立类继承接口
1 package chouxianglianxi; 2 3 public class ShiXian implements IJieKou { 4 private DuanXinBao duanxinbao; 5 6 public ShiXian(){ 7 //new出来实例 8 duanxinbao = new DuanXinBao(); 9 } 10 @Override 11 public void booqian() { 12 //赋值 13 duanxinbao.setQian(100); 14 } 15 16 @Override 17 public void booshuliang() { 18 //赋值 19 duanxinbao.setShuliang(5000); 20 } 21 22 @Override 23 public DuanXinBao dduanxin() { 24 //返回出去 25 return duanxinbao; 26 } 27 28 }
建立一个文件
1 package chouxianglianxi; 2 3 public class MessageDLod { 4 //返回类型为Bean 5 public DuanXinBao getduanxin(IJieKou ijiekou){ 6 // 7 if(ijiekou != null){ 8 //执行赋值的方法 9 ijiekou.booqian(); 10 ijiekou.booshuliang(); 11 //赋值完毕把它返回出去 12 return ijiekou.dduanxin(); 13 } 14 return null; 15 } 16 }
客户端文件
1 package chouxianglianxi; 2 3 public class Client { 4 public static void main(String[] args) { 5 //里面有数量和金额的文件 6 IJieKou is = new ShiXian(); 7 //构造者 8 MessageDLod messag = new MessageDLod(); 9 //拿到文件为它赋值 10 DuanXinBao duanxinbao = messag.getduanxin(is); 11 //读取 12 System.out.println(duanxinbao.getQian()); 13 System.out.println(duanxinbao.getShuliang()); 14 } 15 }
能看到的代码不是自己的,自己写出来的才是自己的