diff --git a/component/src/main/java/com/iluwater/component/App.java b/component/src/main/java/com/iluwater/component/App.java index 8b1378917..e3ba8c0a3 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 000000000..20f58b53b --- /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 000000000..e6b58b7dc --- /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 000000000..137233392 --- /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 000000000..af22356b3 --- /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 000000000..3520d0730 --- /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 000000000..ed358ed45 --- /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 000000000..f2f7669d1 --- /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 000000000..c65ceb472 --- /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); +}