Builder_Pattern
parent
f198b9b7a5
commit
9c2ca9f38c
Binary file not shown.
|
@ -0,0 +1,77 @@
|
|||
package DesignPatterns.Erzeugungsmuster_CreationalPatterns.BuilderPatter;
|
||||
|
||||
public class BuilderPattern {
|
||||
// Private Felder
|
||||
private String teig;
|
||||
private String soße;
|
||||
private String käse;
|
||||
private String topping;
|
||||
|
||||
// Private Konstruktor, nur der Builder kann diesen aufrufen
|
||||
private BuilderPattern(Builder builder) {
|
||||
this.teig = builder.teig;
|
||||
this.soße = builder.soße;
|
||||
this.käse = builder.käse;
|
||||
this.topping = builder.topping;
|
||||
}
|
||||
|
||||
// Statische innere Klasse Builder
|
||||
public static class Builder {
|
||||
// Dieselben Felder wie in der äußeren Klasse
|
||||
private String teig;
|
||||
private String soße;
|
||||
private String käse;
|
||||
private String topping;
|
||||
|
||||
// Methoden für das Setzen der Felder, die den Builder zurückgeben
|
||||
public Builder teig(String teig) {
|
||||
this.teig = teig;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder soße(String soße) {
|
||||
this.soße = soße;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder käse(String käse) {
|
||||
this.käse = käse;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder topping(String topping) {
|
||||
this.topping = topping;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Die build-Methode erstellt das Pizza-Objekt
|
||||
public BuilderPattern build() {
|
||||
return new BuilderPattern(this);
|
||||
}
|
||||
}
|
||||
|
||||
// Methode, um die Pizza anzuzeigen
|
||||
public void anzeigen() {
|
||||
System.out.println("Pizza mit " + teig + " Teig, " + soße + " Soße, " +
|
||||
käse + " Käse und " + (topping != null ? topping : "ohne") + " Topping.");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Erstellen einer Pizza mit allen Zutaten
|
||||
BuilderPattern pizza1 = new BuilderPattern.Builder()
|
||||
.teig("Dünn")
|
||||
.soße("Tomaten")
|
||||
.käse("Mozzarella")
|
||||
.topping("Salami")
|
||||
.build();
|
||||
|
||||
// Erstellen einer Pizza ohne Topping
|
||||
BuilderPattern pizza2 = new BuilderPattern.Builder()
|
||||
.teig("Dick")
|
||||
.soße("Tomaten")
|
||||
.build();
|
||||
|
||||
pizza1.anzeigen(); // Ausgabe: Pizza mit Dünn Teig, Tomaten Soße, Mozzarella Käse und Salami Topping.
|
||||
pizza2.anzeigen(); // Ausgabe: Pizza mit Dick Teig, Tomaten Soße, Cheddar Käse und ohne Topping.
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
|
@ -13,6 +13,15 @@ public class Graph {
|
|||
public void add_Knoten(Knoten knote) {
|
||||
knoten.add(knote);
|
||||
}
|
||||
|
||||
public ArrayList<Knoten> getKnoten() {
|
||||
return knoten;
|
||||
}
|
||||
|
||||
public void zeige_Alle_Knoten() {
|
||||
System.out.println("Alle Knoten, die auf dem Graph sind: ");
|
||||
for (Knoten k : knoten)
|
||||
System.out.println("Knote: " + k.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,4 +17,7 @@ public class Kanten {
|
|||
Knoten partner = (knote == knote1)? knote2 : knote1;
|
||||
return partner;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -17,10 +17,28 @@ public class Knoten {
|
|||
this.kanten.add(kante);
|
||||
}
|
||||
|
||||
public ArrayList<Knoten> get_partner() {
|
||||
public void get_partner() {
|
||||
ArrayList<Knoten> alle_knoten = new ArrayList<>();
|
||||
for (Kanten k : kanten)
|
||||
alle_knoten.add(k.getpartner(this));
|
||||
return alle_knoten;
|
||||
|
||||
System.out.println("Alle Knoten, die mit 'knote " + this.getValue() + "' Verbunden sind: ");
|
||||
for (Knoten knoten:alle_knoten)
|
||||
System.out.println("Knote: " + knoten.getValue());
|
||||
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ArrayList<Kanten> getKanten() {
|
||||
return kanten;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -12,12 +12,12 @@ public class Main {
|
|||
Knoten knote3 = new Knoten(3);
|
||||
Kanten k2 = new Kanten(knote1,knote3);
|
||||
|
||||
System.out.println(knote1.get_partner());
|
||||
knote3.get_partner();
|
||||
|
||||
graph.add_Knoten(knote1);
|
||||
graph.add_Knoten(knote2);
|
||||
graph.add_Knoten(knote3);
|
||||
System.out.println(graph.knoten);
|
||||
graph.zeige_Alle_Knoten();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue