Skip to content

Latest commit

ย 

History

History
434 lines (271 loc) ยท 10.7 KB

design-pattern.md

File metadata and controls

434 lines (271 loc) ยท 10.7 KB

Design Pattern

Builder, Adapter, Observer์— ๋Œ€ํ•˜์—ฌ.

โ€‹

Present time : 2018-06-27-WED

Last updated : 2018-07-05-THURS


0. ๊ณต์‹ ๋ฌธ์„œ

0-1. ๊ฐ€์ด๋“œ


1. Design Pattern

1-1. ์ •์˜

๋งŽ์€ ์‹ค๋ฌด ํ”„๋กœ๊ทธ๋ž˜๋จธ๋“ค์ด ์ธ์ •ํ•œ ํšจ์œจ์ ์ธ ์ฝ”๋”ฉ ๋ฐฉ๋ฒ• ๋˜๋Š” ๊ตฌ์กฐ.

1-2. ํ•„์š”ํ•œ ์ด์œ 

  1. ๋ช…ํ™•ํ•˜๊ณ  ๋‹จ์ˆœํ•œ ์ฝ”๋”ฉ

  2. class๋‚˜ function ๋“ฑ์€ ํ•œ ๊ฐ€์ง€ ๊ธฐ๋Šฅ๋งŒ ํ•˜๋„๋ก ์ž‘๊ฒŒ ์„ธ๋ถ„ํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Œ

  3. ๋†’์€ ์žฌ์‚ฌ์šฉ์„ฑ

  4. ์‰ฌ์šด ์œ ์ง€๋ณด์ˆ˜

  5. ๋ฆฌ์†Œ์Šค์˜ ๋‚ญ๋น„ ์ œ๊ฑฐ

    โ€‹

โ†’ ๋งŒ์•ฝ, (๊ฐœ์ธ ํ”„๋กœ์ ํŠธ + ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์š”๊ตฌ์‚ฌํ•ญ) ์ด๋ผ๋ฉด ๋””์ž์ธ ํŒจํ„ด์€ ํ•„์š”๊ฐ€ ์—†๋‹ค๊ณ  ๋งํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค.

1-3. ๋‹ค์–‘ํ•œ Design Pattern

1-3-1. ๊ฐœ๋…

  1. ์ƒ์„ฑ

    ๊ฐ์ฒด ์ƒ์„ฑ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃจ๊ณ  ์ƒํ™ฉ์— ์ ์ ˆํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

    ์ƒ์„ฑ์— ๊ด€๋ จ๋œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

  2. ๊ตฌ์กฐ

    ๋” ํฐ ๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ํด๋ž˜์Šค์™€ ๊ฐ์ฒด๋ฅผ ํ•ฉ์„ฑํ•˜๋Š”์ง€์™€ ๊ด€๋ จ๋œ ํŒจํ„ด์ด๋‹ค.

  3. ํ–‰์œ„

    • ๊ฐ์ฒด์˜ ํ–‰์œ„๋ฅผ ์กฐ์ง, ๊ด€๋ฆฌ, ์กฐํ•ฉํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด
    • ๊ฐ์ฒด๋“ค์ด ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ๊ทœ์ •
    • ๊ฐ๊ฐ ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค๊ณผ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ์ฒด์˜ ์ฑ…์ž„์„ ๊ทœ์ •ํ•˜์—ฌ ๋ณต์žกํ•œ ํ–‰์œ„๋“ค์„ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
    • ๋‘ ๊ฐ์ฒด ๊ฐ„์˜ ๊ด€๊ณ„์—์„œ๋ถ€ํ„ฐ ์•ฑ์˜ ์ „์ฒด ์•„ํ‚คํ…์ฒ˜์—๊นŒ์ง€ ์˜ํ–ฅ์„ ๋ฏธ์นจ

1-3-2. ์ข…๋ฅ˜

  • ์ƒ์„ฑ

  • Builder

  • Dependency Injection

  • Singleton

  • ๊ตฌ์กฐ

    • Adapter
    • Facade
  • ํ–‰์œ„

    • Command

    • Observer

    • Model View Controller

    • Model View ViewModel

      โ€‹


2. Builder

2-1. ๊ฐœ๋…

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ”ํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด.

๋ณต์žกํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์กฐ๋ฆฝํ•˜์—ฌ ๋งŒ๋“œ๋Š” ๊ตฌ์กฐ.

์ƒ์„ฑ์ž์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋งŽ์€ ํด๋ž˜์Šค์ธ ๊ฒฝ์šฐ ๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Œ.

Android์—์„œ๋Š” NotificationCompat.Builder์™€ AlertDialog.Builder ๊ฐ™์€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉ ํ•  ๋•Œ Builder ํŒจํ„ด์ด ๋‚˜ํƒ€๋‚จ

Notification notification =new NotificationCompat.Builder(this)
                                      .setSmallIcon(R.drawable.ic_notification)
                                      .setContentIntent(pendingIntent)
                                      .setTicker(message)
                                      .build();
new AlertDialog.Builder(this)
    .setTitle("Builder Dialog")
    .setMessage("Builder Dialog Message")
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override public void onClick(DialogInterface dialog, int which) {
        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override public void onClick(DialogInterface dialog, int which) {
        }
    })
    .show();

โ†’ ์ƒ์„ฑ์ž ์ธ์ž๊ฐ€ ๋งŽ์„ ๋•Œ๋Š” Builder ํŒจํ„ด ์ ์šฉ์„ ๊ณ ๋ คํ•˜๋ผ

2-2. ์˜ˆ์ œ

2-2-1. ๊ธฐ๋ณธ

public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    public static class Builder {
        // Required parameters(ํ•„์ˆ˜ ์ธ์ž)
        private final int servingSize;
        private final int servings;

        // Optional parameters - initialized to default values(์„ ํƒ์  ์ธ์ž๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”)
        private int calories      = 0;
        private int fat           = 0;
        private int carbohydrate  = 0;
        private int sodium        = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings    = servings;
        }

        public Builder calories(int val) {
            calories = val;
            return this;    // ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด . ์œผ๋กœ ์ฒด์ธ์„ ์ด์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
        }
        public Builder fat(int val) {
            fat = val;
            return this;
        }
        public Builder carbohydrate(int val) {
            carbohydrate = val;
            return this;
        }
        public Builder sodium(int val) {
            sodium = val;
            return this;
        }
        public NutritionFacts build() {
            return new NutritionFacts(this);
        }
    }

    private NutritionFacts(Builder builder) {
        servingSize  = builder.servingSize;
        servings     = builder.servings;
        calories     = builder.calories;
        fat          = builder.fat;
        sodium       = builder.sodium;
        carbohydrate = builder.carbohydrate;
    }
}

์œ„์™€ ๊ฐ™์ด ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

NutritionFacts.Builder builder = new NutritionFacts.Builder(240, 8);
builder.calories(100);
builder.sodium(35);
builder.carbohydrate(27);
NutritionFacts cocaCola = builder.build();

๋˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

// ๊ฐ ์ค„๋งˆ๋‹ค builder๋ฅผ ํƒ€์ดํ•‘ํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด ํŽธ๋ฆฌํ•˜๋‹ค.
NutritionFacts cocaCola = new NutritionFacts
    .Builder(240, 8)    // ํ•„์ˆ˜๊ฐ’ ์ž…๋ ฅ
    .calories(100)
    .sodium(35)
    .carbohydrate(27)
    .build();           // build() ๊ฐ€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋Œ๋ ค์ค€๋‹ค.

2-2-2. Lombok @Builder

์‚ฌ์šฉ๋ฐฉ๋ฒ•

2-2-1์˜ ๋นŒ๋” ํŒจํ„ด์ด๋ผ๋ฉด ๋กฌ๋ณต์˜ @Builder Annotation์œผ๋กœ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

@Builder
public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;
}

์‚ฌ์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

NutritionFacts.NutritionFactsBuilder builder = NutritionFacts.builder();
builder.calories(230);
builder.fat(10);
NutritionFacts facts = builder.build();

๋ฌผ๋ก  .์ฒด์ธ๋„ ๋œ๋‹ค.

NutritionFacts facts = NutritionFacts.builder()
    .calories(230)
    .fat(10)
    .build();

Setting

  1. Eclipse + Window

  2. Android Studio + Window

    โ€‹

2-2-3. ์žฅ์ 

  1. ๊ฐ ์ธ์ž๊ฐ€ ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์•Œ๊ธฐ ์‰ฝ๋‹ค.

  2. setter ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

  3. ํ•œ ๋ฒˆ์— ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ๊ฐ์ฒด ์ผ๊ด€์„ฑ์ด ๊นจ์ง€์ง€ ์•Š๋Š”๋‹ค.

  4. build() ํ•จ์ˆ˜๊ฐ€ ์ž˜๋ชป๋œ ๊ฐ’์ด ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€ ๊ฒ€์ฆํ•˜๊ฒŒ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    โ€‹


3. Adapter

3-1. ๊ฐœ๋…

๊ธฐ์กด์‹œ์Šคํ…œ ์—…์ฒด์—์„œ ์ œ๊ณตํ•œ ํด๋ž˜์Šค

๊ธฐ์กด์‹œ์Šคํ…œ ์–ด๋Œ‘ํ„ฐ ์—…์ฒด์—์„œ ์ œ๊ณตํ•œ ํด๋ž˜์Šค

Android์—์„œ ๋Œ€ํ‘œ์ ์œผ๋กœ AdapterํŒจํ„ด์„ ๋ณด์ด๋Š” ํด๋ž˜์Šค๋Š” RecyclerView.Adapter

RecyclerView.Adapterํด๋ž˜์Šค๋Š” ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง(Model)๊ณผ RecyclerView๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• 

RecyclerView.Adapter์€ ๋ฐ์ดํ„ฐ ์•„์ดํ…œ์— ์ ‘๊ทผํ•˜๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์•„์ดํ…œ์— View๋ฅผ ๊ทธ๋ฆฌ๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰

public class SampleAdapter extends RecyclerView.Adapter<SampleViewHolder> {
    private List<Test> tests;

    public SampleAdapter() {
        tests = new ArrayList();
    }

    @Override
    public SampleViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        // Create View Holder
    }

    @Override
    public void onBindViewHolder(SampleViewHolder holder) {
        // View Bind
    }

    @Override
    public int getItemCount() {
        return tests.size();
    }

    public void add(Test item) {
        tests.add(item);
    }
}

RecyclerVIew๋Š” Test์— ๋Œ€ํ•ด์„œ ๋ฌด์—‡์ธ์ง€ ๋ชจ๋ฆ„.

SampleAdapter๊ฐ€ Test์™€ RecyclerView์„ ์—ฐ๊ฒฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— RecyclerVIew์€ Test์— ๋Œ€ํ•ด์„œ ์•Œ ํ•„์š”๊ฐ€ ์—†์Œ.

3-2. ์˜ˆ์ œ

http://jusungpark.tistory.com/22 ์ฐธ๊ณ 

3-3. ์žฅ์ 

  • ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Œ

  • ํด๋ผ์ด์–ธํŠธ์™€ ๊ตฌํ˜„๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ถ„๋ฆฌ์‹œํ‚ฌ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ–ฅํ›„ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„ ๊ทธ ๋ณ€๊ฒฝ ๋‚ด์—ญ

    ์€ ์–ด๋Œ‘ํ„ฐ์— ์บก์Šํ™” ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ๋Š” ๋ฐ”๋€” ํ•„์š”๊ฐ€ ์—†์–ด์ง.


4. Observer

4-1. ๊ฐœ๋…

observer : ๊ด€์ฐฐ์ž

๊ฐ์ฒด์ง€ํ–ฅ ์„ค๊ณ„๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด ๊ฐ์ฒด๋“ค ์‚ฌ์ด์—์„œ ๋‹ค์–‘ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด ๊ฐ์ฒด์™€ ์ƒํƒœ์˜ ๋ณ€๊ฒฝ์„ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ด€์ฐฐ ๊ฐ์ฒด(Observer Object)๊ฐ€ ์กด์žฌํ•˜๋ฉฐ ์ด๋“ค์˜ ๊ด€๊ณ„๋Š” 1:1์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ  1:N์ด ๋  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

์„œ๋กœ์˜ ์ •๋ณด๋ฅผ ๋„˜๊ธฐ๊ณ  ๋ฐ›๋Š” ๊ณผ์ •์—์„œ ์ •๋ณด์˜ ๋‹จ์œ„๊ฐ€ ํด์ˆ˜๋ก, ๊ฐ์ฒด๋“ค์˜ ๊ทœ๋ชจ๊ฐ€ ํด์ˆ˜๋ก, ๊ฐ ๊ฐ์ฒด๋“ค์˜ ๊ด€๊ณ„๊ฐ€ ๋ณต์žกํ• ์ˆ˜๋ก ์ ์  ๊ตฌํ˜„ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง€๊ณ  ๋ณต์žก์„ฑ์ด ๋งค์šฐ ์ฆ๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์ œ์‹œํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ Observer Pattern์ด๋‹ค.

๊ฐ์ฒด์™€์˜ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ๋Š์œผ๋ฉฐ ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ๊ทธ ์ •๋ณด๋ฅผ Observer(Subscribe, ๊ตฌ๋…์ž)์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

4-2. ์˜ˆ์ œ

apiService.getData(someData)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe (/* an Observer */);
  • ๊ฐ’(์ด๋ฒคํŠธ)๋ฅผ ๋ฐฉ์ถœ ํ•  Observable ๊ฐ์ฒด๋ฅผ ์ •์˜
  • Observable๋“ค์€ ํ•œ ๋ฒˆ ๋˜๋Š” ์—ฐ์†์ ์œผ๋กœ ์ŠคํŠธ๋ฆผ, ๊ฐ’, ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ถœํ•จ
  • Subscriber๋Š” ์ด๋Ÿฌํ•œ ๊ฐ’์„ ์ˆ˜์‹ ํ•˜๊ณ  ๋„์ž‘ํ•œ ๋Œ€๋กœ ์‘๋‹ต
  • ์˜ˆ๋ฅผ๋“ค์–ด, API ํ˜ธ์ถœ์„ ์ž‘์„ฑํ•˜๊ณ  ์„œ๋ฒ„์—์„œ ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•  Subscriber๋ฅผ ์ง€์ • ํ•  ์ˆ˜ ์žˆ์Œ

4-2-1. ์–ด๋–ค ๊ณณ์— ์“ฐ์ผ๊นŒ?

Java์˜ Swing์ด๋‚˜ Android์˜ View๋‚˜ Button ๋“ฑ์˜ ์œ„์ ฏ์— ๊ฐ์ข… ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ๋•Œ ์“ฐ์ธ๋‹ค.

Android - Button

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(...) {
        // ACTION
    }
})

Button์€ ํ•ญ์ƒ Click์ด๋ฒคํŠธ๊ฐ€ ์žˆ์œผ๋ฉฐ, ์ด ์ด๋ฒคํŠธ๋Š” OnClickListener ๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

์ฆ‰ Button์ด๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ Publisher๊ฐ€ ๋˜๊ณ , OnClickListener๊ฐ€ Observer๊ฐ€ ๋œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. Button์—์„œ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ(ํด๋ฆญ ๋  ๊ฒฝ์šฐ)๋œ๋‹ค๋ฉด OnClickListener๋กœ ์•Œ๋ ค์ค€๋‹ค.

์œ„์˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด Button์— OnClickListener๋ผ๋Š” Observer๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€ํ‘œ์ ์ธ Observer Pattern์„ ์ ์šฉํ•œ ๊ฒƒ์ด๋‹ค.

4-3. ์žฅ์ 

  • ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์„ฑ ์œ ์ง€