码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式学习

时间:2014-11-12 13:33:38      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   java   sp   

1 策略设计模式

 

bubuko.com,布布扣
 1 package thinkJava;
 2 /*
 3  * 策略设计模式
 4  * 像本例一样,能够根据所传递的参数对象的不同而具有不同的行为的方法。
 5  * Processor对象就是一个策略,有3中不同的策略应用到了String类型的对象上。
 6  */
 7 import java.util.Arrays;
 8 
 9 class Processor{
10     public String name(){
11         return getClass().getSimpleName();
12     }
13     Object process(Object input){
14         return input;
15     }
16 }
17 class Upcase extends Processor{
18     String process(Object input){
19         return ((String)input).toUpperCase();
20     }
21 }
22 class Downcase extends Processor{
23     String process(Object input){
24         return ((String)input).toLowerCase();
25     }
26 }
27 class Splitter extends Processor{
28     String process(Object input){
29         return Arrays.toString(((String)input).split(" "));
30     }
31 }
32 public class Apply {
33     
34     public static void process(Processor p, Object s){
35         System.out.println("Using Processor " + p.name());
36         System.out.println(p.process(s));
37     }
38     public static String s = "Disagreemment with beliefs is by definition incorrect";
39     public static void main(String[] args){
40         process(new Upcase(), s);
41         process(new Downcase(), s);
42         process(new Splitter(), s);
43     }
44 
45 }
View Code

 

运行结果

bubuko.com,布布扣

 

设计模式学习

标签:style   blog   http   io   color   ar   os   java   sp   

原文地址:http://www.cnblogs.com/hdu-2010/p/4091766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!