#質問
以下に掲載する3パターンのコードのうち、パターン2とパターン3はDI(依存性の注入)と呼ばれるパターンなのだと思います。
逆にパターン1はクラスのコンストラクタ内で依存しているクラスのインスタンスを生成しているものです。
DIすることでオブジェクト間の依存度を下げることが出来るのはなんとなく理解していますが、何でもかんでもDIすれば良いっていう話でもないような気がしています。
というのも、今回のケースではオブジェクトを一つ引数に渡して済む程度の話ですが、依存するオブジェクトが複数あった場合、その数分だけ引数に渡すとなった場合、煩雑な気がしてしまいます。(そこでDIコンテナが登場という話になるのかもしれませんが、それでも、なんでもDIしたほうが良いという話なのでしょうか…?)
質問としましては以下2点となります。
(1)以下のようなコードの場合はどのパターンがより適しているでしょうか?
(2)どういうオブジェクトを、またはどういうシチュエーションではDIをすべきなのでしょうか?
ご教授頂けると大変ありがたいです。よろしくお願いいたします。
パターン1
TypeScript
1class Circle { 2 3 private point: Point; 4 private radius: Length; 5 6 constructor(x: number, y: number, radius: number) { 7 this.point = new Point(new Cordinate(x), new Cordinate(y)); 8 this.radius = new Length(radius); 9 } 10 11 12 get position(): {x: Cordinate, y: Cordinate} { 13 return this.point.cordinates; 14 } 15 16 17} 18 19 20class Point { 21 22 private x: Cordinate; 23 private y: Cordinate; 24 25 constructor(x: Cordinate, y: Cordinate) { 26 this.x = x; 27 this.y = y; 28 } 29 30 get cordinates(): {x: Cordinate, y: Cordinate} { 31 return {x: this.x, y: this.y}; 32 } 33 34} 35 36class Cordinate { 37 38 private cordinate: number; 39 40 constructor(value: number) { 41 this.cordinate = value; 42 } 43 44 get value(): number{ 45 return this.cordinate; 46 } 47 48} 49 50 51 52class Length { 53 54 private length: number; 55 56 constructor(value: number) { 57 this.length = value; 58 } 59 60 get value(): number { 61 return this.length; 62 } 63 64} 65 66 67var circle = new Circle(10, 10, 10);
パターン2
TypeScript
1class Circle { 2 3 private point: Point; 4 private radius: Length; 5 6 constructor(point: Point, radius: Length) { 7 this.point = point; 8 this.radius = radius; 9 } 10 11 get position(): {x: Cordinate, y: Cordinate} { 12 return this.point.cordinates; 13 } 14 15} 16 17 18class Point { 19 20 private x: Cordinate; 21 private y: Cordinate; 22 23 constructor(x: Cordinate, y: Cordinate) { 24 this.x = x; 25 this.y = y; 26 } 27 28 get cordinates(): {x: Cordinate, y: Cordinate} { 29 return {x: this.x, y: this.y}; 30 } 31 32 33 34} 35 36class Cordinate { 37 38 private cordinate: number; 39 40 constructor(value: number) { 41 this.cordinate = value; 42 } 43 44 get value(): number{ 45 return this.cordinate; 46 } 47 48} 49 50 51 52class Length { 53 54 private length: number; 55 56 constructor(value: number) { 57 this.length = value; 58 } 59 60 get value(): number{ 61 return this.length; 62 } 63 64} 65 66 67var circle = new Circle(new Point(new Cordinate(10), new Cordinate(10)), new Length(10));
パターン3
TypeScript
1class Circle { 2 3 private point: Point; 4 private radius: Length; 5 6 constructor(x: Cordinate, y: Cordinate, radius: number) { 7 this.point = new Point(x, y); 8 this.radius = new Length(radius); 9 } 10 11 get position(): {x: Cordinate, y: Cordinate} { 12 return this.point.cordinates; 13 } 14 15 16} 17 18 19class Point { 20 21 private x: Cordinate; 22 private y: Cordinate; 23 24 constructor(x: Cordinate, y: Cordinate) { 25 this.x = x; 26 this.y = y; 27 } 28 29 get cordinates(): {x: Cordinate, y: Cordinate} { 30 return {x: this.x, y: this.y}; 31 } 32 33} 34 35class Cordinate { 36 37 private cordinate: number; 38 39 constructor(value: number) { 40 this.cordinate = value; 41 } 42 43 get value(): number{ 44 return this.cordinate; 45 } 46 47} 48 49 50class Length { 51 52 private length: number; 53 54 constructor(value: number) { 55 this.length = value; 56 } 57 58 get value(): number { 59 return this.length; 60 } 61 62} 63 64 65var circle = new Circle(new Cordinate(10), new Cordinate(10), 10);
追記(14:29)
TypeScript Playground - https://www.typescriptlang.org/play/
TypeScriptのJavaScriptにコンパイルされたコードの掲載リクエストがありましたので追記させて頂きます。
パターン1のJavaScriptコード
JavaScript
1var Circle = (function () { 2 function Circle(x, y, radius) { 3 this.point = new Point(new Cordinate(x), new Cordinate(y)); 4 this.radius = new Length(radius); 5 } 6 Object.defineProperty(Circle.prototype, "position", { 7 get: function () { 8 return this.point.cordinates; 9 }, 10 enumerable: true, 11 configurable: true 12 }); 13 return Circle; 14}()); 15var Point = (function () { 16 function Point(x, y) { 17 this.x = x; 18 this.y = y; 19 } 20 Object.defineProperty(Point.prototype, "cordinates", { 21 get: function () { 22 return { x: this.x, y: this.y }; 23 }, 24 enumerable: true, 25 configurable: true 26 }); 27 return Point; 28}()); 29var Cordinate = (function () { 30 function Cordinate(value) { 31 this.cordinate = value; 32 } 33 Object.defineProperty(Cordinate.prototype, "value", { 34 get: function () { 35 return this.cordinate; 36 }, 37 enumerable: true, 38 configurable: true 39 }); 40 return Cordinate; 41}()); 42var Length = (function () { 43 function Length(value) { 44 this.length = value; 45 } 46 Object.defineProperty(Length.prototype, "value", { 47 get: function () { 48 return this.length; 49 }, 50 enumerable: true, 51 configurable: true 52 }); 53 return Length; 54}()); 55var circle = new Circle(10, 10, 10); 56
パターン2のJavaScriptコード
JavaScript
1var Circle = (function () { 2 function Circle(point, radius) { 3 this.point = point; 4 this.radius = radius; 5 } 6 Object.defineProperty(Circle.prototype, "position", { 7 get: function () { 8 return this.point.cordinates; 9 }, 10 enumerable: true, 11 configurable: true 12 }); 13 return Circle; 14}()); 15var Point = (function () { 16 function Point(x, y) { 17 this.x = x; 18 this.y = y; 19 } 20 Object.defineProperty(Point.prototype, "cordinates", { 21 get: function () { 22 return { x: this.x, y: this.y }; 23 }, 24 enumerable: true, 25 configurable: true 26 }); 27 return Point; 28}()); 29var Cordinate = (function () { 30 function Cordinate(value) { 31 this.cordinate = value; 32 } 33 Object.defineProperty(Cordinate.prototype, "value", { 34 get: function () { 35 return this.cordinate; 36 }, 37 enumerable: true, 38 configurable: true 39 }); 40 return Cordinate; 41}()); 42var Length = (function () { 43 function Length(value) { 44 this.length = value; 45 } 46 Object.defineProperty(Length.prototype, "value", { 47 get: function () { 48 return this.length; 49 }, 50 enumerable: true, 51 configurable: true 52 }); 53 return Length; 54}()); 55var circle = new Circle(new Point(new Cordinate(10), new Cordinate(10)), new Length(10)); 56
パターン3のJavaScriptコード
JavaScript
1var Circle = (function () { 2 function Circle(x, y, radius) { 3 this.point = new Point(x, y); 4 this.radius = new Length(radius); 5 } 6 Object.defineProperty(Circle.prototype, "position", { 7 get: function () { 8 return this.point.cordinates; 9 }, 10 enumerable: true, 11 configurable: true 12 }); 13 return Circle; 14}()); 15var Point = (function () { 16 function Point(x, y) { 17 this.x = x; 18 this.y = y; 19 } 20 Object.defineProperty(Point.prototype, "cordinates", { 21 get: function () { 22 return { x: this.x, y: this.y }; 23 }, 24 enumerable: true, 25 configurable: true 26 }); 27 return Point; 28}()); 29var Cordinate = (function () { 30 function Cordinate(value) { 31 this.cordinate = value; 32 } 33 Object.defineProperty(Cordinate.prototype, "value", { 34 get: function () { 35 return this.cordinate; 36 }, 37 enumerable: true, 38 configurable: true 39 }); 40 return Cordinate; 41}()); 42var Length = (function () { 43 function Length(value) { 44 this.length = value; 45 } 46 Object.defineProperty(Length.prototype, "value", { 47 get: function () { 48 return this.length; 49 }, 50 enumerable: true, 51 configurable: true 52 }); 53 return Length; 54}()); 55var circle = new Circle(new Cordinate(10), new Cordinate(10), 10); 56
回答2件
あなたの回答
tips
プレビュー