From 115498b4eae54fed99f985ef294889481ce0655c Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:07:48 +0800 Subject: [PATCH 01/44] Create component.urm.puml --- component/etc/component.urm.puml | 1 + 1 file changed, 1 insertion(+) create mode 100644 component/etc/component.urm.puml diff --git a/component/etc/component.urm.puml b/component/etc/component.urm.puml new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/component/etc/component.urm.puml @@ -0,0 +1 @@ + From efd1f461d71f45bbcbcaeaac65fe3997a11e4374 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:08:34 +0800 Subject: [PATCH 02/44] Create App.java --- component/src/main/java/com/iluwater/component/App.java | 1 + 1 file changed, 1 insertion(+) create mode 100644 component/src/main/java/com/iluwater/component/App.java diff --git a/component/src/main/java/com/iluwater/component/App.java b/component/src/main/java/com/iluwater/component/App.java new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/component/src/main/java/com/iluwater/component/App.java @@ -0,0 +1 @@ + From 9db533e8f35a190d5b5d20bc9536d9ccef18fb3d Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:09:11 +0800 Subject: [PATCH 03/44] Add files via upload --- .../main/java/com/iluwater/component/App.java | 54 ++++++++++++++++++- .../component/BjornGraphicsComponent.java | 14 +++++ .../component/BjornInputComponent.java | 11 ++++ .../component/BjornPhysicsComponent.java | 17 ++++++ .../com/iluwater/component/Component.java | 6 +++ .../com/iluwater/component/GameObject.java | 43 +++++++++++++++ .../iluwater/component/GraphicsComponent.java | 6 +++ .../iluwater/component/InputComponent.java | 6 +++ .../iluwater/component/PhysicsComponent.java | 6 +++ 9 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java create mode 100644 component/src/main/java/com/iluwater/component/BjornInputComponent.java create mode 100644 component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java create mode 100644 component/src/main/java/com/iluwater/component/Component.java create mode 100644 component/src/main/java/com/iluwater/component/GameObject.java create mode 100644 component/src/main/java/com/iluwater/component/GraphicsComponent.java create mode 100644 component/src/main/java/com/iluwater/component/InputComponent.java create mode 100644 component/src/main/java/com/iluwater/component/PhysicsComponent.java diff --git a/component/src/main/java/com/iluwater/component/App.java b/component/src/main/java/com/iluwater/component/App.java index 8b137891791f..71667ecabfc6 100644 --- a/component/src/main/java/com/iluwater/component/App.java +++ b/component/src/main/java/com/iluwater/component/App.java @@ -1 +1,53 @@ - +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwater.component; + +import java.util.ArrayList; +/* + * Object-oriented to a certain extent can solve many problems of code reuse and data reuse, + * but it also has great defects: + * 1.the coupling of data organization is very strong. + * 2.Interface logic is difficult to reuse and hot plug. + * + * The component pattern solves the defects of object orientation and process orientation and + * is widely used in game clients + * + * A component is a part of one object. We can consider that a object contains multiple + * components, in another way, multiple components can construct a object. + * Here is a demo using component pattern to solve a game-like problem. + * A person named Bjorn who has three component: get input;judge physics state;show graphics state + * These three component with a common game object can construct our protagonist:Bjorn, also can + * construct other objects like dog or cat if you want to write a real game. + */ + +public class App { + public static void main(String[] args) { + ArrayListarrayList = new ArrayList<>(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + GameObject gameObject = new GameObject(arrayList); + gameObject.update(); + } +} diff --git a/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java b/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java new file mode 100644 index 000000000000..f94052cc0b08 --- /dev/null +++ b/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java @@ -0,0 +1,14 @@ +package com.iluwater.component; + +import static org.slf4j.LoggerFactory.getLogger; + +import org.slf4j.Logger; + +public class BjornGraphicsComponent implements GraphicsComponent { + private static final Logger LOGGER = getLogger(BjornGraphicsComponent.class); + + @Override + public void update(GameObject gameObject) { + LOGGER.info("positive:" + gameObject.getPositionOFx() + "," + gameObject.getPositionOFy()); + } +} diff --git a/component/src/main/java/com/iluwater/component/BjornInputComponent.java b/component/src/main/java/com/iluwater/component/BjornInputComponent.java new file mode 100644 index 000000000000..dd7a343e7729 --- /dev/null +++ b/component/src/main/java/com/iluwater/component/BjornInputComponent.java @@ -0,0 +1,11 @@ +package com.iluwater.component; + + +public class BjornInputComponent implements InputComponent { + + @Override + public void update(GameObject gameObject) { + gameObject.setPositionOFx(gameObject.getPositionOFx() + gameObject.getVelocity()); + gameObject.setPositionOFy(gameObject.getPositionOFy() + gameObject.getVelocity()); + } +} diff --git a/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java b/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java new file mode 100644 index 000000000000..e95b3f34bd5b --- /dev/null +++ b/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java @@ -0,0 +1,17 @@ +package com.iluwater.component; + +import static org.slf4j.LoggerFactory.getLogger; +import org.slf4j.Logger; + + +public class BjornPhysicsComponent implements PhysicsComponent{ + + private static final Logger LOGGER = getLogger(BjornPhysicsComponent.class); + + @Override + public void update(GameObject gameObject) { + if(gameObject.getPositionOFx() == gameObject.getPositionOFy()){ + LOGGER.info("Your position is pretty good, keep it!"); + } + } +} diff --git a/component/src/main/java/com/iluwater/component/Component.java b/component/src/main/java/com/iluwater/component/Component.java new file mode 100644 index 000000000000..5f12d0599a90 --- /dev/null +++ b/component/src/main/java/com/iluwater/component/Component.java @@ -0,0 +1,6 @@ +package com.iluwater.component; + +public interface Component { + + void update(GameObject gameObject); +} diff --git a/component/src/main/java/com/iluwater/component/GameObject.java b/component/src/main/java/com/iluwater/component/GameObject.java new file mode 100644 index 000000000000..a935827bb98e --- /dev/null +++ b/component/src/main/java/com/iluwater/component/GameObject.java @@ -0,0 +1,43 @@ +package com.iluwater.component; + +import java.util.ArrayList; + +public class GameObject { + public int velocity; + public int positionOFx; + public int positionOFy; + ArrayList componentArrayList; + public GameObject(ArrayList componentArrayList){ + this.componentArrayList = componentArrayList; + } + + public void setVelocity(int velocity) { + this.velocity = velocity; + } + + public int getVelocity() { + return velocity; + } + + public void setPositionOFx(int positionOFx) { + this.positionOFx = positionOFx; + } + + public int getPositionOFx() { + return positionOFx; + } + + public void setPositionOFy(int positionOFy) { + this.positionOFy = positionOFy; + } + + public int getPositionOFy() { + return positionOFy; + } + + public void update(){ + for (Component component : componentArrayList) { + component.update(this); + } + } +} diff --git a/component/src/main/java/com/iluwater/component/GraphicsComponent.java b/component/src/main/java/com/iluwater/component/GraphicsComponent.java new file mode 100644 index 000000000000..07acf3997989 --- /dev/null +++ b/component/src/main/java/com/iluwater/component/GraphicsComponent.java @@ -0,0 +1,6 @@ +package com.iluwater.component; + +public interface GraphicsComponent extends Component{ + + void update(GameObject gameObject); +} diff --git a/component/src/main/java/com/iluwater/component/InputComponent.java b/component/src/main/java/com/iluwater/component/InputComponent.java new file mode 100644 index 000000000000..3ab05bae4c3f --- /dev/null +++ b/component/src/main/java/com/iluwater/component/InputComponent.java @@ -0,0 +1,6 @@ +package com.iluwater.component; + +public interface InputComponent extends Component { + + void update(GameObject gameObject); +} diff --git a/component/src/main/java/com/iluwater/component/PhysicsComponent.java b/component/src/main/java/com/iluwater/component/PhysicsComponent.java new file mode 100644 index 000000000000..45aa497c9aaa --- /dev/null +++ b/component/src/main/java/com/iluwater/component/PhysicsComponent.java @@ -0,0 +1,6 @@ +package com.iluwater.component; + +public interface PhysicsComponent extends Component { + + void update(GameObject gameObject); +} From ea1180c6441d68b080ec528caa2029c270c7e37f Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:10:52 +0800 Subject: [PATCH 04/44] Add files via upload --- component/src/main/java/module-info.java | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 component/src/main/java/module-info.java diff --git a/component/src/main/java/module-info.java b/component/src/main/java/module-info.java new file mode 100644 index 000000000000..50a21a12f15f --- /dev/null +++ b/component/src/main/java/module-info.java @@ -0,0 +1,26 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +module com.iluwatar.component { + requires org.slf4j; +} From d725caaea9d643cd9f7d602772de656468b846c8 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:11:16 +0800 Subject: [PATCH 05/44] Add files via upload --- component/etc/component.urm.puml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/component/etc/component.urm.puml b/component/etc/component.urm.puml index 8b137891791f..02af47ddf261 100644 --- a/component/etc/component.urm.puml +++ b/component/etc/component.urm.puml @@ -1 +1,2 @@ - +@startuml +@enduml \ No newline at end of file From 127527d594480f52bfe517d2615a259148d04bdb Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:11:54 +0800 Subject: [PATCH 06/44] Add files via upload --- component/README.md | 25 +++++++++++++++++++++ component/component.iml | 31 ++++++++++++++++++++++++++ component/pom.xml | 48 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 component/README.md create mode 100644 component/component.iml create mode 100644 component/pom.xml diff --git a/component/README.md b/component/README.md new file mode 100644 index 000000000000..e0a43e6cf9b2 --- /dev/null +++ b/component/README.md @@ -0,0 +1,25 @@ +--- +layout: pattern +title: Component +folder: component +permalink: /patterns/component/ +categories: Behavioral +tags: + - Java +--- + +## Intent + +> Allow a single entity to span multiple domains without coupling the domains to each other. + +## Class diagram +![alt text](./etc/commander.urm.png "Commander class diagram") + +## Applicability +1.You want to keep decoupled from each other in a class which contains multiple domains. +2.A class is too big and massive. + + +## Reference + +* [http://gameprogrammingpatterns.com/component.html] diff --git a/component/component.iml b/component/component.iml new file mode 100644 index 000000000000..d63126d7910c --- /dev/null +++ b/component/component.iml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/pom.xml b/component/pom.xml new file mode 100644 index 000000000000..62a95ec9cbdd --- /dev/null +++ b/component/pom.xml @@ -0,0 +1,48 @@ + + + + java-design-patterns + com.iluwatar + 1.23.0-SNAPSHOT + + 4.0.0 + + component + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + com.iluwatar.component.App + + + + + + + + + + \ No newline at end of file From 3c35cdc60e16cdfa8fe8170fe5c94b56c79a0255 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:13:38 +0800 Subject: [PATCH 07/44] Create AppTest.java --- component/src/test/java/com/iluwater/component/AppTest.java | 1 + 1 file changed, 1 insertion(+) create mode 100644 component/src/test/java/com/iluwater/component/AppTest.java diff --git a/component/src/test/java/com/iluwater/component/AppTest.java b/component/src/test/java/com/iluwater/component/AppTest.java new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/component/src/test/java/com/iluwater/component/AppTest.java @@ -0,0 +1 @@ + From 76b3a73a2af8274ec3d00baf4db7e28a99bf0b78 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:13:58 +0800 Subject: [PATCH 08/44] Add files via upload --- .../java/com/iluwater/component/AppTest.java | 35 ++++++++++++- .../com/iluwater/component/UpdateTest.java | 49 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 component/src/test/java/com/iluwater/component/UpdateTest.java diff --git a/component/src/test/java/com/iluwater/component/AppTest.java b/component/src/test/java/com/iluwater/component/AppTest.java index 8b137891791f..abb160a42551 100644 --- a/component/src/test/java/com/iluwater/component/AppTest.java +++ b/component/src/test/java/com/iluwater/component/AppTest.java @@ -1 +1,34 @@ - +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwater.component; + +import org.junit.jupiter.api.Test; +/** + * Tests that Component example runs without errors. + */ +public class AppTest { + @Test + public void test() { + App.main(new String[]{}); + } +} diff --git a/component/src/test/java/com/iluwater/component/UpdateTest.java b/component/src/test/java/com/iluwater/component/UpdateTest.java new file mode 100644 index 000000000000..9a62c518b4af --- /dev/null +++ b/component/src/test/java/com/iluwater/component/UpdateTest.java @@ -0,0 +1,49 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwater.component; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static junit.framework.TestCase.assertEquals; + +/** + * Tests that Component example runs without errors. + */ +public class UpdateTest { + @Test + public void UpdateTest() { + ArrayList arrayList = new ArrayList<>(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + GameObject gameObject = new GameObject(arrayList); + gameObject.setPositionOFy(12); + gameObject.setPositionOFx(13); + gameObject.setVelocity(1); + gameObject.update(); + assertEquals(14, gameObject.getPositionOFx()); + assertEquals(13,gameObject.getPositionOFy()); + } +} From 17e3e47d977472f2740faa0806d8d4c4c97dcb15 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Thu, 7 May 2020 04:14:21 +0800 Subject: [PATCH 09/44] Update README.md --- component/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/component/README.md b/component/README.md index e0a43e6cf9b2..ff595cc2e73d 100644 --- a/component/README.md +++ b/component/README.md @@ -12,8 +12,6 @@ tags: > Allow a single entity to span multiple domains without coupling the domains to each other. -## Class diagram -![alt text](./etc/commander.urm.png "Commander class diagram") ## Applicability 1.You want to keep decoupled from each other in a class which contains multiple domains. From 2424fc05a1f1f3f116e2fa60562459a0521d7a20 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:14:42 +0800 Subject: [PATCH 10/44] Update README.md --- component/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/component/README.md b/component/README.md index ff595cc2e73d..c76d4578af65 100644 --- a/component/README.md +++ b/component/README.md @@ -5,7 +5,9 @@ folder: component permalink: /patterns/component/ categories: Behavioral tags: - - Java + - Java + - Game Programming + - Decoupling --- ## Intent From c4de5d419ac74982ae5c39fa0c4999e5ccd88710 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:16:31 +0800 Subject: [PATCH 11/44] Update pom.xml --- component/pom.xml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/component/pom.xml b/component/pom.xml index 62a95ec9cbdd..399f1d152d01 100644 --- a/component/pom.xml +++ b/component/pom.xml @@ -11,12 +11,6 @@ component - - junit - junit - 4.12 - test - org.junit.jupiter junit-jupiter-engine @@ -45,4 +39,4 @@ - \ No newline at end of file + From 5382562c45e33774175dc35c65d53d872799b30e Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:19:21 +0800 Subject: [PATCH 12/44] Update App.java --- .../src/main/java/com/iluwater/component/App.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/component/src/main/java/com/iluwater/component/App.java b/component/src/main/java/com/iluwater/component/App.java index 71667ecabfc6..67efb3d957a0 100644 --- a/component/src/main/java/com/iluwater/component/App.java +++ b/component/src/main/java/com/iluwater/component/App.java @@ -1,4 +1,4 @@ -/* + /* * The MIT License * Copyright © 2014-2019 Ilkka Seppälä * @@ -24,7 +24,7 @@ package com.iluwater.component; import java.util.ArrayList; -/* +/** * Object-oriented to a certain extent can solve many problems of code reuse and data reuse, * but it also has great defects: * 1.the coupling of data organization is very strong. @@ -36,15 +36,18 @@ * A component is a part of one object. We can consider that a object contains multiple * components, in another way, multiple components can construct a object. * Here is a demo using component pattern to solve a game-like problem. - * A person named Bjorn who has three component: get input;judge physics state;show graphics state - * These three component with a common game object can construct our protagonist:Bjorn, also can + * A person named Bjorn who has three components: input;physics;graphics + * These three components with a common game object can construct our protagonist:Bjorn, also can * construct other objects like dog or cat if you want to write a real game. */ public class App { + /** + * Launcher for this demo design pattern + */ public static void main(String[] args) { - ArrayListarrayList = new ArrayList<>(); - arrayList.add(new BjornInputComponent()); + ArrayList arrayList = new ArrayList<>(); + arrayList.add(new BjornInputComponent());s arrayList.add(new BjornPhysicsComponent()); arrayList.add(new BjornGraphicsComponent()); GameObject gameObject = new GameObject(arrayList); From ee60881a398dcb29757ee33dd67cda13fe0aaba0 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:20:53 +0800 Subject: [PATCH 13/44] Update BjornGraphicsComponent.java --- .../iluwater/component/BjornGraphicsComponent.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java b/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java index f94052cc0b08..4e0321b05b4d 100644 --- a/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java +++ b/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java @@ -4,9 +4,20 @@ import org.slf4j.Logger; +/** + * BjornGraphicsComponent is a class for our main game star + * This class creat a Graphics component for Bjorn. + */ + public class BjornGraphicsComponent implements GraphicsComponent { private static final Logger LOGGER = getLogger(BjornGraphicsComponent.class); + /** + * This method is a logger for Bjorn when happens a Graphics update. + * In real scenario, there will be code for Graphics Update. + * + * @param gameObject is a object in the game, here it is Bjorn + */ @Override public void update(GameObject gameObject) { LOGGER.info("positive:" + gameObject.getPositionOFx() + "," + gameObject.getPositionOFy()); From 6d277bddadab26805b548030d2f57e59a955b8f5 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:21:33 +0800 Subject: [PATCH 14/44] Update BjornInputComponent.java --- .../com/iluwater/component/BjornInputComponent.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/component/src/main/java/com/iluwater/component/BjornInputComponent.java b/component/src/main/java/com/iluwater/component/BjornInputComponent.java index dd7a343e7729..441d2a98282c 100644 --- a/component/src/main/java/com/iluwater/component/BjornInputComponent.java +++ b/component/src/main/java/com/iluwater/component/BjornInputComponent.java @@ -1,8 +1,19 @@ package com.iluwater.component; +/** + * BjornInputComponent is a class for our main game star + * This class creat a Input component for Bjorn. + */ public class BjornInputComponent implements InputComponent { + /** + * This method is a logger for Bjorn when happens a Input update. + * In real scenario, there will be code for dealing with IO. + * + * @param gameObject is a object in the game, here it is Bjorn + */ + @Override public void update(GameObject gameObject) { gameObject.setPositionOFx(gameObject.getPositionOFx() + gameObject.getVelocity()); From d59f5aa71647d787eb58feff1e27564dca22aeee Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:22:12 +0800 Subject: [PATCH 15/44] Update BjornPhysicsComponent.java --- .../iluwater/component/BjornPhysicsComponent.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java b/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java index e95b3f34bd5b..e0f3b871b8bd 100644 --- a/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java +++ b/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java @@ -1,13 +1,23 @@ package com.iluwater.component; import static org.slf4j.LoggerFactory.getLogger; -import org.slf4j.Logger; +import org.slf4j.Logger; +/** + * BjornPhysicsComponent is a class for our main game star + * This class creat a Physics component for Bjorn. + */ public class BjornPhysicsComponent implements PhysicsComponent{ private static final Logger LOGGER = getLogger(BjornPhysicsComponent.class); + /** + * This method is a logger for Bjorn when happens a Physics update. + * In real scenario, there will be code for Physics Update. + * + * @param gameObject is a object in the game, here it is Bjorn + */ @Override public void update(GameObject gameObject) { if(gameObject.getPositionOFx() == gameObject.getPositionOFy()){ From ae15aab168599ce15b6c72d3470b9a3c6ab93918 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:22:59 +0800 Subject: [PATCH 16/44] Update Component.java --- .../src/main/java/com/iluwater/component/Component.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/component/src/main/java/com/iluwater/component/Component.java b/component/src/main/java/com/iluwater/component/Component.java index 5f12d0599a90..ed1fc6b835e7 100644 --- a/component/src/main/java/com/iluwater/component/Component.java +++ b/component/src/main/java/com/iluwater/component/Component.java @@ -1,6 +1,9 @@ package com.iluwater.component; -public interface Component { - void update(GameObject gameObject); +/** + * Component is an interface for all component. + */ + +public interface Component { void update(GameObject gameObject); } From c08896a41def869f655785b3a33c1436b1d5e8a7 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 17:31:32 +0800 Subject: [PATCH 17/44] Update App.java --- .../main/java/com/iluwater/component/App.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/component/src/main/java/com/iluwater/component/App.java b/component/src/main/java/com/iluwater/component/App.java index 67efb3d957a0..2a1f114dcd71 100644 --- a/component/src/main/java/com/iluwater/component/App.java +++ b/component/src/main/java/com/iluwater/component/App.java @@ -1,4 +1,4 @@ - /* +/* * The MIT License * Copyright © 2014-2019 Ilkka Seppälä * @@ -21,7 +21,7 @@ * THE SOFTWARE. */ -package com.iluwater.component; +package com.iluwatar.component; import java.util.ArrayList; /** @@ -37,20 +37,20 @@ * components, in another way, multiple components can construct a object. * Here is a demo using component pattern to solve a game-like problem. * A person named Bjorn who has three components: input;physics;graphics - * These three components with a common game object can construct our protagonist:Bjorn, also can + * These three component with a common game object can construct our protagonist:Bjorn, also can * construct other objects like dog or cat if you want to write a real game. */ public class App { - /** - * Launcher for this demo design pattern - */ - public static void main(String[] args) { - ArrayList arrayList = new ArrayList<>(); - arrayList.add(new BjornInputComponent());s - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - GameObject gameObject = new GameObject(arrayList); - gameObject.update(); - } + /** + * Launcher for this demo design pattern + */ + public static void main(String[] args) { + ArrayList arrayList = new ArrayList<>(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + GameObject gameObject = new GameObject(arrayList); + gameObject.update(); + } } From adb9bda9858cc52288d00053997537f29b7c4e7c Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:57:12 +0800 Subject: [PATCH 18/44] Delete App.java --- .../main/java/com/iluwater/component/App.java | 56 ------------------- 1 file changed, 56 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/App.java diff --git a/component/src/main/java/com/iluwater/component/App.java b/component/src/main/java/com/iluwater/component/App.java deleted file mode 100644 index 2a1f114dcd71..000000000000 --- a/component/src/main/java/com/iluwater/component/App.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.iluwatar.component; - -import java.util.ArrayList; -/** - * Object-oriented to a certain extent can solve many problems of code reuse and data reuse, - * but it also has great defects: - * 1.the coupling of data organization is very strong. - * 2.Interface logic is difficult to reuse and hot plug. - * - * The component pattern solves the defects of object orientation and process orientation and - * is widely used in game clients - * - * A component is a part of one object. We can consider that a object contains multiple - * components, in another way, multiple components can construct a object. - * Here is a demo using component pattern to solve a game-like problem. - * A person named Bjorn who has three components: input;physics;graphics - * These three component with a common game object can construct our protagonist:Bjorn, also can - * construct other objects like dog or cat if you want to write a real game. - */ - -public class App { - /** - * Launcher for this demo design pattern - */ - public static void main(String[] args) { - ArrayList arrayList = new ArrayList<>(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - GameObject gameObject = new GameObject(arrayList); - gameObject.update(); - } -} From f9f9291c7aa6f688945020587913d9b0fb9a29a4 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:57:28 +0800 Subject: [PATCH 19/44] Delete BjornGraphicsComponent.java --- .../component/BjornGraphicsComponent.java | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java diff --git a/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java b/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java deleted file mode 100644 index 4e0321b05b4d..000000000000 --- a/component/src/main/java/com/iluwater/component/BjornGraphicsComponent.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.iluwater.component; - -import static org.slf4j.LoggerFactory.getLogger; - -import org.slf4j.Logger; - -/** - * BjornGraphicsComponent is a class for our main game star - * This class creat a Graphics component for Bjorn. - */ - -public class BjornGraphicsComponent implements GraphicsComponent { - private static final Logger LOGGER = getLogger(BjornGraphicsComponent.class); - - /** - * This method is a logger for Bjorn when happens a Graphics update. - * In real scenario, there will be code for Graphics Update. - * - * @param gameObject is a object in the game, here it is Bjorn - */ - @Override - public void update(GameObject gameObject) { - LOGGER.info("positive:" + gameObject.getPositionOFx() + "," + gameObject.getPositionOFy()); - } -} From 6b9506c883bd4a34b79ad8b7e9c3ce950c17ab6e Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:57:43 +0800 Subject: [PATCH 20/44] Delete BjornInputComponent.java --- .../component/BjornInputComponent.java | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/BjornInputComponent.java diff --git a/component/src/main/java/com/iluwater/component/BjornInputComponent.java b/component/src/main/java/com/iluwater/component/BjornInputComponent.java deleted file mode 100644 index 441d2a98282c..000000000000 --- a/component/src/main/java/com/iluwater/component/BjornInputComponent.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.iluwater.component; - -/** - * BjornInputComponent is a class for our main game star - * This class creat a Input component for Bjorn. - */ - -public class BjornInputComponent implements InputComponent { - - /** - * This method is a logger for Bjorn when happens a Input update. - * In real scenario, there will be code for dealing with IO. - * - * @param gameObject is a object in the game, here it is Bjorn - */ - - @Override - public void update(GameObject gameObject) { - gameObject.setPositionOFx(gameObject.getPositionOFx() + gameObject.getVelocity()); - gameObject.setPositionOFy(gameObject.getPositionOFy() + gameObject.getVelocity()); - } -} From cc14e60a43b164f31c21681b3c532a22fa0ce99e Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:58:09 +0800 Subject: [PATCH 21/44] Delete BjornPhysicsComponent.java --- .../component/BjornPhysicsComponent.java | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java diff --git a/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java b/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java deleted file mode 100644 index e0f3b871b8bd..000000000000 --- a/component/src/main/java/com/iluwater/component/BjornPhysicsComponent.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.iluwater.component; - -import static org.slf4j.LoggerFactory.getLogger; - -import org.slf4j.Logger; - -/** - * BjornPhysicsComponent is a class for our main game star - * This class creat a Physics component for Bjorn. - */ -public class BjornPhysicsComponent implements PhysicsComponent{ - - private static final Logger LOGGER = getLogger(BjornPhysicsComponent.class); - - /** - * This method is a logger for Bjorn when happens a Physics update. - * In real scenario, there will be code for Physics Update. - * - * @param gameObject is a object in the game, here it is Bjorn - */ - @Override - public void update(GameObject gameObject) { - if(gameObject.getPositionOFx() == gameObject.getPositionOFy()){ - LOGGER.info("Your position is pretty good, keep it!"); - } - } -} From e18495f71a92bbe2fefa645ceca44cdcee444c6b Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:58:20 +0800 Subject: [PATCH 22/44] Delete Component.java --- .../src/main/java/com/iluwater/component/Component.java | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/Component.java diff --git a/component/src/main/java/com/iluwater/component/Component.java b/component/src/main/java/com/iluwater/component/Component.java deleted file mode 100644 index ed1fc6b835e7..000000000000 --- a/component/src/main/java/com/iluwater/component/Component.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.iluwater.component; - - -/** - * Component is an interface for all component. - */ - -public interface Component { void update(GameObject gameObject); -} From 14061d8d7bfcb3f81f59c85a798e65e5ab5f3882 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:58:31 +0800 Subject: [PATCH 23/44] Delete GameObject.java --- .../com/iluwater/component/GameObject.java | 43 ------------------- 1 file changed, 43 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/GameObject.java diff --git a/component/src/main/java/com/iluwater/component/GameObject.java b/component/src/main/java/com/iluwater/component/GameObject.java deleted file mode 100644 index a935827bb98e..000000000000 --- a/component/src/main/java/com/iluwater/component/GameObject.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.iluwater.component; - -import java.util.ArrayList; - -public class GameObject { - public int velocity; - public int positionOFx; - public int positionOFy; - ArrayList componentArrayList; - public GameObject(ArrayList componentArrayList){ - this.componentArrayList = componentArrayList; - } - - public void setVelocity(int velocity) { - this.velocity = velocity; - } - - public int getVelocity() { - return velocity; - } - - public void setPositionOFx(int positionOFx) { - this.positionOFx = positionOFx; - } - - public int getPositionOFx() { - return positionOFx; - } - - public void setPositionOFy(int positionOFy) { - this.positionOFy = positionOFy; - } - - public int getPositionOFy() { - return positionOFy; - } - - public void update(){ - for (Component component : componentArrayList) { - component.update(this); - } - } -} From 6e1e1aa9efdb4d9ef3b15a8d0311bdfd13b0a41d Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:58:51 +0800 Subject: [PATCH 24/44] Delete GraphicsComponent.java --- .../main/java/com/iluwater/component/GraphicsComponent.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/GraphicsComponent.java diff --git a/component/src/main/java/com/iluwater/component/GraphicsComponent.java b/component/src/main/java/com/iluwater/component/GraphicsComponent.java deleted file mode 100644 index 07acf3997989..000000000000 --- a/component/src/main/java/com/iluwater/component/GraphicsComponent.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iluwater.component; - -public interface GraphicsComponent extends Component{ - - void update(GameObject gameObject); -} From 435e7fcbbc3024d6b4e4338ff97cf2a9d4bb841d Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:59:05 +0800 Subject: [PATCH 25/44] Delete InputComponent.java --- .../main/java/com/iluwater/component/InputComponent.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/InputComponent.java diff --git a/component/src/main/java/com/iluwater/component/InputComponent.java b/component/src/main/java/com/iluwater/component/InputComponent.java deleted file mode 100644 index 3ab05bae4c3f..000000000000 --- a/component/src/main/java/com/iluwater/component/InputComponent.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iluwater.component; - -public interface InputComponent extends Component { - - void update(GameObject gameObject); -} From 0f586a20a575a144b506d5888a904afb889ae157 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 18:59:22 +0800 Subject: [PATCH 26/44] Delete PhysicsComponent.java --- .../main/java/com/iluwater/component/PhysicsComponent.java | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 component/src/main/java/com/iluwater/component/PhysicsComponent.java diff --git a/component/src/main/java/com/iluwater/component/PhysicsComponent.java b/component/src/main/java/com/iluwater/component/PhysicsComponent.java deleted file mode 100644 index 45aa497c9aaa..000000000000 --- a/component/src/main/java/com/iluwater/component/PhysicsComponent.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iluwater.component; - -public interface PhysicsComponent extends Component { - - void update(GameObject gameObject); -} From fb3998582578e86279a6341be7d72def192a0873 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:00:21 +0800 Subject: [PATCH 27/44] Create App.java --- .../main/java/com/iluwatar/component/App.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/App.java diff --git a/component/src/main/java/com/iluwatar/component/App.java b/component/src/main/java/com/iluwatar/component/App.java new file mode 100644 index 000000000000..3a019942f10d --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/App.java @@ -0,0 +1,56 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.component; + +import java.util.ArrayList; +/** + * Object-oriented to a certain extent can solve many problems of code reuse and data reuse, + * but it also has great defects: + * 1.the coupling of data organization is very strong. + * 2.Interface logic is difficult to reuse and hot plug. + * + * The component pattern solves the defects of object orientation and process orientation and + * is widely used in game clients + * + * A component is a part of one object. We can consider that a object contains multiple + * components, in another way, multiple components can construct a object. + * Here is a demo using component pattern to solve a game-like problem. + * A person named Bjorn who has three components: input;physics;graphics + * These three component with a common game object can construct our protagonist:Bjorn, also can + * construct other objects like dog or cat if you want to write a real game. + */ + +public class App { + /** + * Launcher for this demo design pattern + */ + public static void main(String[] args) { + ArrayList arrayList = new ArrayList<>(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + GameObject gameObject = new GameObject(arrayList); + gameObject.update(); + } +} From eb7231030f617ab632b9435403d0e2e5edcc7360 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:01:03 +0800 Subject: [PATCH 28/44] Update App.java --- component/src/main/java/com/iluwatar/component/App.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/component/src/main/java/com/iluwatar/component/App.java b/component/src/main/java/com/iluwatar/component/App.java index 3a019942f10d..2dd9225e0d04 100644 --- a/component/src/main/java/com/iluwatar/component/App.java +++ b/component/src/main/java/com/iluwatar/component/App.java @@ -37,7 +37,7 @@ * components, in another way, multiple components can construct a object. * Here is a demo using component pattern to solve a game-like problem. * A person named Bjorn who has three components: input;physics;graphics - * These three component with a common game object can construct our protagonist:Bjorn, also can + * These three components with a common game object can construct our protagonist:Bjorn, also can * construct other objects like dog or cat if you want to write a real game. */ From b6c54b519b35aacb1dfa59ade449144204c16466 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:09:13 +0800 Subject: [PATCH 29/44] Update App.java --- component/src/main/java/com/iluwatar/component/App.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/component/src/main/java/com/iluwatar/component/App.java b/component/src/main/java/com/iluwatar/component/App.java index 2dd9225e0d04..1df2cd50f2fc 100644 --- a/component/src/main/java/com/iluwatar/component/App.java +++ b/component/src/main/java/com/iluwatar/component/App.java @@ -37,7 +37,7 @@ * components, in another way, multiple components can construct a object. * Here is a demo using component pattern to solve a game-like problem. * A person named Bjorn who has three components: input;physics;graphics - * These three components with a common game object can construct our protagonist:Bjorn, also can + * These three component with a common game object can construct our protagonist:Bjorn, also can * construct other objects like dog or cat if you want to write a real game. */ @@ -46,11 +46,11 @@ public class App { * Launcher for this demo design pattern */ public static void main(String[] args) { - ArrayList arrayList = new ArrayList<>(); + var arrayList = new ArrayList(); arrayList.add(new BjornInputComponent()); arrayList.add(new BjornPhysicsComponent()); arrayList.add(new BjornGraphicsComponent()); - GameObject gameObject = new GameObject(arrayList); + var gameObject = new GameObject(arrayList); gameObject.update(); } } From ce89d74c3a937cb2b620397f6a6a7b5affc8805f Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:10:10 +0800 Subject: [PATCH 30/44] Create BjornGraphicsComponent.java --- .../component/BjornGraphicsComponent.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java diff --git a/component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java b/component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java new file mode 100644 index 000000000000..912b3190cacb --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java @@ -0,0 +1,25 @@ +package com.iluwatar.component; + +import static org.slf4j.LoggerFactory.getLogger; + +import org.slf4j.Logger; + +/** + * BjornGraphicsComponent is a class for our main game star + * This class creat a Graphics component for Bjorn. + */ + +public class BjornGraphicsComponent implements GraphicsComponent { + private static final Logger LOGGER = getLogger(BjornGraphicsComponent.class); + + /** + * This method is a logger for Bjorn when happens a Graphics update. + * In real scenario, there will be code for Graphics Update. + * + * @param gameObject is a object in the game, here it is Bjorn + */ + @Override + public void update(GameObject gameObject) { + LOGGER.info("positive:" + gameObject.getPositionOFx() + "," + gameObject.getPositionOFy()); + } +} From 4565b893553c55476d1bf4e6a190406a7c0cc2ba Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:10:48 +0800 Subject: [PATCH 31/44] Create BjornInputComponent.java --- .../component/BjornInputComponent.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/BjornInputComponent.java diff --git a/component/src/main/java/com/iluwatar/component/BjornInputComponent.java b/component/src/main/java/com/iluwatar/component/BjornInputComponent.java new file mode 100644 index 000000000000..585f30715b1f --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/BjornInputComponent.java @@ -0,0 +1,22 @@ +package com.iluwatar.component; + +/** + * BjornInputComponent is a class for our main game star + * This class creat a Input component for Bjorn. + */ + +public class BjornInputComponent implements InputComponent { + + /** + * This method is a logger for Bjorn when happens a Input update. + * In real scenario, there will be code for dealing with IO. + * + * @param gameObject is a object in the game, here it is Bjorn + */ + + @Override + public void update(GameObject gameObject) { + gameObject.setPositionOFx(gameObject.getPositionOFx() + gameObject.getVelocity()); + gameObject.setPositionOFy(gameObject.getPositionOFy() + gameObject.getVelocity()); + } +} From 6d0da627687c59bc67e9323dfce0d33fecc6a36b Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:11:14 +0800 Subject: [PATCH 32/44] Create BjornPhysicsComponent.java --- .../component/BjornPhysicsComponent.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java diff --git a/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java b/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java new file mode 100644 index 000000000000..6c3db04dc5a1 --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java @@ -0,0 +1,27 @@ +package com.iluwatar.component; + +import static org.slf4j.LoggerFactory.getLogger; + +import org.slf4j.Logger; + +/** + * BjornPhysicsComponent is a class for our main game star + * This class creat a Physics component for Bjorn. + */ +public class BjornPhysicsComponent implements PhysicsComponent{ + + private static final Logger LOGGER = getLogger(BjornPhysicsComponent.class); + + /** + * This method is a logger for Bjorn when happens a Physics update. + * In real scenario, there will be code for Physics Update. + * + * @param gameObject is a object in the game, here it is Bjorn + */ + @Override + public void update(GameObject gameObject) { + if(gameObject.getPositionOFx() == gameObject.getPositionOFy()){ + LOGGER.info("Your position is pretty good, keep it!"); + } + } +} From 31cbbe137f797d001a803b2ea3c84a1a53c64961 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:11:37 +0800 Subject: [PATCH 33/44] Create Component.java --- .../src/main/java/com/iluwatar/component/Component.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/Component.java diff --git a/component/src/main/java/com/iluwatar/component/Component.java b/component/src/main/java/com/iluwatar/component/Component.java new file mode 100644 index 000000000000..1771b3b88fb8 --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/Component.java @@ -0,0 +1,9 @@ +package com.iluwatar.component; + + +/** + * Component is an interface for all component. + */ + +public interface Component { void update(GameObject gameObject); +} From a904f007ace02ea3f92307c5d6bee00dcaf13e31 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:12:06 +0800 Subject: [PATCH 34/44] Create GameObject.java --- .../com/iluwatar/component/GameObject.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/GameObject.java diff --git a/component/src/main/java/com/iluwatar/component/GameObject.java b/component/src/main/java/com/iluwatar/component/GameObject.java new file mode 100644 index 000000000000..78844e6baea6 --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/GameObject.java @@ -0,0 +1,87 @@ +package com.iluwatar.component; + +import java.util.ArrayList; + +/** + * GameObject is a class for all object in the game. + * It was constructed by a collection of component. + */ + +public class GameObject { + private int velocity; + private int positionOFx; + private int positionOFy; + ArrayList componentArrayList; + + /** + * Constructor for GameObject + * @param componentArrayList is the list of this object contains + */ + + public GameObject(ArrayList componentArrayList){ + this.componentArrayList=new ArrayList<>(); + this.componentArrayList.addAll(componentArrayList); + } + + /** + * setter for velocity + * @param velocity is the velocity of this object + */ + + public void setVelocity(int velocity) { + this.velocity = velocity; + } + + /** + * getter for velocity + */ + + public int getVelocity() { + return velocity; + } + + /** + * setter for PositionOFx + * @param positionOFx is the PositionOFx of this object + */ + + public void setPositionOFx(int positionOFx) { + this.positionOFx = positionOFx; + } + + + /** + * getter for PositionOFx + */ + + public int getPositionOFx() { + return positionOFx; + } + + /** + * setter for PositionOFy + * @param positionOFy is the PositionOFy of this object + */ + + public void setPositionOFy(int positionOFy) { + this.positionOFy = positionOFy; + } + + /** + * getter for PositionOFy + */ + + public int getPositionOFy() { + return positionOFy; + } + + /** + * update for this object's components. + */ + + public void update(){ + for (Component component : componentArrayList) { + component.update(this); + } + } +} From 0a523ae8d2902fe6f23e662ef1f2c1dcc6574e57 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:12:26 +0800 Subject: [PATCH 35/44] Create GraphicsComponent.java --- .../java/com/iluwatar/component/GraphicsComponent.java | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/GraphicsComponent.java diff --git a/component/src/main/java/com/iluwatar/component/GraphicsComponent.java b/component/src/main/java/com/iluwatar/component/GraphicsComponent.java new file mode 100644 index 000000000000..4e6a58ce079f --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/GraphicsComponent.java @@ -0,0 +1,8 @@ +package com.iluwatar.component; + +/** + * GraphicsComponent is an interface for Graphics function + */ + +public interface GraphicsComponent extends Component { void update(GameObject gameObject); +} From 44849baf23c5ab698c288eb0bb6610027ff86ff1 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:12:49 +0800 Subject: [PATCH 36/44] Create InputComponent.java --- .../main/java/com/iluwatar/component/InputComponent.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/InputComponent.java diff --git a/component/src/main/java/com/iluwatar/component/InputComponent.java b/component/src/main/java/com/iluwatar/component/InputComponent.java new file mode 100644 index 000000000000..4f020b26563d --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/InputComponent.java @@ -0,0 +1,7 @@ +package com.iluwatar.component; + +/** + * InputComponent is an interface for Input function + */ +public interface InputComponent extends Component { void update(GameObject gameObject); +} From 4eb0d15fff83f6fdb9bce05ac6e672fc20fe8b0d Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:13:38 +0800 Subject: [PATCH 37/44] Create PhysicsComponent.java --- .../main/java/com/iluwatar/component/PhysicsComponent.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 component/src/main/java/com/iluwatar/component/PhysicsComponent.java diff --git a/component/src/main/java/com/iluwatar/component/PhysicsComponent.java b/component/src/main/java/com/iluwatar/component/PhysicsComponent.java new file mode 100644 index 000000000000..ec6facc5a410 --- /dev/null +++ b/component/src/main/java/com/iluwatar/component/PhysicsComponent.java @@ -0,0 +1,7 @@ +package com.iluwatar.component; + +/** + * PhysicsComponent is an interface for Physics function + */ +public interface PhysicsComponent extends Component { void update(GameObject gameObject); +} From e81741bdcaed3c728eb3fe3888eca26801c1156d Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:14:17 +0800 Subject: [PATCH 38/44] Delete AppTest.java --- .../java/com/iluwater/component/AppTest.java | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 component/src/test/java/com/iluwater/component/AppTest.java diff --git a/component/src/test/java/com/iluwater/component/AppTest.java b/component/src/test/java/com/iluwater/component/AppTest.java deleted file mode 100644 index abb160a42551..000000000000 --- a/component/src/test/java/com/iluwater/component/AppTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.iluwater.component; - -import org.junit.jupiter.api.Test; -/** - * Tests that Component example runs without errors. - */ -public class AppTest { - @Test - public void test() { - App.main(new String[]{}); - } -} From 0216f743930b75ba10a903db04013946165bb1e0 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:14:35 +0800 Subject: [PATCH 39/44] Delete UpdateTest.java --- .../com/iluwater/component/UpdateTest.java | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 component/src/test/java/com/iluwater/component/UpdateTest.java diff --git a/component/src/test/java/com/iluwater/component/UpdateTest.java b/component/src/test/java/com/iluwater/component/UpdateTest.java deleted file mode 100644 index 9a62c518b4af..000000000000 --- a/component/src/test/java/com/iluwater/component/UpdateTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.iluwater.component; - -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; - -import static junit.framework.TestCase.assertEquals; - -/** - * Tests that Component example runs without errors. - */ -public class UpdateTest { - @Test - public void UpdateTest() { - ArrayList arrayList = new ArrayList<>(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - GameObject gameObject = new GameObject(arrayList); - gameObject.setPositionOFy(12); - gameObject.setPositionOFx(13); - gameObject.setVelocity(1); - gameObject.update(); - assertEquals(14, gameObject.getPositionOFx()); - assertEquals(13,gameObject.getPositionOFy()); - } -} From 87ab3a9543d3de8aa78950b5547a32c42e5ab5ed Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:15:35 +0800 Subject: [PATCH 40/44] Create AppTest.java --- .../java/com/iluwatar/component/AppTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 component/src/test/java/com/iluwatar/component/AppTest.java diff --git a/component/src/test/java/com/iluwatar/component/AppTest.java b/component/src/test/java/com/iluwatar/component/AppTest.java new file mode 100644 index 000000000000..04f86d81ffe5 --- /dev/null +++ b/component/src/test/java/com/iluwatar/component/AppTest.java @@ -0,0 +1,37 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.component; + +import org.junit.jupiter.api.Test; +/** + * Tests that Component example runs without errors. + */ +public class AppTest { + /** + * test for the design pattern runnable. + */ + @Test + public void test() { + App.main(new String[]{}); + } +} From ecfd38b3abc5829fbdc6045918afeeb8c75a30d6 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:16:14 +0800 Subject: [PATCH 41/44] Create UpdateTest.java --- .../com/iluwatar/component/UpdateTest.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 component/src/test/java/com/iluwatar/component/UpdateTest.java diff --git a/component/src/test/java/com/iluwatar/component/UpdateTest.java b/component/src/test/java/com/iluwatar/component/UpdateTest.java new file mode 100644 index 000000000000..362ae9b577e3 --- /dev/null +++ b/component/src/test/java/com/iluwatar/component/UpdateTest.java @@ -0,0 +1,138 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.component; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static junit.framework.TestCase.assertEquals; + +/** + * Tests that Component example runs without errors. + */ + +public class UpdateTest { + /** + * test for the update for the input component of the object + */ + @Test + public void inputUpdateTest() { + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + var gameObject = new GameObject(arrayList); + gameObject.setPositionOFy(12); + gameObject.setPositionOFx(13); + gameObject.setVelocity(1); + gameObject.update(); + assertEquals(14, gameObject.getPositionOFx()); + assertEquals(13,gameObject.getPositionOFy()); + } + /** + * test for the update for the Physics component of the object + */ + @Test + public void physicsUpdateTest() { + var arrayList = new ArrayList(); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); + gameObject.setPositionOFy(13); + gameObject.setPositionOFx(12); + gameObject.setVelocity(1); + gameObject.update(); + assertEquals(13, gameObject.getPositionOFx()); + assertEquals(14,gameObject.getPositionOFy()); + } + /** + * test for the update for the Graphics component of the object + */ + @Test + public void graphicsUpdateTest() { + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); + gameObject.setPositionOFy(1); + gameObject.setPositionOFx(1); + gameObject.setVelocity(1); + gameObject.update(); + assertEquals(2, gameObject.getPositionOFx()); + assertEquals(2,gameObject.getPositionOFy()); + } + /** + * test for the setPositionOFx + */ + @Test + public void setPositionOFxTest(){ + var gameObject = new GameObject(null); + gameObject.setPositionOFx(1); + assertEquals(1,gameObject.getPositionOFx()); + } + /** + * test for the getPositionOFx + */ + @Test + public void getPositionOFxTest(){ + var gameObject = new GameObject(null); + gameObject.setPositionOFx(1); + assertEquals(1,gameObject.getPositionOFx()); + } + /** + * test for the setPositionOFy + */ + @Test + public void setPositionOFyTest(){ + var gameObject = new GameObject(null); + gameObject.setPositionOFy(1); + assertEquals(1,gameObject.getPositionOFy()); + } + /** + * test for the getPositionOFy + */ + @Test + public void getPositionOFyTest(){ + var gameObject = new GameObject(null); + gameObject.setPositionOFy(1); + assertEquals(1,gameObject.getPositionOFy()); + } + /** + * test for the setVelocity + */ + @Test + public void setVelocityTest(){ + var gameObject = new GameObject(null); + gameObject.setVelocity(1); + assertEquals(1,gameObject.getVelocity()); + } + /** + * test for the getVelocity + */ + @Test + public void getVelocityTest(){ + var gameObject = new GameObject(null); + gameObject.setVelocity(1); + assertEquals(1,gameObject.getVelocity()); + } +} From 7b58d277a62ee16e22a4a64da3ec52acba2e9e72 Mon Sep 17 00:00:00 2001 From: YanchaoMiao <11710204@mail.sustech.edu.cn> Date: Sun, 24 May 2020 19:21:26 +0800 Subject: [PATCH 42/44] Update pom.xml --- pom.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b81cfe8d483e..3b1f2155660f 100644 --- a/pom.xml +++ b/pom.xml @@ -184,6 +184,8 @@ combinator update-method leader-followers + arrange-act-assert + component @@ -537,4 +539,4 @@ - \ No newline at end of file + From 9dc7df8fa1ea8fd9e68d575b6638dc1b05b9c669 Mon Sep 17 00:00:00 2001 From: Hrighter Date: Sat, 30 May 2020 09:36:01 +0800 Subject: [PATCH 43/44] fix errors --- component/component.iml | 31 -------------- component/pom.xml | 1 - .../main/java/com/iluwatar/component/App.java | 6 +-- .../component/BjornPhysicsComponent.java | 4 +- .../com/iluwatar/component/GameObject.java | 24 +++++------ .../iluwatar/component/GraphicsComponent.java | 2 +- .../iluwatar/component/InputComponent.java | 2 +- .../iluwatar/component/PhysicsComponent.java | 2 +- .../com/iluwatar/component/UpdateTest.java | 40 +++++++++++++++---- pom.xml | 2 +- 10 files changed, 54 insertions(+), 60 deletions(-) delete mode 100644 component/component.iml diff --git a/component/component.iml b/component/component.iml deleted file mode 100644 index d63126d7910c..000000000000 --- a/component/component.iml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/component/pom.xml b/component/pom.xml index 399f1d152d01..f4e9a00100e4 100644 --- a/component/pom.xml +++ b/component/pom.xml @@ -38,5 +38,4 @@ - diff --git a/component/src/main/java/com/iluwatar/component/App.java b/component/src/main/java/com/iluwatar/component/App.java index 1df2cd50f2fc..df6ecae0e559 100644 --- a/component/src/main/java/com/iluwatar/component/App.java +++ b/component/src/main/java/com/iluwatar/component/App.java @@ -29,10 +29,10 @@ * but it also has great defects: * 1.the coupling of data organization is very strong. * 2.Interface logic is difficult to reuse and hot plug. - * + *

* The component pattern solves the defects of object orientation and process orientation and * is widely used in game clients - * + *

* A component is a part of one object. We can consider that a object contains multiple * components, in another way, multiple components can construct a object. * Here is a demo using component pattern to solve a game-like problem. @@ -42,7 +42,7 @@ */ public class App { - /** + /**. * Launcher for this demo design pattern */ public static void main(String[] args) { diff --git a/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java b/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java index 6c3db04dc5a1..65783a8db00e 100644 --- a/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java +++ b/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java @@ -8,7 +8,7 @@ * BjornPhysicsComponent is a class for our main game star * This class creat a Physics component for Bjorn. */ -public class BjornPhysicsComponent implements PhysicsComponent{ +public class BjornPhysicsComponent implements PhysicsComponent { private static final Logger LOGGER = getLogger(BjornPhysicsComponent.class); @@ -20,7 +20,7 @@ public class BjornPhysicsComponent implements PhysicsComponent{ */ @Override public void update(GameObject gameObject) { - if(gameObject.getPositionOFx() == gameObject.getPositionOFy()){ + if (gameObject.getPositionOFx() == gameObject.getPositionOFy()) { LOGGER.info("Your position is pretty good, keep it!"); } } diff --git a/component/src/main/java/com/iluwatar/component/GameObject.java b/component/src/main/java/com/iluwatar/component/GameObject.java index 78844e6baea6..9a632f4a2515 100644 --- a/component/src/main/java/com/iluwatar/component/GameObject.java +++ b/component/src/main/java/com/iluwatar/component/GameObject.java @@ -2,7 +2,7 @@ import java.util.ArrayList; -/** +/**. * GameObject is a class for all object in the game. * It was constructed by a collection of component. */ @@ -13,17 +13,17 @@ public class GameObject { private int positionOFy; ArrayList componentArrayList; - /** + /**. * Constructor for GameObject * @param componentArrayList is the list of this object contains */ - public GameObject(ArrayList componentArrayList){ - this.componentArrayList=new ArrayList<>(); + public GameObject(ArrayList componentArrayList) { + this.componentArrayList = new ArrayList<>(); this.componentArrayList.addAll(componentArrayList); } - /** + /**. * setter for velocity * @param velocity is the velocity of this object */ @@ -32,7 +32,7 @@ public void setVelocity(int velocity) { this.velocity = velocity; } - /** + /**. * getter for velocity */ @@ -40,7 +40,7 @@ public int getVelocity() { return velocity; } - /** + /**. * setter for PositionOFx * @param positionOFx is the PositionOFx of this object */ @@ -50,7 +50,7 @@ public void setPositionOFx(int positionOFx) { } - /** + /**. * getter for PositionOFx */ @@ -58,7 +58,7 @@ public int getPositionOFx() { return positionOFx; } - /** + /**. * setter for PositionOFy * @param positionOFy is the PositionOFy of this object */ @@ -67,7 +67,7 @@ public void setPositionOFy(int positionOFy) { this.positionOFy = positionOFy; } - /** + /**. * getter for PositionOFy */ @@ -75,11 +75,11 @@ public int getPositionOFy() { return positionOFy; } - /** + /**. * update for this object's components. */ - public void update(){ + public void update() { for (Component component : componentArrayList) { component.update(this); } diff --git a/component/src/main/java/com/iluwatar/component/GraphicsComponent.java b/component/src/main/java/com/iluwatar/component/GraphicsComponent.java index 4e6a58ce079f..b7ca526a44fd 100644 --- a/component/src/main/java/com/iluwatar/component/GraphicsComponent.java +++ b/component/src/main/java/com/iluwatar/component/GraphicsComponent.java @@ -1,6 +1,6 @@ package com.iluwatar.component; -/** +/**. * GraphicsComponent is an interface for Graphics function */ diff --git a/component/src/main/java/com/iluwatar/component/InputComponent.java b/component/src/main/java/com/iluwatar/component/InputComponent.java index 4f020b26563d..0daf137b9211 100644 --- a/component/src/main/java/com/iluwatar/component/InputComponent.java +++ b/component/src/main/java/com/iluwatar/component/InputComponent.java @@ -1,6 +1,6 @@ package com.iluwatar.component; -/** +/**. * InputComponent is an interface for Input function */ public interface InputComponent extends Component { void update(GameObject gameObject); diff --git a/component/src/main/java/com/iluwatar/component/PhysicsComponent.java b/component/src/main/java/com/iluwatar/component/PhysicsComponent.java index ec6facc5a410..afa84ca802e4 100644 --- a/component/src/main/java/com/iluwatar/component/PhysicsComponent.java +++ b/component/src/main/java/com/iluwatar/component/PhysicsComponent.java @@ -1,6 +1,6 @@ package com.iluwatar.component; -/** +/**. * PhysicsComponent is an interface for Physics function */ public interface PhysicsComponent extends Component { void update(GameObject gameObject); diff --git a/component/src/test/java/com/iluwatar/component/UpdateTest.java b/component/src/test/java/com/iluwatar/component/UpdateTest.java index 362ae9b577e3..c7a6c29d73d4 100644 --- a/component/src/test/java/com/iluwatar/component/UpdateTest.java +++ b/component/src/test/java/com/iluwatar/component/UpdateTest.java @@ -26,7 +26,8 @@ import java.util.ArrayList; -import static junit.framework.TestCase.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + /** * Tests that Component example runs without errors. @@ -54,6 +55,7 @@ public void inputUpdateTest() { @Test public void physicsUpdateTest() { var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); arrayList.add(new BjornPhysicsComponent()); arrayList.add(new BjornGraphicsComponent()); var gameObject = new GameObject(arrayList); @@ -86,7 +88,11 @@ public void graphicsUpdateTest() { */ @Test public void setPositionOFxTest(){ - var gameObject = new GameObject(null); + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); gameObject.setPositionOFx(1); assertEquals(1,gameObject.getPositionOFx()); } @@ -95,7 +101,11 @@ public void setPositionOFxTest(){ */ @Test public void getPositionOFxTest(){ - var gameObject = new GameObject(null); + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); gameObject.setPositionOFx(1); assertEquals(1,gameObject.getPositionOFx()); } @@ -104,7 +114,11 @@ public void getPositionOFxTest(){ */ @Test public void setPositionOFyTest(){ - var gameObject = new GameObject(null); + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); gameObject.setPositionOFy(1); assertEquals(1,gameObject.getPositionOFy()); } @@ -113,7 +127,11 @@ public void setPositionOFyTest(){ */ @Test public void getPositionOFyTest(){ - var gameObject = new GameObject(null); + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); gameObject.setPositionOFy(1); assertEquals(1,gameObject.getPositionOFy()); } @@ -122,7 +140,11 @@ public void getPositionOFyTest(){ */ @Test public void setVelocityTest(){ - var gameObject = new GameObject(null); + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); gameObject.setVelocity(1); assertEquals(1,gameObject.getVelocity()); } @@ -131,7 +153,11 @@ public void setVelocityTest(){ */ @Test public void getVelocityTest(){ - var gameObject = new GameObject(null); + var arrayList = new ArrayList(); + arrayList.add(new BjornInputComponent()); + arrayList.add(new BjornPhysicsComponent()); + arrayList.add(new BjornGraphicsComponent()); + var gameObject = new GameObject(arrayList); gameObject.setVelocity(1); assertEquals(1,gameObject.getVelocity()); } diff --git a/pom.xml b/pom.xml index 298be66237d3..a6e3750524d6 100644 --- a/pom.xml +++ b/pom.xml @@ -185,7 +185,7 @@ update-method leader-followers arrange-act-assert - component + component From dc69a288e7539fcb7ee47c336fc1cfe87c363c7b Mon Sep 17 00:00:00 2001 From: Hrighter Date: Sat, 30 May 2020 22:38:53 +0800 Subject: [PATCH 44/44] finite state machine design pattern --- component/README.md | 25 --- component/etc/component.urm.puml | 2 - component/pom.xml | 41 ----- .../main/java/com/iluwatar/component/App.java | 56 ------ .../component/BjornGraphicsComponent.java | 25 --- .../component/BjornInputComponent.java | 22 --- .../component/BjornPhysicsComponent.java | 27 --- .../com/iluwatar/component/Component.java | 9 - .../com/iluwatar/component/GameObject.java | 87 ---------- .../iluwatar/component/GraphicsComponent.java | 8 - .../iluwatar/component/InputComponent.java | 7 - .../iluwatar/component/PhysicsComponent.java | 7 - .../com/iluwatar/component/UpdateTest.java | 164 ------------------ .../factory/method => }/module-info.java | 0 .../featuretoggle => }/module-info.java | 0 finite-state-machine/README.md | 25 +++ .../etc/finite-state-machine.urm.puml | 66 +++++++ finite-state-machine/pom.xml | 45 +++++ .../iluwatar/finite/state/machine/App.java | 52 ++++++ .../finite/state/machine/Context.java | 64 +++++++ .../iluwatar/finite/state/machine/Event.java | 34 ++++ .../finite/state/machine/RunState.java | 64 +++++++ .../finite/state/machine/StandState.java | 64 +++++++ .../iluwatar/finite/state/machine/State.java | 37 ++++ .../finite/state/machine/WalkState.java | 64 +++++++ .../src/main/java/module-info.java | 52 +++--- .../finite/state/machine}/AppTest.java | 10 +- .../finite/state/machine/StateTest.java | 121 +++++++++++++ pom.xml | 2 +- 29 files changed, 668 insertions(+), 512 deletions(-) delete mode 100644 component/README.md delete mode 100644 component/etc/component.urm.puml delete mode 100644 component/pom.xml delete mode 100644 component/src/main/java/com/iluwatar/component/App.java delete mode 100644 component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java delete mode 100644 component/src/main/java/com/iluwatar/component/BjornInputComponent.java delete mode 100644 component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java delete mode 100644 component/src/main/java/com/iluwatar/component/Component.java delete mode 100644 component/src/main/java/com/iluwatar/component/GameObject.java delete mode 100644 component/src/main/java/com/iluwatar/component/GraphicsComponent.java delete mode 100644 component/src/main/java/com/iluwatar/component/InputComponent.java delete mode 100644 component/src/main/java/com/iluwatar/component/PhysicsComponent.java delete mode 100644 component/src/test/java/com/iluwatar/component/UpdateTest.java rename factory-method/src/main/java/{com/iluwatar/factory/method => }/module-info.java (100%) rename feature-toggle/src/main/java/{com/iluwatar/featuretoggle => }/module-info.java (100%) create mode 100644 finite-state-machine/README.md create mode 100644 finite-state-machine/etc/finite-state-machine.urm.puml create mode 100644 finite-state-machine/pom.xml create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/App.java create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Context.java create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Event.java create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/RunState.java create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/StandState.java create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/State.java create mode 100644 finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/WalkState.java rename {component => finite-state-machine}/src/main/java/module-info.java (93%) rename {component/src/test/java/com/iluwatar/component => finite-state-machine/src/test/java/com/iluwatar/finite/state/machine}/AppTest.java (90%) create mode 100644 finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/StateTest.java diff --git a/component/README.md b/component/README.md deleted file mode 100644 index c76d4578af65..000000000000 --- a/component/README.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -layout: pattern -title: Component -folder: component -permalink: /patterns/component/ -categories: Behavioral -tags: - - Java - - Game Programming - - Decoupling ---- - -## Intent - -> Allow a single entity to span multiple domains without coupling the domains to each other. - - -## Applicability -1.You want to keep decoupled from each other in a class which contains multiple domains. -2.A class is too big and massive. - - -## Reference - -* [http://gameprogrammingpatterns.com/component.html] diff --git a/component/etc/component.urm.puml b/component/etc/component.urm.puml deleted file mode 100644 index 02af47ddf261..000000000000 --- a/component/etc/component.urm.puml +++ /dev/null @@ -1,2 +0,0 @@ -@startuml -@enduml \ No newline at end of file diff --git a/component/pom.xml b/component/pom.xml deleted file mode 100644 index f4e9a00100e4..000000000000 --- a/component/pom.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - java-design-patterns - com.iluwatar - 1.23.0-SNAPSHOT - - 4.0.0 - - component - - - org.junit.jupiter - junit-jupiter-engine - test - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - - - - com.iluwatar.component.App - - - - - - - - - diff --git a/component/src/main/java/com/iluwatar/component/App.java b/component/src/main/java/com/iluwatar/component/App.java deleted file mode 100644 index df6ecae0e559..000000000000 --- a/component/src/main/java/com/iluwatar/component/App.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package com.iluwatar.component; - -import java.util.ArrayList; -/** - * Object-oriented to a certain extent can solve many problems of code reuse and data reuse, - * but it also has great defects: - * 1.the coupling of data organization is very strong. - * 2.Interface logic is difficult to reuse and hot plug. - *

- * The component pattern solves the defects of object orientation and process orientation and - * is widely used in game clients - *

- * A component is a part of one object. We can consider that a object contains multiple - * components, in another way, multiple components can construct a object. - * Here is a demo using component pattern to solve a game-like problem. - * A person named Bjorn who has three components: input;physics;graphics - * These three component with a common game object can construct our protagonist:Bjorn, also can - * construct other objects like dog or cat if you want to write a real game. - */ - -public class App { - /**. - * Launcher for this demo design pattern - */ - public static void main(String[] args) { - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.update(); - } -} diff --git a/component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java b/component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java deleted file mode 100644 index 912b3190cacb..000000000000 --- a/component/src/main/java/com/iluwatar/component/BjornGraphicsComponent.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.iluwatar.component; - -import static org.slf4j.LoggerFactory.getLogger; - -import org.slf4j.Logger; - -/** - * BjornGraphicsComponent is a class for our main game star - * This class creat a Graphics component for Bjorn. - */ - -public class BjornGraphicsComponent implements GraphicsComponent { - private static final Logger LOGGER = getLogger(BjornGraphicsComponent.class); - - /** - * This method is a logger for Bjorn when happens a Graphics update. - * In real scenario, there will be code for Graphics Update. - * - * @param gameObject is a object in the game, here it is Bjorn - */ - @Override - public void update(GameObject gameObject) { - LOGGER.info("positive:" + gameObject.getPositionOFx() + "," + gameObject.getPositionOFy()); - } -} diff --git a/component/src/main/java/com/iluwatar/component/BjornInputComponent.java b/component/src/main/java/com/iluwatar/component/BjornInputComponent.java deleted file mode 100644 index 585f30715b1f..000000000000 --- a/component/src/main/java/com/iluwatar/component/BjornInputComponent.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.iluwatar.component; - -/** - * BjornInputComponent is a class for our main game star - * This class creat a Input component for Bjorn. - */ - -public class BjornInputComponent implements InputComponent { - - /** - * This method is a logger for Bjorn when happens a Input update. - * In real scenario, there will be code for dealing with IO. - * - * @param gameObject is a object in the game, here it is Bjorn - */ - - @Override - public void update(GameObject gameObject) { - gameObject.setPositionOFx(gameObject.getPositionOFx() + gameObject.getVelocity()); - gameObject.setPositionOFy(gameObject.getPositionOFy() + gameObject.getVelocity()); - } -} diff --git a/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java b/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java deleted file mode 100644 index 65783a8db00e..000000000000 --- a/component/src/main/java/com/iluwatar/component/BjornPhysicsComponent.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.iluwatar.component; - -import static org.slf4j.LoggerFactory.getLogger; - -import org.slf4j.Logger; - -/** - * BjornPhysicsComponent is a class for our main game star - * This class creat a Physics component for Bjorn. - */ -public class BjornPhysicsComponent implements PhysicsComponent { - - private static final Logger LOGGER = getLogger(BjornPhysicsComponent.class); - - /** - * This method is a logger for Bjorn when happens a Physics update. - * In real scenario, there will be code for Physics Update. - * - * @param gameObject is a object in the game, here it is Bjorn - */ - @Override - public void update(GameObject gameObject) { - if (gameObject.getPositionOFx() == gameObject.getPositionOFy()) { - LOGGER.info("Your position is pretty good, keep it!"); - } - } -} diff --git a/component/src/main/java/com/iluwatar/component/Component.java b/component/src/main/java/com/iluwatar/component/Component.java deleted file mode 100644 index 1771b3b88fb8..000000000000 --- a/component/src/main/java/com/iluwatar/component/Component.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.iluwatar.component; - - -/** - * Component is an interface for all component. - */ - -public interface Component { void update(GameObject gameObject); -} diff --git a/component/src/main/java/com/iluwatar/component/GameObject.java b/component/src/main/java/com/iluwatar/component/GameObject.java deleted file mode 100644 index 9a632f4a2515..000000000000 --- a/component/src/main/java/com/iluwatar/component/GameObject.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.iluwatar.component; - -import java.util.ArrayList; - -/**. - * GameObject is a class for all object in the game. - * It was constructed by a collection of component. - */ - -public class GameObject { - private int velocity; - private int positionOFx; - private int positionOFy; - ArrayList componentArrayList; - - /**. - * Constructor for GameObject - * @param componentArrayList is the list of this object contains - */ - - public GameObject(ArrayList componentArrayList) { - this.componentArrayList = new ArrayList<>(); - this.componentArrayList.addAll(componentArrayList); - } - - /**. - * setter for velocity - * @param velocity is the velocity of this object - */ - - public void setVelocity(int velocity) { - this.velocity = velocity; - } - - /**. - * getter for velocity - */ - - public int getVelocity() { - return velocity; - } - - /**. - * setter for PositionOFx - * @param positionOFx is the PositionOFx of this object - */ - - public void setPositionOFx(int positionOFx) { - this.positionOFx = positionOFx; - } - - - /**. - * getter for PositionOFx - */ - - public int getPositionOFx() { - return positionOFx; - } - - /**. - * setter for PositionOFy - * @param positionOFy is the PositionOFy of this object - */ - - public void setPositionOFy(int positionOFy) { - this.positionOFy = positionOFy; - } - - /**. - * getter for PositionOFy - */ - - public int getPositionOFy() { - return positionOFy; - } - - /**. - * update for this object's components. - */ - - public void update() { - for (Component component : componentArrayList) { - component.update(this); - } - } -} diff --git a/component/src/main/java/com/iluwatar/component/GraphicsComponent.java b/component/src/main/java/com/iluwatar/component/GraphicsComponent.java deleted file mode 100644 index b7ca526a44fd..000000000000 --- a/component/src/main/java/com/iluwatar/component/GraphicsComponent.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.iluwatar.component; - -/**. - * GraphicsComponent is an interface for Graphics function - */ - -public interface GraphicsComponent extends Component { void update(GameObject gameObject); -} diff --git a/component/src/main/java/com/iluwatar/component/InputComponent.java b/component/src/main/java/com/iluwatar/component/InputComponent.java deleted file mode 100644 index 0daf137b9211..000000000000 --- a/component/src/main/java/com/iluwatar/component/InputComponent.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.iluwatar.component; - -/**. - * InputComponent is an interface for Input function - */ -public interface InputComponent extends Component { void update(GameObject gameObject); -} diff --git a/component/src/main/java/com/iluwatar/component/PhysicsComponent.java b/component/src/main/java/com/iluwatar/component/PhysicsComponent.java deleted file mode 100644 index afa84ca802e4..000000000000 --- a/component/src/main/java/com/iluwatar/component/PhysicsComponent.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.iluwatar.component; - -/**. - * PhysicsComponent is an interface for Physics function - */ -public interface PhysicsComponent extends Component { void update(GameObject gameObject); -} diff --git a/component/src/test/java/com/iluwatar/component/UpdateTest.java b/component/src/test/java/com/iluwatar/component/UpdateTest.java deleted file mode 100644 index c7a6c29d73d4..000000000000 --- a/component/src/test/java/com/iluwatar/component/UpdateTest.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.iluwatar.component; - -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; - -import static org.junit.jupiter.api.Assertions.assertEquals; - - -/** - * Tests that Component example runs without errors. - */ - -public class UpdateTest { - /** - * test for the update for the input component of the object - */ - @Test - public void inputUpdateTest() { - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFy(12); - gameObject.setPositionOFx(13); - gameObject.setVelocity(1); - gameObject.update(); - assertEquals(14, gameObject.getPositionOFx()); - assertEquals(13,gameObject.getPositionOFy()); - } - /** - * test for the update for the Physics component of the object - */ - @Test - public void physicsUpdateTest() { - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFy(13); - gameObject.setPositionOFx(12); - gameObject.setVelocity(1); - gameObject.update(); - assertEquals(13, gameObject.getPositionOFx()); - assertEquals(14,gameObject.getPositionOFy()); - } - /** - * test for the update for the Graphics component of the object - */ - @Test - public void graphicsUpdateTest() { - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFy(1); - gameObject.setPositionOFx(1); - gameObject.setVelocity(1); - gameObject.update(); - assertEquals(2, gameObject.getPositionOFx()); - assertEquals(2,gameObject.getPositionOFy()); - } - /** - * test for the setPositionOFx - */ - @Test - public void setPositionOFxTest(){ - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFx(1); - assertEquals(1,gameObject.getPositionOFx()); - } - /** - * test for the getPositionOFx - */ - @Test - public void getPositionOFxTest(){ - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFx(1); - assertEquals(1,gameObject.getPositionOFx()); - } - /** - * test for the setPositionOFy - */ - @Test - public void setPositionOFyTest(){ - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFy(1); - assertEquals(1,gameObject.getPositionOFy()); - } - /** - * test for the getPositionOFy - */ - @Test - public void getPositionOFyTest(){ - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setPositionOFy(1); - assertEquals(1,gameObject.getPositionOFy()); - } - /** - * test for the setVelocity - */ - @Test - public void setVelocityTest(){ - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setVelocity(1); - assertEquals(1,gameObject.getVelocity()); - } - /** - * test for the getVelocity - */ - @Test - public void getVelocityTest(){ - var arrayList = new ArrayList(); - arrayList.add(new BjornInputComponent()); - arrayList.add(new BjornPhysicsComponent()); - arrayList.add(new BjornGraphicsComponent()); - var gameObject = new GameObject(arrayList); - gameObject.setVelocity(1); - assertEquals(1,gameObject.getVelocity()); - } -} diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/module-info.java b/factory-method/src/main/java/module-info.java similarity index 100% rename from factory-method/src/main/java/com/iluwatar/factory/method/module-info.java rename to factory-method/src/main/java/module-info.java diff --git a/feature-toggle/src/main/java/com/iluwatar/featuretoggle/module-info.java b/feature-toggle/src/main/java/module-info.java similarity index 100% rename from feature-toggle/src/main/java/com/iluwatar/featuretoggle/module-info.java rename to feature-toggle/src/main/java/module-info.java diff --git a/finite-state-machine/README.md b/finite-state-machine/README.md new file mode 100644 index 000000000000..62e132519678 --- /dev/null +++ b/finite-state-machine/README.md @@ -0,0 +1,25 @@ +--- +layout: pattern +title: Finite state machine +folder: Finite-state-machine +permalink: /patterns/Finite-state-machine/ +categories: Behavioral +tags: + - Decoupling +--- + +## Intent +It represents a mathematical model of a finite number of states and their transitions and actions, +and is an efficient programming method within a logical unit. Make the program logic clear and easy +to understand. + + +## Applicability +Use the Finite-state-machine pattern when + +* There are finite state for our model or software. + + +## Credits + +* [State Machine Design Pattern](http://community.wvu.edu/~hhammar//rts/adv%20rts/statecharts%20patterns%20papers%20and%20%20examples/paper%20on%20state%20pattern%20B31-full.pdf) diff --git a/finite-state-machine/etc/finite-state-machine.urm.puml b/finite-state-machine/etc/finite-state-machine.urm.puml new file mode 100644 index 000000000000..597f2de95f02 --- /dev/null +++ b/finite-state-machine/etc/finite-state-machine.urm.puml @@ -0,0 +1,66 @@ +@startuml +package com.iluwatar.finite.state.machine { + class App { + + App() + + main(args : String[]) {static} + } + class Context { + - state : State + + Context(state : State) + + getState() : State + + request(event : Event) + + setState(state : State) + } + class CoolState { + + CoolState() + - Cool() + - Stop() + + handle(context : Context, event : Event) + } + enum Event { + + MOVING_ON_OFF {static} + + MOVING_RUN {static} + + valueOf(name : String) : Event {static} + + values() : Event[] {static} + } + class OffState { + + OffState() + - Start() + + handle(context : Context, event : Event) + } + class OnState { + + OnState() + - Cool() + - Stop() + + handle(context : Context, event : Event) + } + class RunState { + + RunState() + - cool_down() + + handle(context : Context, event : Event) + - stop() + } + class StandState { + + StandState() + + handle(context : Context, event : Event) + - start_running() + - start_walking() + } + interface State { + + handle(Context, Event) {abstract} + } + class WalkState { + + WalkState() + + handle(context : Context, event : Event) + - start_running() + - stop() + } +} +Context --> "-state" State +CoolState ..|> State +OffState ..|> State +OnState ..|> State +RunState ..|> State +StandState ..|> State +WalkState ..|> State +@enduml \ No newline at end of file diff --git a/finite-state-machine/pom.xml b/finite-state-machine/pom.xml new file mode 100644 index 000000000000..0cab1ad991ee --- /dev/null +++ b/finite-state-machine/pom.xml @@ -0,0 +1,45 @@ + + + + + java-design-patterns + com.iluwatar + 1.23.0-SNAPSHOT + + 4.0.0 + + finite-state-machine + + + org.junit.jupiter + junit-jupiter-engine + test + + + + \ No newline at end of file diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/App.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/App.java new file mode 100644 index 000000000000..57f1a181a43a --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/App.java @@ -0,0 +1,52 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * This model named finite state machine model, which uses finite state machine to + * implement one thing that pull out all the states, events and actions, and manage + * the complex state migration logic uniformly. + *

+ * In this demo, I create a scenario that there is a game character, who has three states. + * States: Standing;Walking;Running + * And also there are two events, which means you can create requests to control it. + * Events: MOVING_ON;MOVING_OFF + * In this demo I use state pattern to implement this FSM pattern. For the meaning of every class, + * refer to the class's Javadoc. + */ +public class App { + /**. + * Launcher of this demo. + * @param args is default for main method. + */ + public static void main(String[]args) { + var initState = new StandState(); + var context = new Context(initState); + + context.request(Event.MOVING_ON_OFF); + context.request(Event.MOVING_RUN); + context.request(Event.MOVING_ON_OFF); + context.request(Event.MOVING_ON_OFF); + } +} diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Context.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Context.java new file mode 100644 index 000000000000..e4a0fa391bde --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Context.java @@ -0,0 +1,64 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * This class is a context for the State. + * It defines the State that we focus on. + */ +public class Context { + private State state; + + /**. + * Constructor for Context. + * @param state is state data. + */ + public Context(State state) { + setState(state); + } + + /**. + * getter for state. + * @return state of this context. + */ + public State getState() { + return state; + } + + /**. + * setter for state. + * @param state is a state data + */ + public void setState(State state) { + this.state = state; + } + + /**. + * request method for an event, then it will call handle method. + * @param event is an event. + */ + public void request(Event event) { + state.handle(this,event); + } +} diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Event.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Event.java new file mode 100644 index 000000000000..c7dc15a9f790 --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/Event.java @@ -0,0 +1,34 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * This enum event is the event for the game character. + */ +public enum Event { + //Event for the game character moving or stop moving + MOVING_ON_OFF, + //Event for the game character run. + MOVING_RUN +} diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/RunState.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/RunState.java new file mode 100644 index 000000000000..004a1477de3e --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/RunState.java @@ -0,0 +1,64 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * A running state for the game character. + */ +public class RunState implements State { + /**. + * This method is a handler for event. + * @param context is context data. + * @param event is an event. + */ + @Override + public void handle(Context context, Event event) { + switch (event) { + case MOVING_ON_OFF: + context.setState(new StandState()); + stop(); + break; + case MOVING_RUN: + context.setState(new WalkState()); + cool_down(); + break; + default: + break; + } + } + + /**. + * A method which simulates Game character stop. + */ + private void stop() { + System.out.println("Stop running! Game character is standing now!"); + } + + /**. + * A method which simulates Game character change from run to walk. + */ + private void cool_down() { + System.out.println("Stop running! Game character is walking now!"); + } +} diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/StandState.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/StandState.java new file mode 100644 index 000000000000..82dc4b3744a7 --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/StandState.java @@ -0,0 +1,64 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * A standing state for the game character. + */ +public class StandState implements State { + /**. + * This method is a handler for event. + * @param context is context data. + * @param event is an event. + */ + @Override + public void handle(Context context, Event event) { + switch (event) { + case MOVING_ON_OFF: + context.setState(new WalkState()); + start_walking(); + break; + case MOVING_RUN: + context.setState(new RunState()); + start_running(); + break; + default: + break; + } + } + + /**. + * A method which simulates Game character begin to walk. + */ + private void start_walking() { + System.out.println("Stop standing! Game character is walking now!"); + } + + /**. + * A method which simulates Game character begin to run. + */ + private void start_running() { + System.out.println("Stop standing! Game character is running now!"); + } +} diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/State.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/State.java new file mode 100644 index 000000000000..ecd20bd35f6e --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/State.java @@ -0,0 +1,37 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * This is an interface for State. Which encapsulates the behavior of a + * particular state of an Context object. + */ +public interface State { + /**. + * this method is logic processing for context and event. + * @param context is context data. + * @param event is an event. + */ + void handle(Context context, Event event); +} diff --git a/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/WalkState.java b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/WalkState.java new file mode 100644 index 000000000000..14edcb43ee35 --- /dev/null +++ b/finite-state-machine/src/main/java/com/iluwatar/finite/state/machine/WalkState.java @@ -0,0 +1,64 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.finite.state.machine; + +/**. + * A walking state for the game character. + */ +public class WalkState implements State { + /**. + * This method is a handler for event. + * @param context is context data. + * @param event is an event. + */ + @Override + public void handle(Context context, Event event) { + switch (event) { + case MOVING_ON_OFF: + context.setState(new StandState()); + stop(); + break; + case MOVING_RUN: + context.setState(new RunState()); + start_running(); + break; + default: + break; + } + } + + /**. + * A method which simulates Game character stop. + */ + private void stop() { + System.out.println("Stop walking! Game character is standing now!"); + } + + /**. + * A method which simulates Game character change from walk to run. + */ + private void start_running() { + System.out.println("Stop walking! Game character is running now!"); + } +} diff --git a/component/src/main/java/module-info.java b/finite-state-machine/src/main/java/module-info.java similarity index 93% rename from component/src/main/java/module-info.java rename to finite-state-machine/src/main/java/module-info.java index 50a21a12f15f..07b9fba315a0 100644 --- a/component/src/main/java/module-info.java +++ b/finite-state-machine/src/main/java/module-info.java @@ -1,26 +1,26 @@ -/* - * The MIT License - * Copyright © 2014-2019 Ilkka Seppälä - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -module com.iluwatar.component { - requires org.slf4j; -} +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +module com.iluwatar.finite.state.machine { + requires org.slf4j; +} \ No newline at end of file diff --git a/component/src/test/java/com/iluwatar/component/AppTest.java b/finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/AppTest.java similarity index 90% rename from component/src/test/java/com/iluwatar/component/AppTest.java rename to finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/AppTest.java index 04f86d81ffe5..427cc7ab57cb 100644 --- a/component/src/test/java/com/iluwatar/component/AppTest.java +++ b/finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/AppTest.java @@ -20,15 +20,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package com.iluwatar.component; +package com.iluwatar.finite.state.machine; import org.junit.jupiter.api.Test; -/** - * Tests that Component example runs without errors. +/**. + * Tests that demo runs without errors. */ public class AppTest { - /** - * test for the design pattern runnable. + /**. + * Test for main method. */ @Test public void test() { diff --git a/finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/StateTest.java b/finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/StateTest.java new file mode 100644 index 000000000000..02a235ebee0d --- /dev/null +++ b/finite-state-machine/src/test/java/com/iluwatar/finite/state/machine/StateTest.java @@ -0,0 +1,121 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.finite.state.machine; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/**. + * Tests that demo runs without errors. + */ +public class StateTest { + /**. + * Test for init + */ + @Test + public void init_stand_state(){ + var initState = new StandState(); + var context = new Context(initState); + assertEquals(initState.getClass(),context.getState().getClass()); + } + /**. + * Test for init + */ + @Test + public void init_walk_state(){ + var initState = new WalkState(); + var context = new Context(initState); + assertEquals(initState.getClass(),context.getState().getClass()); + } + /**. + * Test for init + */ + @Test + public void init_run_state(){ + var initState = new RunState(); + var context = new Context(initState); + assertEquals(initState.getClass(),context.getState().getClass()); + } + /**. + * Test for state change. + */ + @Test + public void stand_to_walk(){ + var initState = new StandState(); + var context = new Context(initState); + context.request(Event.MOVING_ON_OFF); + assertEquals(WalkState.class,context.getState().getClass()); + } + /**. + * Test for state change. + */ + @Test + public void walk_to_stand(){ + var initState = new WalkState(); + var context = new Context(initState); + context.request(Event.MOVING_ON_OFF); + assertEquals(StandState.class,context.getState().getClass()); + } + /**. + * Test for state change. + */ + @Test + public void stand_to_run(){ + var initState = new StandState(); + var context = new Context(initState); + context.request(Event.MOVING_RUN); + assertEquals(RunState.class,context.getState().getClass()); + } + /**. + * Test for state change. + */ + @Test + public void run_to_stand(){ + var initState = new RunState(); + var context = new Context(initState); + context.request(Event.MOVING_ON_OFF); + assertEquals(StandState.class,context.getState().getClass()); + } + /**. + * Test for state change. + */ + @Test + public void walk_to_run(){ + var initState = new WalkState(); + var context = new Context(initState); + context.request(Event.MOVING_RUN); + assertEquals(RunState.class,context.getState().getClass()); + } + /**. + * Test for state change. + */ + @Test + public void run_to_walk(){ + var initState = new RunState(); + var context = new Context(initState); + context.request(Event.MOVING_RUN); + assertEquals(WalkState.class,context.getState().getClass()); + } + +} diff --git a/pom.xml b/pom.xml index a6e3750524d6..bc1c2ee8cf29 100644 --- a/pom.xml +++ b/pom.xml @@ -185,7 +185,7 @@ update-method leader-followers arrange-act-assert - component + finite-state-machine