Skip to content

Commit

Permalink
WIP. Example of visitor pattern
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Sep 20, 2024
1 parent f314ddf commit 0359e7d
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.snowdrop.visitorpattern;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class Action {
private String name;
private String description;
private List<Step> steps;

public Action(String name, String description) {
this.name = name;
this.description = description;
}
}
14 changes: 14 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/Configurator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.snowdrop.visitorpattern;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Configurator {
private String name;
private String provider;
private String resourceType;
private String description;
private Job job;
}
14 changes: 14 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/Job.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.snowdrop.visitorpattern;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class Job {
private List<Action> actions;

public Job() {}
}
19 changes: 19 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/JobFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.snowdrop.visitorpattern;

import java.util.List;
import java.util.stream.Collectors;

public class JobFactory {
public static Visitable createResource(Configurator config) {
if (config.getResourceType().equalsIgnoreCase("PipelineRun")) {
return new PipelineRun(config);
} else if (config.getResourceType().equalsIgnoreCase("TaskRun")) {
// Convert Actions to Steps
List<Step> steps = config.getJob().getActions().stream()
.map(action -> new Step(action.getName()))
.collect(Collectors.toList());
return new TaskRun(config.getName(), steps);
}
throw new IllegalArgumentException("Unknown resource type: " + config.getResourceType());
}
}
29 changes: 29 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.snowdrop.visitorpattern;

import java.util.Arrays;
import java.util.List;

public class MainApp {
public static void main(String[] args) {
// Create a sample Configurator
Job job = new Job();

List<Action> actions = Arrays.asList(
new Action("Action1", "Description1"),
new Action("Action2", "Description2")
);
job.setActions(actions);

Configurator configurator = new Configurator();
configurator.setName("Example Config");
configurator.setResourceType("PipelineRun"); // Or "TaskRun"
configurator.setJob(job);

// Create resource (PipelineRun or TaskRun)
Visitable resource = JobFactory.createResource(configurator);

// Process resource using visitor
ResourceVisitor visitor = new ResourceVisitor();
resource.accept(visitor);
}
}
25 changes: 25 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/PipelineRun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.snowdrop.visitorpattern;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class PipelineRun implements Visitable {
private String name;
private List<Action> actions;

public PipelineRun(Configurator cfg) {
this.name = cfg.getName();
this.actions = cfg.getJob().getActions();
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

// Other methods related to PipelineRun
}
16 changes: 16 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/ResourceVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.snowdrop.visitorpattern;

public class ResourceVisitor implements Visitor {

@Override
public void visit(PipelineRun pipelineRun) {
System.out.println("Processing PipelineRun: " + pipelineRun.getName());
// Logic to process PipelineRun
}

@Override
public void visit(TaskRun taskRun) {
System.out.println("Processing TaskRun: " + taskRun.getName());
// Logic to process TaskRun
}
}
14 changes: 14 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/Step.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.snowdrop.visitorpattern;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Step {
private String name;

public Step(String name) {
this.name = name;
}
}
27 changes: 27 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/TaskRun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.snowdrop.visitorpattern;

import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.List;

@Getter
@Setter
public class TaskRun implements Visitable {
private String name;
private List<Step> steps;

public TaskRun(String name, List<Step> steps) {
this.name = name;
this.steps = steps;
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

// Other methods related to TaskRun
}

6 changes: 6 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/Visitable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.snowdrop.visitorpattern;

public interface Visitable {
void accept(Visitor visitor);
}

7 changes: 7 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/Visitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.snowdrop.visitorpattern;

public interface Visitor {
void visit(PipelineRun pipelineRun);
void visit(TaskRun taskRun);
}

0 comments on commit 0359e7d

Please sign in to comment.