欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java-完全解耦-例子

程序员文章站 2022-05-29 09:41:55
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 ex ......
 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

不知道对不对