Java

「Javaプログラミング能力認定試験課題プログラムのリファクタリングレポート」に触発されて

ryoasai さんの「Javaプログラミング能力認定試験課題プログラムのリファクタリングレポート」に触発されたので、 Java の勉強がてら、同様なプログラムを作成してみる。 Step 2 メニューを表示し、"E" が入力されれば終了。 Main.java import java.io.Buffe…

「Javaプログラミング能力認定試験課題プログラムのリファクタリングレポート」に触発されて

id:ryoasai さんの 「Javaプログラミング能力認定試験課題プログラムのリファクタリングレポート」に触発されたので、 Java の勉強がてら、同様なプログラムを作成してみる。 Step 1 まず、"9" が入力されるまで、入力を待つだけのプログラム Main.java impo…

Interpreter パターン

Interpreter パターン MyMain public class MyMain { public static void main(String args[]) { if (args.length < 3) return; Operator ope; if (args[0].equals("add")) ope = new Addition(); else if (args[0].equals("sub")) ope = new Subtraction();…

State パターン

State パターン MyMain public class MyMain { public static void main(String args[]) { MyContext context = new MyContext(); context.changeState(MyStateNight.getInstance()); for (int i = 0; i <= 24; i++) { System.out.print(i); System.out.prin…

Proxy パターン

Proxy パターン MyMain public class MyMain { public static void main(String args[]) { // 代理人クラスを使用しているか、 // 本物のクラスを使用しているか、わからない MyClass classA = new MyProxy(args[0]); System.out.println(classA.getName());…

Flyweight パターン

Flyweight パターン MyMain public class MyMain { public static void main(String args[]) { MyPool pool = new MyPool(); MyAlbum album = pool.getEmployee("TWIST", "サイコーな Rock You!"); if (album != null) System.out.println(album.albumName …

Facade パターン

Facade パターン Step 1 MyMain public class MyMain { public static void main(String args[]) { MyClassA classA = new MyClassA(); System.out.println(classA.getName()); System.out.println(classA.getAge()); MyClassB classB = new MyClassB(); Sys…

Decorator パターン

Decorator パターン Step 1 MyMain public class MyMain { public static void main(String args[]) { MyClassA classA = new MyClassA(); System.out.println(classA.getName()); // classA を 拡張したクラス classA = new MyClassAex(); System.out.print…

Composite パターン

Composite パターン Step 1 MyMain public class MyMain { public static void main(String args[]) { MyArtist artist = new MyArtist("矢沢永吉"); MyAlbum album = new MyAlbum("TWIST"); artist.addAlbum(album); album.addMusic(new MyMusic("サイコー…

Bridge パターン

Bridge パターン Step 1 MyMain public class MyMain { public static void main(String args[]) { // AとBの組み合わせが 2X2で 4種類のクラスを作成 MyClass myClass = new MyClassA1B1(); System.out.println(myClass.getName()); System.out.print…

Adapter パターン

Adapter パターン MyMain public class MyMain { public static void main(String args[]) { MyClassA classA = new MyClassA("2000", "01", "01"); MyClassB adapter = new MyAdapter(classA); MyClassB classB = new MyClassB("1999/12/31"); // 同じイン…

Prototype パターン

Prototype パターン MyMain public class MyMain { public static void main(String args[]) { MyBuilder builder = new MyBuilder(); // "年月日"形式の日付 MyDirectorA dirA = new MyDirectorA(); MyClass ymd = dirA.createMyClass(builder); // "月日年…

Builder パターン

Builder パターン MyMain public class MyMain { public static void main(String args[]) { MyBuilder builder = new MyBuilder(); // "年月日"形式の日付 MyDirectorA dirA = new MyDirectorA(); MyClass myclass = dirA.createMyClass(builder); System.o…

Abstract Factory パターン

Abstract Factory パターン MyMain public class MyMain { public static void main(String args[]) { // シンプルファクトリを 使用して、ファクトリオブジェクトを 作成 MyFactory factory = MySimpleFactory.getFactory(args[0]); if (factory == null) r…

Factory Method パターン

Factory Method パターン Step 1 MyMain public class MyMain { public static void main(String args[]) { MyClassA classA = new MyClassA(); System.out.println(classA.getName()); System.out.println(classA.getAge()); MyClassB classB = new MyClass…

Strategy パターン

Strategy パターン Step 4 Strategy パターン MiniDuckSimulatorStep 1 public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); RedHeadDuck redhead = new RedHeadDuck(); mallard.display(…

Strategy パターン

Strategy パターン Step 3 委譲 MiniDuckSimulatorStep 1 public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); RedHeadDuck redhead = new RedHeadDuck(); mallard.display(); mallard.pe…

Strategy パターン

Strategy パターン Step 2 オーバーライド MiniDuckSimulatorStep 1 public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); RedHeadDuck redhead = new RedHeadDuck(); mallard.display(); …

Strategy パターン

Strategy パターン Step 1 MiniDuckSimulator public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); RedHeadDuck redhead = new RedHeadDuck(); mallard.display(); mallard.performQuack(…

デザインパターン

デザインパターン in Java Strategy パターン step 1 Strategy パターン step 2 Strategy パターン step 3 Strategy パターン step 4 State パターン Factory Method パターン Abstract Factory パターン Adapter パターン Bridge パターン Decorator パター…

57. アノテーション

57. アノテーション 情報の付加 L:\lesson132\test.java @interface TestCode { } @TestCode @interface Copyright1 { String value(); } @Copyright1(value = "LeonAkasaka") @interface Copyright2 { String value(); } @Copyright2("LeonAkasaka") @inter…

56. 可変長パラメータ

56. 可変長パラメータ 任意の数の引数を得る L:\lesson131\test.java class test { public static void main(String args[]) { getArray("Kitty" , "Cat" , 1 , 10 , Math.PI); String [] ary = new String[] { "Kitty" , "on", "your" , "lap" }; getArray(…

55. staticインポート

55. staticインポート staticメンバの限定名を省略する L:\lesson129\test.java import java.lang.Math.*; class test { public static void main(String args[]) { System.out.println(Math.PI); System.out.println(Math.abs(-50)); System.out.println(Ma…

54. 列挙型

54. 列挙型 Enumクラス L:\lesson126\test.java class test { public static void main(String args[]) { System.out.println(OStan.WIN95); System.out.println(OStan.WIN98); System.out.println(OStan.WINME); System.out.println(OStan.WIN2K); System.o…

53. for-each文

53. for-each文 拡張されたfor文 L:\lesson125\test.java class test { public static void main(String args[]) { MyList list1 = new MyList(10); for(Integer i : list1) { System.out.println("i = " + i); } java.util.Collection<String> list2 = new java.uti</string>…

52. ボクシング

52. ボクシング オートボクシングとアンボクシング L:\lesson124\test.java class test { public static void main(String args[]) { Integer objValue1 = new Integer(100); // 従来の方法 Integer objValue2 = 100; // OK int iValue = objValue2; // OK S…

51. ワイルドカード

51. ワイルドカード 型変数を特定しない操作 L:\lesson122\test.java class test { public static void main(String args[]) { printValue(new Value<String>("kitty on your lap")); printValue(new Value<Integer>(new Integer(10))); Value obj; obj = new Value<String>("Test"); </string></integer></string>…

50. 型変数

50. 型変数 実装の保障 L:\lesson121\test.java class test { public static void main(String args[]) { Value<Integer> obj1 = new Value<Integer>(new Integer(10000)); Value<Double> obj2 = new Value<Double>( new Double(0.12345)); obj1.printValue(); obj2.printValue(); } } class Va</double></double></integer></integer>…

49. Generics

49. Generics パラメータ化された型 L:\lesson118\test.java class test { public static void main(String args[]) { Value<String> obj1 = new Value<String>("kitty on your lap"); Value<Integer> obj2 = new Value<Integer>(new Integer(10)); KeyValue<String, Integer> obj3 = new KeyValue<String, Integer>("keyValue", n</string,></string,></integer></integer></string></string>…

48. 内部クラス4

48. 内部クラス4 無名クラス L:\lesson117\test.java class test { public static void main(String args[]) { top obj = new top(); NEKO rena = obj.getKitty(); rena.write(); } } class top { NEKO getKitty() { return new NEKO() { void write() { Sys…