荣小石

导航

java-完全解耦-例子

 1 class filter{
 2     public String name(){
 3         return getClass().getSimpleName();
 4         }   
 5      public String process(String s){
 6         return s;
 7 }
 8 class filter1 extends filter{
 9     public filter1(){
10         print("I'm filter1");
11     }
12 }
13 class filter2 extens filter{
14     public filter2(){
15         print("I'm filter2");
16     }
17 }
18 interface processer{
19     Object process(Object item);
20 }
21 class filteradapter implements processer{
22         filter f;
23         filteradapter(filter f){
24             this.f = f;
25         }
26         public Object pocess(Object item){
27             f.process((String)item);
28             }
29 }
30 public class coupling{
31     public void process(processer p, String s){
32         p.process(s);
33         }
34     public static void main(String[] args){
35         String s = "learing";
36         process(new filteradapter(new filter1()), s);
37         process(new filteradapter(new filter2()), s);
38     }
39 }

自己看代码运行结果:

1 I'm filter1
2 learning
3 I'm filter2
4 learning

不知道对不对

posted on 2019-11-23 01:31  Qiav  阅读(472)  评论(0编辑  收藏  举报