java-ja 第1回 チキチキ『( ゜ェ゜)・;'.、ゴフッ』 - Strategy

1,923 views
1,778 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,923
On SlideShare
0
From Embeds
0
Number of Embeds
42
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

java-ja 第1回 チキチキ『( ゜ェ゜)・;'.、ゴフッ』 - Strategy

  1. 1. ( ) ;. Strategy @daisuke_m 2011/04/25
  2. 2. • @daisuke_m• @j5ik2o•• Java• Jiemamy• Apache Mahout
  3. 3. Strategy•••• ( ) →
  4. 4. • …
  5. 5. http://ja.wikipedia.org/wiki/Strategy_
  6. 6. " "• java.io.File
  7. 7. File•… • … • • …• file1, file2, file10 …
  8. 8. Strategy• case1 : •• case2 : if elseif elseif… •
  9. 9. /* case1 */void sort(List<File> target) { // sort by name}/* case2 */void sort(List<File> target, String type) { if ("name".equasls(type)) { // sort by name } else if ("size".equals(type)) { // sort by size } else { // default ... }}
  10. 10. Strategy• case 3 : ( ) •• case 4 : •
  11. 11. •" "
  12. 12. Strategy• •• • • → • SQL → RDBMS•
  13. 13. public interface Strategy { String process(int i);}public class Client { Strategy strategy; public Client(Strategy strategy) { this.strategy = strategy; } public void run() { for (int i = 1; i <= 100; i++) { System.out.println(strategy.process(i); } }}
  14. 14. /* FizzBuzz */public class FizzBuzzStrategy implements Strategy { public String process(int i) { if (i % 3 == 0 && i % 5 == 0) return "FizzBuzz"; if (i % 3 == 0) return "Fizz"; if (i % 5 == 0) return "Buzz"; return String.valueOf(i); }}/* Nabeatsu */public class NabeatsuStrategy implements Strategy { public String process(int i) { if (i % 3 == 0 || String.valueOf(i).contains("3")) { return i + " ( ´Д` ) ";; } return String.valueOf(i); }}
  15. 15. Client c = new Client( ); c.run();new FizzBuzzStrategy() new NabeatsuStrategy() FizzBuzz Nabeatsu 1 1 2 2 Fizz 3 ( ´Д` ) 4 4 Buzz 5 Fizz 6 ( ´Д` ) 7 7 8 8 9 ( ´Д` ) Fizz Buzz 10 11 11 12 ( ´Д` ) Fizz 13 13 ( ´Д` )

×