Created
February 8, 2019 21:42
-
-
Save tcelik/4e03c222de230fa563b6379897b16996 to your computer and use it in GitHub Desktop.
Builder Design Pattern Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.csystem.builderapp.app; | |
import java.util.Iterator; | |
public class App { | |
public static void main(String[] args) | |
{ | |
System.out.println("hello"); | |
//enum bildirimlerini kullanmak, new arka planda yapılır. | |
Button YESNO = Button.YESNO; | |
Button YESNOCANCEL = Button.YESNOCANCEL; | |
Button OK = Button.OK; | |
Button OKCANCEL = Button.OKCANCEL; | |
//artık elinde bir enum nesnesi var. | |
System.out.println(YESNO.ordinal()); | |
System.out.println(YESNOCANCEL.ordinal()); | |
System.out.println(OK.ordinal()); | |
System.out.println(OKCANCEL.ordinal()); | |
yıldızBas(2); | |
Button [] buttons = Button.values(); | |
for (Button b : buttons) { | |
System.out.println(b.ordinal()); | |
} | |
Button [] buttons1 = new Button[3]; | |
buttons1[0] = Button.OK; | |
buttons1[1] = Button.YESNO; | |
buttons1[2] = YESNOCANCEL; | |
yıldızBas(15); | |
Button1 YESNO1 = Button1.YESNO; | |
Button1 YESNOCANCEL1 = Button1.YESNOCANCEL; | |
System.out.println(YESNO1.ordinal()); | |
yıldızBas(1); | |
System.out.println(YESNOCANCEL1.ordinal()); | |
yıldızBas(20); | |
//static sınıf bildiriminden nesne almak | |
Sample.Builder builder = new Sample.Builder(new Sample()); | |
builder.foo(); | |
AlertDialog.Builder b = new AlertDialog.Builder(); | |
b.setCaption("Alert Dialog") | |
.setText("Hoşgeldiniz") | |
.setIcon(Icon.WARNING) | |
.setButton(Button.YESNO); | |
AlertDialog myAlert = b.build(); | |
myAlert.show(); | |
AlertDialog alert = new AlertDialog.Builder() | |
.setCaption("Dikkat") | |
.setText("Çıkmak istediğinize eminmisiniz?") | |
.setIcon(Icon.QUESTION) | |
.setButton(Button.YESNOCANCEL) | |
.build(); | |
alert.show(); | |
AlertDialog dlg1 = new AlertDialog.Builder() | |
.setCaption("CAPTİON İS HERE") | |
.setText("This is my text") | |
.setIcon(Icon.CRITICAL) | |
.setButton(Button.OK).build(); | |
//nesnem var artık | |
} | |
public static void yıldızBas(int n) //2 | |
{ | |
while(n-- > 0) { | |
System.out.print("*"); | |
} | |
System.out.println(); | |
} | |
} | |
enum Icon {WARNING, QUESTION, CRITICAL} | |
enum Button {YESNO, YESNOCANCEL, OK, OKCANCEL} | |
//bildrim | |
class Button1 { | |
private int m_val; | |
public static final Button1 YESNO = new Button1(0); | |
public static final Button1 YESNOCANCEL = new org.csystem.builderapp.app.Button1(1); | |
public static final Button1 OK = new Button1(2); | |
public static final Button1 OKCANCEL = new Button1(3);; | |
private Button1(int val) | |
{ | |
m_val = val; | |
} | |
public int ordinal() | |
{ | |
return m_val; | |
} | |
//enum türünde bunuda kendisi yazıyor | |
public String toString() | |
{ | |
switch(this.ordinal()) { | |
case 0: return "YESNO"; | |
case 1: return "YESNOCANCEL"; | |
case 2: return "OK"; | |
case 3: return "OKCANCEL"; | |
} | |
return "null"; | |
} | |
} | |
//Soyut template, Bu alertdialog | |
class AlertDialog { | |
//Başlık ve yazı | |
private String m_caption, m_text; //each alert has | |
//each alert icon enum olur | |
private Icon m_icon; //şu an null | |
//button enum olur alert dialog üstünde çıkacak | |
private Button m_button; | |
private AlertDialog() {} //nesne yaratılmasın. | |
//sınıf içinde sınıf bildirimi yapılabilir. | |
public static class Builder { | |
private AlertDialog m_dlg; //her builderin kendi alerti var fluent kalıbı | |
public Builder() | |
{ | |
//yeni bir nesne al. | |
m_dlg = new AlertDialog(); | |
} | |
public AlertDialog build() | |
{ | |
return m_dlg; | |
} | |
//başlığı buil edelim | |
public Builder setCaption(String caption) | |
{ | |
//elimde bir nesne var, setter gerek yok. | |
m_dlg.m_caption = caption; | |
return this; //builder nesnesinin kendisini dönme teması. | |
} | |
public Builder setText(String text) | |
{ | |
this.m_dlg.m_text = text; | |
return this; | |
} | |
public Builder setIcon(Icon icon) | |
{ | |
this.m_dlg.m_icon = icon; | |
return this; | |
} | |
public Builder setButton(Button button) | |
{ | |
this.m_dlg.m_button = button; | |
return this; | |
} | |
}//static class end | |
public void show() //this alert dialog, alert.show() | |
{ | |
System.out.printf("Caption:%s%n", this.m_caption); | |
System.out.printf("Text:%s%n", this.m_text); | |
System.out.printf("Icon:%s%n", this.m_icon); | |
System.out.printf("Button:%s%n", this.m_button); | |
} | |
} | |
class Sample { | |
public int sample = 2; | |
//static sınıf bildirimi, java'da top-level class static olamaz. inner class'lar static olabilir. | |
public static class Builder { | |
public int val = 1; | |
public Builder(Sample test) | |
{ | |
Sample s = new Sample(); | |
this.val = test.sample; | |
} | |
public void foo() | |
{ | |
} | |
} | |
} | |
//sınıf içinde sınıf bildirimi nested class. | |
class Mample { | |
class A { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment