Skip to main content

Pattern

Soit une interface Job representant un job, un stage. Les jobs ont a minima un titre et un identifiant

Job

 

public interface Job {

	Long getId();
	String getTitle();
}

 

Les Stages

Les Stages sont des types de Job

public class Stage implements Job{

	private String title;
	private Long id;
	
	public Stage()
	{
		
	}
	public String getTitle() {
		return title;
	}

	public Long getId() {
		return id;
	}

	@Override
	public String toString() {
		return "Stage [title=" + title + ", id=" + id + "]";
	}

 

Pattern de builder

Le pattern de builder permet de construire des objets sans pouvoir les modifier (object immuable):

private Stage(Builder builder) {
		this.title = builder.title;
		this.id = builder.id;
	}
	/**
	 * Creates builder to build {@link Stage}.
	 * @return created builder
	 */
	public static Builder builder() {
		return new Builder();
	}
	/**
	 * Builder to build {@link Stage}.
	 */
	public static final class Builder {
		private String title;
		private Long id;

		private Builder() {
		}

		public Builder withTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder withId(Long id) {
			this.id = id;
			return this;
		}

		public Stage build() {
			return new Stage(this);
		}
	}