Update Method pattern (#1106)

* Add update method pattern

* Add unit tests

* Add README.md

* Resolve conflict
This commit is contained in:
Azureyjt 2019-12-01 21:06:19 +08:00 committed by Ilkka Seppälä
parent 05e582ca3e
commit 55769e9841
12 changed files with 1167 additions and 477 deletions

955
pom.xml
View File

@ -15,172 +15,173 @@
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. -->
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<packaging>pom</packaging>
<inceptionYear>2014-2019</inceptionYear>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.2.18.Final</hibernate.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring-boot.version>2.0.9.RELEASE</spring-boot.version>
<spring-data.version>2.0.14.RELEASE</spring-data.version>
<h2.version>1.4.190</h2.version>
<junit.version>4.12</junit.version>
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<junit-vintage.version>${junit.version}.2</junit-vintage.version>
<sping-test-junit5.version>1.0.2</sping-test-junit5.version>
<compiler.version>3.8.1</compiler.version>
<jacoco.version>0.8.4</jacoco.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<camel.version>2.24.0</camel.version>
<guava.version>19.0</guava.version>
<mockito.version>1.10.19</mockito.version>
<htmlunit.version>2.22</htmlunit.version>
<guice.version>4.0</guice.version>
<mongo-java-driver.version>3.3.0</mongo-java-driver.version>
<slf4j.version>1.7.28</slf4j.version>
<logback.version>1.2.3</logback.version>
<aws-lambda-core.version>1.1.0</aws-lambda-core.version>
<aws-java-sdk-dynamodb.version>1.11.289</aws-java-sdk-dynamodb.version>
<aws-lambda-java-events.version>2.0.1</aws-lambda-java-events.version>
<jackson.version>2.8.5</jackson.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<jaxb-impl.version>2.3.2</jaxb-impl.version>
<annotation-api.version>1.3.2</annotation-api.version>
<system-rules.version>1.19.0</system-rules.version>
</properties>
<modules>
<module>abstract-factory</module>
<module>tls</module>
<module>builder</module>
<module>factory-method</module>
<module>prototype</module>
<module>singleton</module>
<module>adapter</module>
<module>bridge</module>
<module>composite</module>
<module>dao</module>
<module>data-mapper</module>
<module>decorator</module>
<module>facade</module>
<module>flyweight</module>
<module>proxy</module>
<module>chain</module>
<module>command</module>
<module>interpreter</module>
<module>iterator</module>
<module>mediator</module>
<module>memento</module>
<module>model-view-presenter</module>
<module>observer</module>
<module>state</module>
<module>strategy</module>
<module>template-method</module>
<module>visitor</module>
<module>double-checked-locking</module>
<module>servant</module>
<module>service-locator</module>
<module>null-object</module>
<module>event-aggregator</module>
<module>callback</module>
<module>execute-around</module>
<module>property</module>
<module>intercepting-filter</module>
<module>producer-consumer</module>
<module>pipeline</module>
<module>poison-pill</module>
<module>reader-writer-lock</module>
<module>lazy-loading</module>
<module>service-layer</module>
<module>specification</module>
<module>tolerant-reader</module>
<module>model-view-controller</module>
<module>flux</module>
<module>double-dispatch</module>
<module>multiton</module>
<module>resource-acquisition-is-initialization</module>
<module>thread-pool</module>
<module>twin</module>
<module>private-class-data</module>
<module>object-pool</module>
<module>dependency-injection</module>
<module>naked-objects</module>
<module>front-controller</module>
<module>repository</module>
<module>async-method-invocation</module>
<module>monostate</module>
<module>step-builder</module>
<module>business-delegate</module>
<module>half-sync-half-async</module>
<module>layers</module>
<module>eip-message-channel</module>
<module>fluentinterface</module>
<module>reactor</module>
<module>caching</module>
<module>eip-publish-subscribe</module>
<module>delegation</module>
<module>event-driven-architecture</module>
<module>api-gateway</module>
<module>factory-kit</module>
<module>feature-toggle</module>
<module>value-object</module>
<module>module</module>
<module>monad</module>
<module>mute-idiom</module>
<module>mutex</module>
<module>semaphore</module>
<module>hexagonal</module>
<module>abstract-document</module>
<module>aggregator-microservices</module>
<module>promise</module>
<module>page-object</module>
<module>event-asynchronous</module>
<module>event-queue</module>
<module>queue-load-leveling</module>
<module>object-mother</module>
<module>data-bus</module>
<module>converter</module>
<module>guarded-suspension</module>
<module>balking</module>
<module>extension-objects</module>
<module>marker</module>
<module>cqrs</module>
<module>event-sourcing</module>
<module>data-transfer-object</module>
<module>throttling</module>
<module>unit-of-work</module>
<module>partial-response</module>
<module>eip-wire-tap</module>
<module>eip-splitter</module>
<module>eip-aggregator</module>
<module>retry</module>
<module>dirty-flag</module>
<module>trampoline</module>
<module>serverless</module>
<module>ambassador</module>
<module>acyclic-visitor</module>
<module>collection-pipeline</module>
<module>master-worker-pattern</module>
<module>spatial-partition</module>
<module>priority-queue</module>
<module>commander</module>
<module>typeobjectpattern</module>
<module>bytecode</module>
<module>leader-election</module>
<module>data-locality</module>
<module>subclass-sandbox</module>
<module>circuit-breaker</module>
<module>role-object</module>
<module>saga</module>
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<packaging>pom</packaging>
<inceptionYear>2014-2019</inceptionYear>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.2.18.Final</hibernate.version>
<spring.version>5.0.13.RELEASE</spring.version>
<spring-boot.version>2.0.9.RELEASE</spring-boot.version>
<spring-data.version>2.0.14.RELEASE</spring-data.version>
<h2.version>1.4.190</h2.version>
<junit.version>4.12</junit.version>
<junit-jupiter.version>5.5.2</junit-jupiter.version>
<junit-vintage.version>${junit.version}.2</junit-vintage.version>
<sping-test-junit5.version>1.0.2</sping-test-junit5.version>
<compiler.version>3.8.1</compiler.version>
<jacoco.version>0.8.4</jacoco.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<camel.version>2.24.0</camel.version>
<guava.version>19.0</guava.version>
<mockito.version>1.10.19</mockito.version>
<htmlunit.version>2.22</htmlunit.version>
<guice.version>4.0</guice.version>
<mongo-java-driver.version>3.3.0</mongo-java-driver.version>
<slf4j.version>1.7.28</slf4j.version>
<logback.version>1.2.3</logback.version>
<aws-lambda-core.version>1.1.0</aws-lambda-core.version>
<aws-java-sdk-dynamodb.version>1.11.289</aws-java-sdk-dynamodb.version>
<aws-lambda-java-events.version>2.0.1</aws-lambda-java-events.version>
<jackson.version>2.8.5</jackson.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<jaxb-impl.version>2.3.2</jaxb-impl.version>
<annotation-api.version>1.3.2</annotation-api.version>
<system-rules.version>1.19.0</system-rules.version>
</properties>
<modules>
<module>abstract-factory</module>
<module>tls</module>
<module>builder</module>
<module>factory-method</module>
<module>prototype</module>
<module>singleton</module>
<module>adapter</module>
<module>bridge</module>
<module>composite</module>
<module>dao</module>
<module>data-mapper</module>
<module>decorator</module>
<module>facade</module>
<module>flyweight</module>
<module>proxy</module>
<module>chain</module>
<module>command</module>
<module>interpreter</module>
<module>iterator</module>
<module>mediator</module>
<module>memento</module>
<module>model-view-presenter</module>
<module>observer</module>
<module>state</module>
<module>strategy</module>
<module>template-method</module>
<module>visitor</module>
<module>double-checked-locking</module>
<module>servant</module>
<module>service-locator</module>
<module>null-object</module>
<module>event-aggregator</module>
<module>callback</module>
<module>execute-around</module>
<module>property</module>
<module>intercepting-filter</module>
<module>producer-consumer</module>
<module>pipeline</module>
<module>poison-pill</module>
<module>reader-writer-lock</module>
<module>lazy-loading</module>
<module>service-layer</module>
<module>specification</module>
<module>tolerant-reader</module>
<module>model-view-controller</module>
<module>flux</module>
<module>double-dispatch</module>
<module>multiton</module>
<module>resource-acquisition-is-initialization</module>
<module>thread-pool</module>
<module>twin</module>
<module>private-class-data</module>
<module>object-pool</module>
<module>dependency-injection</module>
<module>naked-objects</module>
<module>front-controller</module>
<module>repository</module>
<module>async-method-invocation</module>
<module>monostate</module>
<module>step-builder</module>
<module>business-delegate</module>
<module>half-sync-half-async</module>
<module>layers</module>
<module>eip-message-channel</module>
<module>fluentinterface</module>
<module>reactor</module>
<module>caching</module>
<module>eip-publish-subscribe</module>
<module>delegation</module>
<module>event-driven-architecture</module>
<module>api-gateway</module>
<module>factory-kit</module>
<module>feature-toggle</module>
<module>value-object</module>
<module>module</module>
<module>monad</module>
<module>mute-idiom</module>
<module>mutex</module>
<module>semaphore</module>
<module>hexagonal</module>
<module>abstract-document</module>
<module>aggregator-microservices</module>
<module>promise</module>
<module>page-object</module>
<module>event-asynchronous</module>
<module>event-queue</module>
<module>queue-load-leveling</module>
<module>object-mother</module>
<module>data-bus</module>
<module>converter</module>
<module>guarded-suspension</module>
<module>balking</module>
<module>extension-objects</module>
<module>marker</module>
<module>cqrs</module>
<module>event-sourcing</module>
<module>data-transfer-object</module>
<module>throttling</module>
<module>unit-of-work</module>
<module>partial-response</module>
<module>eip-wire-tap</module>
<module>eip-splitter</module>
<module>eip-aggregator</module>
<module>retry</module>
<module>dirty-flag</module>
<module>trampoline</module>
<module>serverless</module>
<module>ambassador</module>
<module>acyclic-visitor</module>
<module>collection-pipeline</module>
<module>master-worker-pattern</module>
<module>spatial-partition</module>
<module>priority-queue</module>
<module>commander</module>
<module>typeobjectpattern</module>
<module>bytecode</module>
<module>leader-election</module>
<module>data-locality</module>
<module>subclass-sandbox</module>
<module>circuit-breaker</module>
<module>role-object</module>
<module>saga</module>
<module>double-buffer</module>
<module>sharding</module>
<module>game-loop</module>
<module>combinator</module>
<module>combinator</module>
<module>update-method</module>
</modules>
<repositories>
@ -190,323 +191,323 @@
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.sbrannen</groupId>
<artifactId>spring-test-junit5</artifactId>
<version>${sping-test-junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>${htmlunit.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongo-java-driver.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${annotation-api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb-impl.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.25.0-GA</version>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>${system-rules.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.sbrannen</groupId>
<artifactId>spring-test-junit5</artifactId>
<version>${sping-test-junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>${htmlunit.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongo-java-driver.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${annotation-api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb-impl.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.25.0-GA</version>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>${system-rules.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.version}</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>-Xmx1024M ${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
<!-- Maven assembly plugin template for all child project to follow -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- below two line make sure the fat jar is sharing the same name
as of project name -->
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.version}</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>-Xmx1024M ${argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
<!-- Maven assembly plugin template for all child project to follow -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- below two line make sure the fat jar is sharing the same name
as of project name -->
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>validate</id>
<goals>
<goal>check</goal>
</goals>
<phase>validate</phase>
<configuration>
<configLocation>google_checks.xml</configLocation>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
<encoding>UTF-8</encoding>
<failOnViolation>true</failOnViolation>
<violationSeverity>warning</violationSeverity>
<includeTestSourceDirectory>false</includeTestSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>validate</id>
<goals>
<goal>check</goal>
</goals>
<phase>validate</phase>
<configuration>
<configLocation>google_checks.xml</configLocation>
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
<encoding>UTF-8</encoding>
<failOnViolation>true</failOnViolation>
<violationSeverity>warning</violationSeverity>
<includeTestSourceDirectory>false</includeTestSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- Resolves ${projectRoot} to the project root directory. Used by the
license-maven-plugin to find the correct location of license-plugin-header-style.xml
when built from a submodule directory. -->
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
<version>0.3.1</version>
<executions>
<execution>
<id>directories</id>
<goals>
<goal>directory-of</goal>
</goals>
<phase>initialize</phase>
<configuration>
<property>projectRoot</property>
<project>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
</project>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<header>com/mycila/maven/plugin/license/templates/MIT.txt</header>
<properties>
<owner>Ilkka Seppälä</owner>
</properties>
<skipExistingHeaders>true</skipExistingHeaders>
<headerDefinitions>
<headerDefinition>${projectRoot}${file.separator}license-plugin-header-style.xml</headerDefinition>
</headerDefinitions>
<mapping>
<java>SLASHSTAR_CUSTOM_STYLE</java>
</mapping>
<excludes>
<exclude>.github/FUNDING.yml</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>install-format</id>
<phase>install</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Resolves ${projectRoot} to the project root directory. Used by the
license-maven-plugin to find the correct location of license-plugin-header-style.xml
when built from a submodule directory. -->
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
<version>0.3.1</version>
<executions>
<execution>
<id>directories</id>
<goals>
<goal>directory-of</goal>
</goals>
<phase>initialize</phase>
<configuration>
<property>projectRoot</property>
<project>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
</project>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<header>com/mycila/maven/plugin/license/templates/MIT.txt</header>
<properties>
<owner>Ilkka Seppälä</owner>
</properties>
<skipExistingHeaders>true</skipExistingHeaders>
<headerDefinitions>
<headerDefinition>${projectRoot}${file.separator}license-plugin-header-style.xml</headerDefinition>
</headerDefinitions>
<mapping>
<java>SLASHSTAR_CUSTOM_STYLE</java>
</mapping>
<excludes>
<exclude>.github/FUNDING.yml</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>install-format</id>
<phase>install</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</reporting>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</reporting>
</project>
</project>

35
update-method/README.md Normal file
View File

@ -0,0 +1,35 @@
---
layout: pattern
title: Update Method
folder: update-method
permalink: /patterns/update-method/
categories: Other
tags:
- Java
- Difficulty-Beginner
---
## Intent
Update method pattern simulates a collection of independent objects by telling each to process one frame of behavior at a time.
## Applicability
If the Game Loop pattern is the best thing since sliced bread, then the Update Method pattern is its butter. A wide swath of games featuring live entities that the player interacts with use this pattern in some form or other. If the game has space marines, dragons, Martians, ghosts, or athletes, theres a good chance it uses this pattern.
However, if the game is more abstract and the moving pieces are less like living actors and more like pieces on a chessboard, this pattern is often a poor fit. In a game like chess, you dont need to simulate all of the pieces concurrently, and you probably dont need to tell the pawns to update themselves every frame.
Update methods work well when:
- Your game has a number of objects or systems that need to run simultaneously.
- Each objects behavior is mostly independent of the others.
- The objects need to be simulated over time.
## Explanation
The game world maintains a collection of objects. Each object implements an update method that simulates one frame of the objects behavior. Each frame, the game updates every object in the collection.
To learn more about how the game loop runs and when the update methods are invoked, please refer to Game Loop Pattern.
## Credits
* [Game Programming Patterns - Update Method](http://gameprogrammingpatterns.com/update-method.html)

44
update-method/pom.xml Normal file
View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<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>update-method</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,61 @@
/*
* 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.updatemethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This pattern simulate a collection of independent objects by telling each to
* process one frame of behavior at a time. The game world maintains a collection
* of objects. Each object implements an update method that simulates one frame of
* the objects behavior. Each frame, the game updates every object in the collection.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private static final int GAME_RUNNING_TIME = 2000;
/**
* Program entry point.
* @param args runtime arguments
*/
public static void main(String[] args) {
try {
var world = new World();
var skeleton1 = new Skeleton(1, 10);
var skeleton2 = new Skeleton(2, 70);
var statue = new Statue(3, 20);
world.addEntity(skeleton1);
world.addEntity(skeleton2);
world.addEntity(statue);
world.run();
Thread.sleep(GAME_RUNNING_TIME);
world.stop();
} catch (InterruptedException e) {
LOGGER.error(e.getMessage());
}
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.updatemethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract class for all the entity types.
*/
public abstract class Entity {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
protected int id;
protected int position;
public Entity(int id) {
this.id = id;
this.position = 0;
}
public abstract void update();
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}

View File

@ -0,0 +1,78 @@
/*
* 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.updatemethod;
/**
* Skeletons are always patrolling on the game map. Initially all the skeletons
* patrolling to the right, and after them reach the bounding, it will start
* patrolling to the left. For each frame, one skeleton will move 1 position
* step.
*/
public class Skeleton extends Entity {
private static final int PATROLLING_LEFT_BOUNDING = 0;
private static final int PATROLLING_RIGHT_BOUNDING = 100;
protected boolean patrollingLeft;
/**
* Constructor of Skeleton.
*
* @param id id of skeleton
*/
public Skeleton(int id) {
super(id);
patrollingLeft = false;
}
/**
* Constructor of Skeleton.
*
* @param id id of skeleton
* @param postition position of skeleton
*/
public Skeleton(int id, int postition) {
super(id);
this.position = position;
patrollingLeft = false;
}
@Override
public void update() {
if (patrollingLeft) {
position -= 1;
if (position == PATROLLING_LEFT_BOUNDING) {
patrollingLeft = false;
}
} else {
position += 1;
if (position == PATROLLING_RIGHT_BOUNDING) {
patrollingLeft = true;
}
}
logger.info("Skeleton " + id + " is on position " + position + ".");
}
}

View File

@ -0,0 +1,69 @@
/*
* 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.updatemethod;
/**
* Statues shoot lightning at regular intervals.
*/
public class Statue extends Entity {
protected int frames;
protected int delay;
/**
* Constructor of Statue.
*
* @param id id of statue
*/
public Statue(int id) {
super(id);
this.frames = 0;
this.delay = 0;
}
/**
* Constructor of Statue.
*
* @param id id of statue
* @param delay the number of frames between two lightning
*/
public Statue(int id, int delay) {
super(id);
this.frames = 0;
this.delay = delay;
}
@Override
public void update() {
if (++ frames == delay) {
shootLightning();
frames = 0;
}
}
private void shootLightning() {
logger.info("Statue " + id + " shoots lightning!");
}
}

View File

@ -0,0 +1,114 @@
/*
* 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.updatemethod;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The game world class. Maintain all the objects existed in the game frames.
*/
public class World {
private static final Logger LOGGER = LoggerFactory.getLogger(World.class);
protected List<Entity> entities;
protected volatile boolean isRunning;
public World() {
entities = new ArrayList<>();
isRunning = false;
}
/**
* Main game loop. This loop will always run until the game is over. For
* each loop it will process user input, update internal status, and render
* the next frames. For more detail please refer to the game-loop pattern.
*/
private void gameLoop() {
while (isRunning) {
processInput();
update();
render();
}
}
/**
* Handle any user input that has happened since the last call. In order to
* simulate the situation in real-life game, here we add a random time lag.
* The time lag ranges from 50 ms to 250 ms.
*/
private void processInput() {
try {
int lag = new Random().nextInt(200) + 50;
Thread.sleep(lag);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage());
}
}
/**
* Update internal status. The update method pattern invoke udpate method for
* each entity in the game.
*/
private void update() {
for (var entity : entities) {
entity.update();
}
}
/**
* Render the next frame. Here we do nothing since it is not related to the
* pattern.
*/
private void render() {}
/**
* Run game loop.
*/
public void run() {
LOGGER.info("Start game.");
isRunning = true;
var thread = new Thread(this::gameLoop);
thread.start();
}
/**
* Stop game loop.
*/
public void stop() {
LOGGER.info("Stop game.");
isRunning = false;
}
public void addEntity(Entity entity) {
entities.add(entity);
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.updatemethod;
import org.junit.Test;
public class AppTest {
@Test
public void testMain() {
String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,78 @@
/*
* 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.updatemethod;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class SkeletonTest {
private Skeleton skeleton;
@Before
public void setup() {
skeleton = new Skeleton(1);
}
@After
public void tearDown() {
skeleton = null;
}
@Test
public void testUpdateForPatrollingLeft() {
skeleton.patrollingLeft = true;
skeleton.setPosition(50);
skeleton.update();
Assert.assertEquals(49, skeleton.getPosition());
}
@Test
public void testUpdateForPatrollingRight() {
skeleton.patrollingLeft = false;
skeleton.setPosition(50);
skeleton.update();
Assert.assertEquals(51, skeleton.getPosition());
}
@Test
public void testUpdateForReverseDirectionFromLeftToRight() {
skeleton.patrollingLeft = true;
skeleton.setPosition(1);
skeleton.update();
Assert.assertEquals(0, skeleton.getPosition());
Assert.assertEquals(false, skeleton.patrollingLeft);
}
@Test
public void testUpdateForReverseDirectionFromRightToLeft() {
skeleton.patrollingLeft = false;
skeleton.setPosition(99);
skeleton.update();
Assert.assertEquals(100, skeleton.getPosition());
Assert.assertEquals(true, skeleton.patrollingLeft);
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.updatemethod;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class StatueTest {
private Statue statue;
@Before
public void setup() {
statue = new Statue(1, 20);
}
@After
public void tearDown() {
statue = null;
}
@Test
public void testUpdateForPendingShoot() {
statue.frames = 10;
statue.update();
Assert.assertEquals(11, statue.frames);
}
@Test
public void testUpdateForShooting() {
statue.frames = 19;
statue.update();
Assert.assertEquals(0, statue.frames);
}
}

View File

@ -0,0 +1,63 @@
/*
* 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.updatemethod;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class WorldTest {
private World world;
@Before
public void setup() {
world = new World();
}
@After
public void tearDown() {
world = null;
}
@Test
public void testRun() {
world.run();
Assert.assertEquals(true, world.isRunning);
}
@Test
public void testStop() {
world.stop();
Assert.assertEquals(false, world.isRunning);
}
@Test
public void testAddEntity() {
var entity = new Skeleton(1);
world.addEntity(entity);
Assert.assertEquals(entity, world.entities.get(0));
}
}