关闭

链表模式

229人阅读 评论(0) 收藏 举报

定义一个接口

public interface Filter {
	public String doFilter(String smg);
}

 

继承接口的处理方法

public class CrudFilter implements Filter {
	String message;
	@Override
	public String doFilter(String smg) {
		// TODO Auto-generated method stub
		this.message = smg.replace("脏话", "*");
		return message;
	}
}

 

public class FaceFilter implements Filter {
	String message;
	@Override
	public String doFilter(String smg) {
		// TODO Auto-generated method stub
		this.message = smg.replace(":)", "笑脸");
		return message;
	}
}

 

 

public class HarmoniousFilter implements Filter {
	String message;
	@Override
	public String doFilter(String smg) {
		return smg.replace("不和谐", "和谐");
	}
}

 

处理方式一:

 

public class TestMain {
	public static void main(String[] args) {
		String result;
		String msg = ":),脏话   不和谐   关键字";
		FaceFilter faceFilter = new FaceFilter();
		CrudFilter crudFilter = new CrudFilter();
		HarmoniousFilter harmonious = new HarmoniousFilter();
		msg = faceFilter.doFilter(msg);
		msg = crudFilter.doFilter(msg);
		msg = harmonious.doFilter(msg);
		System.out.println(msg);
	}
}

 

处理方式二:public class ArrayTest {

	public static void main(String[] args) {
		Filter[] arrayFilters ={new CrudFilter(),new FaceFilter(),new HarmoniousFilter()};
		String result;
		String msg = ":),脏话   不和谐   关键字";
		for(int i = 0; i < arrayFilters.length; i++)
		{
			msg = arrayFilters[i].doFilter(msg);
		}
		System.out.println(msg);
	}
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:325906次
    • 积分:8913
    • 等级:
    • 排名:第2245名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论