๋นŒ๋” ํŒจํ„ด์€ ๋ฌด์—‡์ผ๊นŒ? ๋ฌด์—‡์„ ์กฐ์‹ฌํ•ด์•ผ ํ• ๊นŒ? ์–ด๋–ค์‹์œผ๋กœ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ?

Builder

  • ๊ฐœ์ฒด์˜ ์ƒ์„ฑ๊ณผ์ •์„ ๊ทธ ๊ฐœ์ฒด์˜ ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๊ฐœ์ฒด์˜ ๋ถ€๋ถ„๋ถ€๋ถ„์„ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€๋‹ค ์ค€๋น„๊ฐ€๋˜๋ฉด ๊ทธ์ œ์„œ์•ผ ๊ฐœ์ฒด๋ฅผ ์ƒ์„ฑ

StringBuilder

  • ๋‹จ์ˆœํ•œ ๊ฒƒ์€ ์ด๊ฑธ ์‚ฌ์šฉํ•ด์„œ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ๋ณต์žกํ•ด์ง„๋‹ค๋ฉด?
  • ์˜ˆ๋ฅผ ๋“ค์–ด ํ•˜๋‚˜์˜ String์— 100๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด String์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค๋ฉด?
StringBuilder builder = new StringBuilder(4096);
builder.append(heading);
builder.append(newLine);
builder.append(newLine);
 
builder.append(leadingParagraph);
builder.append(newLine);
 
for (KeyValue kv: data) {
    builder.append(" * ");
    builder.append(kv.getKey());
    builder.append(": ");
    builder.append(kv.getValue());
    builder.append(newLine);
}
 
String document = builder.toString();
  • ์ด๋Ÿด ๋•Œ StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธํ•˜๋‹ค.
  • overloading๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์–ด์„œ ๋‹ค์–‘ํ•œ ์ž๋ฃŒํ˜•๋„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์‹ฌ์ง€์–ด ์„ฑ๋Šฅ๋„ ๋” ๋น ๋ฅด๋‹ค. ์•Œ์•„์„œ ํšจ์œจ์ ์œผ๋กœ ํ•ฉ์ณ์ค€๋‹ค.
    • ๋‹ค๋งŒ ๋‚ด๋ถ€๊ฐ€ ์–ด๋–ป๊ฒŒ ๋„๋Š”์ง€ ์•Œ๋ฉด ๋” ์ข‹์Œ
  • ๋งˆ์ง€๋ง‰์— toString()์„ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฌผ์„ ๊ฐ€์ ธ์˜ด

๋ฌธ์ œ์ 

  • ์ฝ”๋“œ๊ฐ€ ์ž˜ ์ฝํžˆ๋‚˜ 2%๋ถ€์กฑ
  • ์ž‘์„ฑ์ž์˜ ์˜๋„: ์ œ๋ชฉ์„ ๋„ฃ๊ณ  ์ค„์„ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค.
  • ๊ทผ๋ฐ ์ € ์œ„์— ์ฝ”๋“œ๋งŒ ๋ด์„œ๋Š” ๋…๋ฆฝ์ ์ธ 3๊ฐœ์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋Š๋‚Œ์ž„

Fluent interface

  • GoF์—๋Š” ์—†์Œ
  • ์ตœ๊ทผ์— builder ํŒจํ„ด์˜ ๋ฐœ์ „๋œ ๋ฐฉ์‹
StringBuilder builder = new StringBuilder(1024 * 1024);
builder.append(heading)
       .append(newLine)
       .append(newLine);
 
builder.append(leadingParagraph)
       .append(newLine);
 
for (KeyValue kv: data) {
    builder.append(" * ");
           .append(kv.getKey());
           .append(": ");
           .append(kv.getValue());
           .append(newLine);
}
 
String document = builder.toString();
  • ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ์ž๊ธฐ์ž์‹ ์„ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹!
    • (Walnut์ด builder pattern์œผ๋กœ ๊ตฌํ˜„๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Œ)

์ž๊ธฐ ์Šค์Šค๋กœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ โ€ฆ?

  • ํ•จ์ˆ˜ Signature๋งŒ ๋ด์„œ๋Š” ์ด์ƒํ•˜๋‹ค.
    • ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ํ•ด๋‹น ๊ฐœ์ฒด๋ฅผ ๋“ค๊ณ  ์žˆ๋Š”๋ฐ ์™œ return this๋ฅผ ํ•˜์ง€?
    • ์ด์ƒํ•˜์ง€๋งŒ ์ด์ œ๋Š” ์ƒ์‹์œผ๋กœ ์ทจ๊ธ‰๋œ๋‹ค.

์ž˜๋ชป ์‚ฌ์šฉํ•˜๋Š” ๋นŒ๋” ํŒจํ„ด

public final class Employee {
    private String firstName;
    private String lastName;
    private int id;
    private int yearStarted;
    private int age;
 
    public Employee(String firstName, String lastName, int id, int, yearStarted, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
        this.yearStarted = yearStarted;
        this.age = age;
    }
}
 
Employee wansik = new Employee("Wansik", "Choi", 1, 1995, 28);
Employee howon = new Employee("Kim", "Howon", 30, 1993, 2);
  • ์ง์› ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž.
  • ๊ทผ๋ฐ ๋‘๋ฒˆ์งธ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.
  • ์„ฑ๊ณผ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์„œ ๋„ฃ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์ด์™€ Id๋„!
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ๋‹น์—ฐํžˆ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์žก์•„์ค„ ์ˆ˜ ์—†๋‹ค.
  • ์ด๋Ÿฐ ๊ฒƒ์„ builder๋กœ ํ•ด๊ฒฐํ•ด๋ณด์ž.
Employee howon = new EmployeeBuilder(1)
                     .withAge(30)
                     .withStartingYear(1993)
                     .withName("Howon", "Kim")
                     .build();
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฉ”์„œ๋“œ ์ด๋ฆ„์œผ๋กœ ๋„ฃ์œผ๋‹ˆ ์ž˜๋ชป๋œ ๊ฐ’์„ ์ „๋‹ฌํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์ ์–ด์ง„๋‹ค.
  • ํ•˜์ง€๋งŒ, ์ด๊ฑด ์ž˜๋ชป๋œ ํ•ด๊ฒฐ๋ฒ•

๋งŒ์•ฝ์— ์‹ค์ˆ˜ํ•œ๋‹ค๋ฉด?

Employee howon = new EmployeeBuilder(1)
                     .withAge(30)
                     .withName("Howon", "Kim")
                     .build();
  • ์ž๋ฐ”์—์„œ๋Š” ์ดˆ๊ธฐ๊ฐ’์ด 0์ด๋‹ค. ๋”ฐ๋ผ์„œ 0๋…„๋ถ€ํ„ฐ ์ผํ•œ ์ง์›์ด ๋œ๋‹ค.
  • ๊ฐœ์ฒด๋Š” ์ƒ์„ฑ๋ถ€ํ„ฐ ์œ ํšจํ•œ ์ƒํƒœ์—ฌ์•ผ ํ•˜๋Š”๋ฐ ์ด๋ฅผ ์–ด๊ฒผ๋‹ค.
  • ์ด๊ฑด ๋””์ž์ธ ํŒจํ„ด์„ ์ž˜๋ชป ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ๋‹ค.
  • ๊ทผ๋ฐ ์ด๋Ÿฐ์‹์œผ๋กœ ๋งŒ๋“  SDK๊ฐ€ ๋งŽ์•„์กŒ๋‹ค..
    • G๋ชจ์‚ฌ์กฐ์ฐจ๋„..
  • StringBuilder๋Š” ์œ ํšจํ•œ ๊ฐœ์ฒด๋งŒ ๋ฐ˜ํ™˜ํ–ˆ๋‹ค.
    • String๋งŒ ๋‹ค๋ค˜๊ธฐ ๋•Œ๋ฌธ์—, ์–ธ์ œ ํ˜ธ์ถœํ•ด๋„ ์œ ํšจํ•œ String๊ฐœ์ฒด๊ฐ€ ๋‚˜์˜จ๋‹ค.

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• 1

  • ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋นผ๋จน์ง€ ์•Š์œผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ž์ฒด๋ฅผ ๊ฐ€์ง€๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ ๊ด€๋ฆฌํ•˜์ž.
public final class Employee {
    private String firstName;
    private String lastName;
    private int id;
    private int yearStarted;
    private int age;
 
    public Employee(CreateEmployeeParams params) {
        this.firstName = params.first;
        this.lastName = params.lastName;
        this.id = params.id;
        this.yearStarted = params.yearStarted;
        this.age = params.age;
    }
}
 
CreateEmployeeParams params = new CreateEmployeeParams();
params.firstName = "Howon";
params.lastName = "Kim";
params.id = 1;
params.age = 30;
params.yearStarted = 1993;
 
Employee howon = new Employee(params)
  • ์ˆœ์ˆ˜ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ๋‹ด์€ ๊ตฌ์กฐ์ฒด์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
    • swift๋ผ๋ฉด ๋‹น์—ฐํžˆ struct๋กœ ๋งŒ๋“ค๊ฒ ์ง€
    • DTO
  • ์ž˜๋ชป ์‚ฌ์šฉํ•œ Builder ํŒจํ„ด ๋ณด๋‹ค๋Š” ์ด๊ฒŒ ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ณ  ์ข‹๋‹ค.
  • ์ธ์ž ์ˆœ์„œ(firstName, lastName) ์ž˜๋ชป ๋„ฃ๋Š” ๋ฌธ์ œ๋Š” ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ทผ๋ฐ ๊ทธ๋‚˜๋งˆ ๋‚ซ๋‹ค๋Š” ๊ฑฐ์ง€ ์—ฌ์ „ํžˆ ์ธ์ž๋ฅผ ๋นผ๋จน์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.
  • ๊ตณ์ด with~, build() ํ˜ธ์ถœํ•˜๊ณ  ํ•˜๋Š” ๋ฐฉ์‹์„ ์“ธ ์ด์œ ๊ฐ€ ์—†๋‹ค๋Š” ๋ง์„ ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ

ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• 2

  • named parameter
  • ์ธ์ž์— ์ด๋ฆ„์„ ๋‹ฌ์•„๋ฒ„๋ฆฌ๋Š” ๋ฐฉ๋ฒ•
  • ์œ„์˜ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ๊ณ ์น  ์ˆ˜ ์žˆ๋‹ค.
  • Swift์—์„œ ๋ณ€์ˆ˜ ์ธ์ž๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.
Employee(firstName: "Howon", lastName: "Kim", id: 1, yearStarted: 1993, age: 30);

์ •๋ฆฌ

  • ์œ„์™€ ๊ฐ™์ด ์–ธ์–ด ์ž์ฒด์—์„œ ์ง€์›ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋‹ค.
  • ์–ธ์–ด์—์„œ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ(named parameter)๋•Œ๋ฌธ์— builder๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ผผ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • builder pattern์€, ์–ด๋Š ์ƒํƒœ์—์„œ ์ ์šฉํ•ด๋„ ๊ฐœ์ฒด์˜ ์œ ํšจ์„ฑ์ด ๋ณด์žฅ๋  ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, UIView์— ๋‹ค์–‘ํ•œ ์š”์†Œ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋„ฃ์ง€ ์•Š์•„๋„ UIView์ž์ฒด๊ฐ€ ์˜๋ฏธ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ฒ ๋‹ค.

๋‹คํ˜•์ ์ธ ๋นŒ๋” ํŒจํ„ด

  • .csv ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.
  • ์ด๊ฑธ HTML, markdown์œผ๋กœ ๊ฐ๊ฐ ๋ณ€ํ™˜ํ•˜๊ณ  ์‹ถ๋‹ค.

CsvReader reader = new CsvReader(csvText);
HtmlTableBuilder builder = new HtmlTableBuilder();
 
reader.writeTo(builder);
 
HtmlDocument html = builder.toHtmlDocument();
CsvReader reader = new CsvReader(csvText);
MarkdownTableBuilder = builder = new MarkdownTableBuilder();
 
reader.writeTo(builder);
 
String mdText = builder.toMarkDownText();
  1. CsvReader์— ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ค€๋‹ค.
  2. ์–ด๋–ค ํƒ€์ž…์˜ Builder๋ฅผ ๋งŒ๋“ค์ง€ ์„ ํƒ
  3. CsvReader๋Š” ๊ฐ™์€ ํƒ€์ž…์—์„œ ์ฒ˜๋ฆฌํ•ด๋ฒ„๋ฆฐ๋‹ค. ๊ฐ๊ฐ์˜ Builder์—์„œ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•๋งŒ ๋‹ค๋ฅผ๋ฟ์ด๋‹ค. (๋‹คํ˜•์  ํ˜ธ์ถœ)
  4. ๋งˆ์ง€๋ง‰์œผ๋กœ builder์—์„œ CsvReader์—์„œ ๋ช…๋ นํ•œ ๊ฒฐ๊ณผ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค. (๋‹คํ˜•์  ํ˜ธ์ถœ X)

Reference