Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component pattern #3133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 15 additions & 65 deletions component/pom.xml
Original file line number Diff line number Diff line change
@@ -1,65 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 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.26.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>component</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<dependencies>
<!-- JUnit 5 Dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
62 changes: 0 additions & 62 deletions component/src/main/java/com/iluwatar/component/App.java

This file was deleted.

36 changes: 36 additions & 0 deletions component/src/main/java/com/iluwatar/component/FirstTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.iluwatar.component;
public class FirstTask implements Task {
private String title;

public FirstTask(String title) {
this.title = title;
}

@Override
public String getTitle() {
return title;
}

@Override
public void setTitle(String title) {
this.title = title;
}

@Override
public void addTask(Task task) {
// Not applicable for FirstTask, as it's a leaf node.
}

@Override
public void removeTask(Task task) {
// Not applicable for FirstTask, as it's a leaf node.
}

@Override
public void display() {
System.out.println("FirstTask: " + title);
}
}



117 changes: 0 additions & 117 deletions component/src/main/java/com/iluwatar/component/GameObject.java
Original file line number Diff line number Diff line change
@@ -1,118 +1 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 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 com.iluwatar.component.component.graphiccomponent.GraphicComponent;
import com.iluwatar.component.component.graphiccomponent.ObjectGraphicComponent;
import com.iluwatar.component.component.inputcomponent.DemoInputComponent;
import com.iluwatar.component.component.inputcomponent.InputComponent;
import com.iluwatar.component.component.inputcomponent.PlayerInputComponent;
import com.iluwatar.component.component.physiccomponent.ObjectPhysicComponent;
import com.iluwatar.component.component.physiccomponent.PhysicComponent;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
* The GameObject class has three component class instances that allow
* the creation of different game objects based on the game design requirements.
*/
@Getter
@RequiredArgsConstructor
public class GameObject {
private final InputComponent inputComponent;
private final PhysicComponent physicComponent;
private final GraphicComponent graphicComponent;

private final String name;
private int velocity = 0;
private int coordinate = 0;

/**
* Creates a player game object.
*
* @return player object
*/
public static GameObject createPlayer() {
return new GameObject(new PlayerInputComponent(),
new ObjectPhysicComponent(),
new ObjectGraphicComponent(),
"player");
}


/**
* Creates a NPC game object.
*
* @return npc object
*/
public static GameObject createNpc() {
return new GameObject(
new DemoInputComponent(),
new ObjectPhysicComponent(),
new ObjectGraphicComponent(),
"npc");
}

/**
* Updates the three components of the NPC object used in the demo in App.java
* note that this is simply a duplicate of update() without the key event for
* demonstration purposes.
*
* <p>This method is usually used in games if the player becomes inactive.
*/
public void demoUpdate() {
inputComponent.update(this, 0);
physicComponent.update(this);
graphicComponent.update(this);
}

/**
* Updates the three components for objects based on key events.
*
* @param e key event from the player.
*/
public void update(int e) {
inputComponent.update(this, e);
physicComponent.update(this);
graphicComponent.update(this);
}

/**
* Update the velocity based on the acceleration of the GameObject.
*
* @param acceleration the acceleration of the GameObject
*/
public void updateVelocity(int acceleration) {
this.velocity += acceleration;
}


/**
* Set the c based on the current velocity.
*/
public void updateCoordinate() {
this.coordinate += this.velocity;
}
}
10 changes: 10 additions & 0 deletions component/src/main/java/com/iluwatar/component/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.iluwatar.component;

public interface Task {
String getTitle();
void setTitle(String title);
void addTask(Task task);
void removeTask(Task task);
void display();
}

14 changes: 14 additions & 0 deletions component/src/main/java/com/iluwatar/component/TaskFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.iluwatar.component;

public class TaskFactory {

// Creates and returns a simple task
public static Task createFirstTask(String title) {
return new FirstTask(title);
}

// Creates and returns a task list
public static TaskList createTaskList(String title) {
return new TaskList(title);
}
}
46 changes: 46 additions & 0 deletions component/src/main/java/com/iluwatar/component/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.iluwatar.component;
import java.util.ArrayList;
import java.util.List;

public class TaskList implements Task {
private String title;
private final List<Task> tasks;

public TaskList(String title) {
this.title = title;
this.tasks = new ArrayList<>();
}

@Override
public String getTitle() {
return title;
}

@Override
public void setTitle(String title) {
this.title = title;
}

@Override
public void addTask(Task task) {
tasks.add(task);
}

@Override
public void removeTask(Task task) {
tasks.remove(task);
}

@Override
public void display() {
System.out.println("Task List: " + title);
for (Task task : tasks) {
task.display();
}
}

// Getter method for testing
public List<Task> getTasks() {
return tasks;
}
}
Loading
Loading