一、学习目的
代理模式是Java常见的设计模式之一。所谓代理模式是指客户端并不直接调用实际的对象,而是通过调用代理,来间接的调用实际的对象。
二、学习内容
(1)字符串
String类
1,String创建字符串的方法
//第一种
String str1 = "abc";
String是系统提供的类,因此可以使用构造方法创建一个字符串,但没有意义,
//第二种
String str3 = new String(); //无意义
2, 字符串的比较:
-
“==”比较两个对象是否相同,即比较地址是否相同
-
equals方法比较两个字符串的内容是否相同
String str1 = "abc"; String str2 = "abc"; System.out.println(str1 == str2); System.out.println(str1.equals(str2));

输出结果均为true,原因是str1与str2都是指向“abc”,所以内存地址相同
3,String创建的字符串是不可变的 一旦创建内容不可改变
4,使用字节数组byte创建一个字符串
//使用字节数组 创建一个字符串
byte[] name = {'x','w','j'};
String str4 = new String(name);
System.out.println(str4);
byte[] name2 = {97,98,99};
String str5 = new String(name2); //ASCII码也可转化成字符串
5,提取字节输出的一部分创建字符串
//使用字节数组的一部分 创建一个新的字符串
String str6 = new String(name,0,2); //
System.out.println(str6);
6,字符数组转化成字符串,char占两个字节
char[] hello = {'你','好','啊'};
String h = new String(hello);
System.out.println(h);
7,获取字符串的第一个字符用 charAt方法
char c = str4.charAt(0); //零就是第一个字符
8,两个字符串的比较 :可以知道大小关系 返回值 为0 则相同 ,大于零就是表示大于 ,小于零就是表示小于
compareTo按字典顺序比较 ,区分大小写
compareToIgnoreCase忽略大小写比较
int result = str4.compareTo(str5);
System.out.println(result);
9,将两个字符串连接起来,用concat方法
String nStr = str5.concat(str7);
System.out.println(nStr);
10,判断一个字符串是否包含另外一个字符串,用contains方法
// contains
boolean b = "hello".contains("ll");
System.out.println(b);
11,判断是否以某个字符串开头或结尾,用endsWith和startsWith方法
String url = "http//www.baidu.com";
if(url.endsWith(".com")){
System.out.println("网址",7);
}
if(url.startsWith("http")){
System.out.println("http协议");
}
if(url.startsWith("www")){
System.out.println("万维网");
}
12, 判断一个字符串在另一个字符串里面的位置用 IndexOf方法,若存在则返回该字符串第一个字符出现的位置, 若不存在则返回-1
String il = "hello java";
int index = il.indexOf("java");
System.out.println(index);
13,获取子字符串 subString
//从index到结尾
String sStr = il.substring(6);
System.out.println(sStr);
//从1到5的字符组成新的字符串
String sStr1 = il.substring(1,5);
System.out.println(sStr1);
14, 将字符串转化为字符数组用toCharArray方法;将所有字符转化为小写 toLowerCase / toUpperCase方法; 将字符串前面和后面的空格字符删除用 trim方法
15,可变字符串
StringBuffer 编程安全;StringBuilder 线程不安全 效率更高
//创建的同时先准备好6个字符的空间
StringBuilder sb = new StringBuilder(6);
// append 在末尾追加
sb.append("I");
sb.append(" Love");
sb.append(" Android");
System.out.println(sb);
// insert 插入数据
sb.insert(2, "also ");
System.out.println(sb);
// replace 替换
// start end string
int start = sb.indexOf("Android");
int end = start + "Android".length();
sb.replace(start,end,"you!!!");
System.out.println(sb);
// reverse 反转
sb.reverse();
System.out.println(sb);
}
}
三、代理设计模式
项目:设计一个程序改变字体大小和颜色的功能
方法一:
1,先创建一个阅读界面
public class Read{
private String text;
private String color;
private int size;
public Read(String text,String color,int size){
this.text = text;
this.color = color;
this.size = size;
}
//模拟进入设置页面
public void goToSetting(){
//1,创建设置页面的对象
Setting setting = new Setting(this);
//2,推送到设置页面
setting.starSetting();
}
//提供给外部一个方法,可以通过这个方法为我赋值
public void change(String color,int size){
System.out.println("改变前的颜色"+this.color+"改变前的字体大小"+this.size);
this.color = color;
this.size = size;
System.out.println("改变后的颜色"+this.color+"改变后的字体大小"+this.size);
}
}
在设置一个设置类
public class Setting{
Read deleance; //用来记录为谁设置颜色和大小 或记录设置完成后将数据返回给谁
//创建设置页面对象的时候,就需要告诉设置页面是谁创建的,也就是这个界面因谁而创建
public Setting(Read deleance){
this.deleance = deleance;
}
//开始设置 需要返回给
public void startSetting(){
System.out.println("开始设置");
System.out.println("即将返回数据");
//如果可以直接访问属性 直接通过属性给值
//比较少用 对象没办法第一时间知道自己需要的值已经得到
deleance.change("red",18);
}
}
但此时加入多一个聊天界面想要使用这个setting类更改字体和颜色,就必须在Setting类面写两个构造方法,虽然可以通过多态的方式处理,但在后面调用change方法是还要用instanceof找到具体的类,另外,在两个界面都调用的时候还会出现空指针异常,显然这不是一种好的方法。此时可以使用接口实现回调,即代理设计模式
方法二:
1,阅读界面
package swu.xwj.day7;
import swu.xwj.day7.Setting.FontSeetingInterface;
/**
* 阅读界面
*/
public class Read implements FontSeetingInterface {
private String text;
private String color;
private int size;
public Read(String text,String color,int size){
this.text = text;
this.color = color;
this.size = size;
}
public void goTOsetting(){
//1,创建一个设置页面
Setting setting = new Setting(this);
//2,推送到设置页面
setting.startSetting();
}
@Override
public void change(String color, int size) {
System.out.println("设置前的颜色为"+this.color+"设置前的字体大小为"+this.size);
this.color = color;
this.size = size;
System.out.println("设置前的颜色为"+this.color+"设置前的字体大小为"+this.size);
}
}
2,聊天界面
package swu.xwj.day7;
/**
* 聊天界面
*/
public class Chat implements Setting.FontSeetingInterface {
private String color;
private int size;
public Chat(String color,int size){
this.color = color;
this.size = size;
}
public void goTOsetting(){
//1,创建一个设置页面
Setting setting = new Setting(this);
//2,推送到设置页面
setting.startSetting();
}
@Override
public void change(String color, int size) {
System.out.println("设置前的颜色为"+this.color+"设置前的字体大小为"+this.size);
this.color = color;
this.size = size;
System.out.println("设置前的颜色为"+this.color+"设置前的字体大小为"+this.size);
}
}
3,设置界面
package swu.xwj.day7;
/**
* 设置界面 设置字体颜色和大小 此种方法复用性低
*/
public class Setting {
FontSeetingInterface object;
public Setting(FontSeetingInterface object) {
this.object = object;
}
//使用一个接口,强制所有的使用者实现这个方法
public interface FontSeetingInterface{
//自己规定的方法
void change(String color,int size);
}
public void startSetting(){
object.change("yellow",16);
}
}
4,主界面
package swu.xwj.day7;
/**
* 接口实现回调 代理设计模式
*/
public class MyClass {
public static void main(String[] args){
//创建阅读界面
Read read = new Read("hello world","black",16);
//设置字体颜色
read.goTOsetting();
//创建聊天界面
Chat chat = new Chat("red",24);
//设置字体颜色
chat.goTOsetting();
}
}

四、感悟
今天学习的东西比较零碎,但都不算难理解,重点就在于代理设计模式,也就是用接口实现回调,解决好类与类之间的数据处理,接口的概念虽然简单,但是实际操作比较困难,多联系才能真正掌握它。