链表模式
定义一个接口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); } }
回忆过去,珍惜现在,放眼未来