dimanche 22 mai 2022

Observer design pattern java task

Observer is a behavioral design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen with the object they’re observing.

Implement methods:

newRepository - returns a Repository. It supports commits to various branches and merges between branches. Also, it supports WebHooks - observers that observes commit or merge events. mergeToBranchWebHook - returns a WebHook that observes merge events for a target branch. commitToBranchWebHook - returns a WebHook that observes commit events for a target branch.

I'm new to design patterns so can someone give a hint on how to implement these methods.

Webhook Interface

public interface WebHook {
String branch();
Event.Type type();
List<Event> caughtEvents();
void onEvent(Event event);}

Repository Interdace

public interface Repository {
void addWebHook(WebHook webHook);
Commit commit(String branch, String author, String[] changes);
void merge(String sourceBranch, String targetBranch);}

Commit Class

public class Commit{
private String author;
private String[] changes;

public Commit(final String author, final String[] changes) {
    this.author = author;
    this.changes = changes;
}

String author(){
    return author;
}

String[] changes(){
    return changes;
}

@Override
public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    final Commit commit = (Commit) o;

    if (!Objects.equals(author, commit.author)) return false;
    // Probably incorrect - comparing Object[] arrays with Arrays.equals
    return Arrays.equals(changes, commit.changes);
}

@Override
public int hashCode() {
    int result = author != null ? author.hashCode() : 0;
    result = 31 * result + Arrays.hashCode(changes);
    return result;
}

@Override
public String toString() {
    return new StringJoiner(", ", Commit.class.getSimpleName() + "[", "]")
            .add(author)
            .add(Arrays.toString(changes))
            .toString();
}}

Event Class

public class Event {
private Type type;
private String branch;
private List<Commit> commits;

public Event(final Type type, final String branch, final List<Commit> commits) {
    this.type = type;
    this.branch = branch;
    this.commits = commits;
}

public Event() {

}

Type type() {
    return type;
}

String branch() {
    return branch;
}

List<Commit> commits() {
    return commits;
}

@Override
public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    final Event event = (Event) o;

    if (type != event.type) return false;
    if (!Objects.equals(branch, event.branch)) return false;
    return Objects.equals(commits, event.commits);
}

@Override
public int hashCode() {
    int result = type != null ? type.hashCode() : 0;
    result = 31 * result + (branch != null ? branch.hashCode() : 0);
    result = 31 * result + (commits != null ? commits.hashCode() : 0);
    return result;
}

enum Type {
    COMMIT,
    MERGE
}

@Override
public String toString() {
    return new StringJoiner(", ", Event.class.getSimpleName() + "[", "]")
            .add(type.toString())
            .add(branch)
            .add(commits.toString())
            .toString();
}}

Aucun commentaire:

Enregistrer un commentaire