delete unwanted files

上级 1c70cdc3
---
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]
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="CheckStyle-IDEA-Module">
<option name="configuration">
<map />
</option>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.5.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.5.2" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
</component>
</module>
\ No newline at end of file
@startuml
@enduml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>component</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven assembly plugin is invoked with default setting which we have
in parent pom and specifying the class having main method -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.component.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
/*
* 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<Component>();
arrayList.add(new BjornInputComponent());
arrayList.add(new BjornPhysicsComponent());
arrayList.add(new BjornGraphicsComponent());
var gameObject = new GameObject(arrayList);
gameObject.update();
}
}
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());
}
}
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());
}
}
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!");
}
}
}
package com.iluwatar.component;
/**
* Component is an interface for all component.
*/
public interface Component { void update(GameObject gameObject);
}
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<Component> componentArrayList;
/**
* Constructor for GameObject
* @param componentArrayList is the list of this object contains
*/
public GameObject(ArrayList<Component> 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);
}
}
}
package com.iluwatar.component;
/**
* GraphicsComponent is an interface for Graphics function
*/
public interface GraphicsComponent extends Component { void update(GameObject gameObject);
}
package com.iluwatar.component;
/**
* InputComponent is an interface for Input function
*/
public interface InputComponent extends Component { void update(GameObject gameObject);
}
package com.iluwatar.component;
/**
* PhysicsComponent is an interface for Physics function
*/
public interface PhysicsComponent extends Component { void update(GameObject gameObject);
}
/*
* 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.
*/
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[]{});
}
}
/*
* 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<Component>();
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<Component>();
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<Component>();
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());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册