四角を動かすだけ(2)。
前回↑の続き。
package com.ukiyu.blog002; public class Service { public static void main(String[] args) { System.out.println("Ver.002 Start"); // 座標(100, 100)に存在する20x30の大きさの四角形 Rectangle rectangle = Rectangle.of(100, 100, 20, 30); System.out.println(rectangle); // x方向に10動かす。 rectangle.addX(10); // y方向に20動かす。 rectangle.addY(20); System.out.println(rectangle); } }
package com.ukiyu.blog002; public class Rectangle { private double x; private double y; private double height; private double width; private Rectangle(double x, double y, double height, double width) { this.x = x; this.y = y; this.height = height; this.width = width; } static public Rectangle of(double x, double y, double height, double width) { return new Rectangle(x, y, height, width); } public void addX(double x) { this.x += x; } public void addY(double y) { this.y += y; } @Override public String toString() { return "Rectangle (x, y) = ( " + x + ", " + y + ") , (height, width) = ( " + height + ", " + width + ")"; } }
コードはこちら↓。
https://github.com/ukiyu/blog/tree/master/ForBlog/src/com/ukiyu/blog002
気になる点。
- 引数が多い。
- インスタンスが可変。
- x,y同時に動かしたいな。
x, y 同時に動かしたいな。
まぁ、全く難しい事ではないので、さくっと。
public void addXY(double x, double y) { this.x += x; this.y += y; }
rectangle.addXY(10, 20);
複数存在することで1つの概念を成すものは、常に同時に扱われるようにします。
インスタンスが可変。
addXY()を行うことによってインスタンスの状態が変化してしまいます。
これを防ぐためには常に新しいインスタンスを返すようにします。
public Rectangle addXY(double x, double y) { return of(this.x + x, this.y + y, this.height, this.width); }
// x方向に10, y方向に20動かす。 rectangle = rectangle.addXY(10, 20);
addXY()を呼んでるrectangleと戻り値を受け取るrectangleは異なるインスタンスを参照しており、
それぞれのインスタンスの状態は不変であることが分かるでしょうか?
分かりにくいです。NGです。
だから、ローカル変数を使いまわすなと・・・。
正しく、分かりやすく名前を付け直しましょう。addXY()する前と、した後は違う状態なのです。
名前が異なるのが当然です。
// x方向に10, y方向に20動かす。 Rectangle addedRectangle = rectangle.addXY(10, 20);
これでrectangleは最初に生成されてから、状態は常に不変であり、それが明白になりました。
もちろん、addedRectangleも常に不変です。
修正後。
package com.ukiyu.blog004; public class Service { public static void main(String[] args) { System.out.println("Ver.004 Start"); // 座標(100, 100)に存在する20x30の大きさの四角形 Rectangle rectangle = Rectangle.of(100, 100, 20, 30); System.out.println(rectangle); // x方向に10, y方向に20動かす。 Rectangle addedRectangle = rectangle.addXY(10, 20); System.out.println(addedRectangle); } }
package com.ukiyu.blog004; public class Rectangle { private final double x; private final double y; private final double height; private final double width; private Rectangle(double x, double y, double height, double width) { this.x = x; this.y = y; this.height = height; this.width = width; } static public Rectangle of(double x, double y, double height, double width) { return new Rectangle(x, y, height, width); } public Rectangle addXY(double x, double y) { return of(this.x + x, this.y + y, this.height, this.width); } @Override public String toString() { return "Rectangle (x, y) = ( " + x + ", " + y + ") , (height, width) = ( " + height + ", " + width + ")"; } }
コンソール出力。
Ver.004 Start Rectangle (x, y) = ( 100.0, 100.0) , (height, width) = ( 20.0, 30.0) Rectangle (x, y) = ( 110.0, 120.0) , (height, width) = ( 20.0, 30.0)
コードはこちら↓。
https://github.com/ukiyu/blog/tree/master/ForBlog/src/com/ukiyu/blog004
さて、モデリングっぽい修正が必要な、
- 引数が多い。
点の修正は、また次回。