Context
とFilter
実装の詳細の間に危険な依存関係を作成しています。Context
このように結合度が高いと、を追加または変更するたびにを変更する必要があるなど、すでに気づいている問題に悩まされますFilter
。
Context
知っておくべきことはFilter
、実装の詳細を実行する public メソッドがあることです。そして、それ以上のものはありません!このメソッドはFilter
インターフェースで定義されています。
Filter
の存在に依存しContext
ます。また、パラメータの処理方法も知っています。
一連の責任を実装するためにContext
、一連のFilter
オブジェクトを使用できます。これらのオブジェクトを反復処理して、インターフェイスで定義されたフィルタリング メソッドを呼び出すことができます。
擬似コードでは、次のようになります。
class Filter {
function Filter(context, params) {
// initializes a filter object with a context and its specific parameters
}
function run() {
// run is the method defined by the Filter interface
// here goes the implementation details for this filter
}
}
class Context {
array filters = [];
function applyFilter(filter) {
filters.push(filter);
}
function run() {
for filter in filters {
// all a context needs to know is how to execute a filter
filter.run
}
}
}
void main() {
context = new Context();
filter_a = new Filter(context, params_a);
filter_b = new Filter(context, params_b);
context.applyFilter(filter_a);
context.applyFilter(filter_b);
context.run();
}
疑似コードについては、あらかじめお詫び申し上げます。あなたの例の構文に基づいて、Java またはおそらく C++ を使用していると思います。どちらもわかりませんが、同じ構文に従おうとしました。
これが問題に光を当てることを願っています。何か明確にすることができるかどうか教えてください。
一番!