技術メモ/FluentInterface(流れるようなインターフェース)

1月 1, 2003 · Posted in 技術メモ · Comment 

概要

メソッドを数珠繋ぎで呼び出す。

-http://www.martinfowler.com/bliki/FluentInterface.html
http://capsctrl.que.jp/kdmsnr/wiki/bliki/?FluentInterface
-http://en.wikipedia.org/wiki/Fluent_interface
–a fluent interface (as first coined by Eric Evans and Martin Fowler)
–defined through the return value of a called method
–self referential, where the new context is equivalent to the last context
–terminated through the return of a void context.

サンプルコード

package test;

/**
* fluent interfaceのテスト
*/
public class FluentCalc {
int sum;

public FluentCalc add(int num) {
sum = sum + num;
return this;
}

public FluentCalc sub(int num) {
sum = sum – num;
return this;
}

public void reset(){
sum = 0;
}

public static void main(String[] args) {
FluentCalc test = new FluentCalc();

test.reset();
test.add(10);
test.add(20);
test.sub(50);
test.add(100);
System.out.println(test.sum);
// 80

test.reset();

test.add(10).add(20).sub(50).add(100);

System.out.println(test.sum);
// 80
}
}

技術メモ/アンチパターン

1月 1, 2003 · Posted in 技術メモ · Comment 

技術メモ/アンチパターン/オブジェクト汚染

1月 1, 2003 · Posted in アンチパターン, 技術メモ · Comment 

引数で受け取ったオブジェクトのプロパティを、メソッドの内部で変更すること。

オブジェクト汚染メソッド

public void getData(DataDto dataDto){
dataDto.dataList = Service.getData();
}

呼び出し側

public void service(){
DataDto dataDto = new DataDto();
getData(dataDto);
}

この実装方法は、たぶんC++の流れ。これをやられると、呼び出し側を見ても何がおきているのかまったくわからない。
最近は保守の案件ばかり引き受けているため、2000年〜2003年くらいにカットオーバーされたシステムとよく出会う。するとたいていこのパターンに出会う。
-可読性最悪
-保守性最悪
-テストできない

次ページへ »