Skip to content

Commit

Permalink
Improved the 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 0359e7d commit a9d80bc
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 30 deletions.
9 changes: 9 additions & 0 deletions src/main/java/dev/snowdrop/visitorpattern/AbstractRun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.snowdrop.visitorpattern;

public abstract class AbstractRun implements Visitable {

public AbstractRun() {}

@Override
public abstract AbstractRun accept(Visitor visitor);
}
6 changes: 1 addition & 5 deletions src/main/java/dev/snowdrop/visitorpattern/JobFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ 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);
return new TaskRun(config);
}
throw new IllegalArgumentException("Unknown resource type: " + config.getResourceType());
}
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/dev/snowdrop/visitorpattern/MainApp.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dev.snowdrop.visitorpattern;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class MainApp {
public static void main(String[] args) {
// Create a sample Configurator
Expand All @@ -15,7 +18,7 @@ public static void main(String[] args) {
job.setActions(actions);

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

Expand All @@ -24,6 +27,28 @@ public static void main(String[] args) {

// Process resource using visitor
ResourceVisitor visitor = new ResourceVisitor();
resource.accept(visitor);
AbstractRun run = resource.accept(visitor);
if (run instanceof PipelineRun) {
System.out.println("A PipelineRun : " + ((PipelineRun)run).getName());
} else {
log.info("A TaskRun : " + ((TaskRun) run).getName());
}

configurator.setName("TaskRun example");
configurator.setResourceType("TaskRun"); // Or "TaskRun"
configurator.setJob(job);

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

// Process resource using visitor
visitor = new ResourceVisitor();
run = resource.accept(visitor);
if (run instanceof PipelineRun) {
System.out.println("A PipelineRun : " + ((PipelineRun)run).getName());
} else {
System.out.println("A TaskRun : " + ((TaskRun)run).getName());
}

}
}
14 changes: 7 additions & 7 deletions src/main/java/dev/snowdrop/visitorpattern/PipelineRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

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

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

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

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

import java.util.stream.Collectors;

public class ResourceVisitor implements Visitor {

@Override
public void visit(PipelineRun pipelineRun) {
System.out.println("Processing PipelineRun: " + pipelineRun.getName());
// Logic to process PipelineRun
public AbstractRun visit(PipelineRun pipelineRun) {
System.out.println("Processing PipelineRun ...");
Configurator cfg = pipelineRun.getConfig();
pipelineRun.setActions(cfg.getJob().getActions());
pipelineRun.setName(cfg.getName());
return pipelineRun;
}

@Override
public void visit(TaskRun taskRun) {
System.out.println("Processing TaskRun: " + taskRun.getName());
// Logic to process TaskRun
public TaskRun visit(TaskRun taskRun) {
System.out.println("Processing TaskRun ...");
Configurator cfg = taskRun.getConfig();
taskRun.setSteps(cfg.getJob().getActions().stream()
.map(action -> new Step(action.getName()))
.collect(Collectors.toList()));
taskRun.setName(cfg.getName());
return taskRun;
}
}
13 changes: 6 additions & 7 deletions src/main/java/dev/snowdrop/visitorpattern/TaskRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
import lombok.Getter;
import lombok.Setter;

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

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

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

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

// Other methods related to TaskRun
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/snowdrop/visitorpattern/Visitable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.snowdrop.visitorpattern;

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

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

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

0 comments on commit a9d80bc

Please sign in to comment.