|
| 1 | +## ๋ชฉ์ |
| 2 | + |
| 3 | +Validation Strategy ์ ํจ์ฑ ๊ฒ์ฆ ์ค๊ณ : ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ์ ๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฆ์ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํฉ๋๋ค. |
| 4 | +(Strategy Pattern(์ ๋ตํจํด) : ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ์ฒด์ ์ผ๋ถ๋ถ์ผ๋ก ์บก์ํํ์ฌ ๋
๋ฆฝ์ ์ผ๋ก ์๊ณ ๋ฆฌ์ฆ์ ๋ณ๊ฒฝ๊ฐ๋ฅ) |
| 5 | + |
| 6 | +## ๋์ |
| 7 | + |
| 8 | +์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ํ์ํ ํ์ผ |
| 9 | + |
| 10 | +### `๋ก๊ทธ์ธ ํผ` |
| 11 | + |
| 12 | +- **๋์์ธ ํจํด ์ ํ** |
| 13 | + - Strategy Pattern(์ ๋ตํจํด) |
| 14 | +- **์๋๋ฆฌ์ค** |
| 15 | + |
| 16 | + - ์ด๋ฉ์ผ ๋ฐ ๋น๋ฐ๋ฒํธ ์ ํจ์ฑ ๊ฒ์ฌ์, ํน์ ํ ์กฐ๊ฑด์ ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์ถ๊ฐ๋ ๊ฒฝ์ฐ๋ฅผ ์ํด ๋์์ธ ํจํด ์ ์ฉ |
| 17 | + (LooseValidation, StrongValidation) |
| 18 | + |
| 19 | + ``` |
| 20 | + classDiagram |
| 21 | + class ValidationStrategy { |
| 22 | + +emailValidate(input: string) bool |
| 23 | + +passwordValidate(input: string) bool |
| 24 | + } |
| 25 | + class LooseValidation { |
| 26 | + +emailValidate(input: string) bool |
| 27 | + +passwordValidate(input: string) bool |
| 28 | + } |
| 29 | + class StrongValidation { |
| 30 | + +emailValidate(input: string) bool |
| 31 | + +passwordValidate(input: string) bool |
| 32 | + } |
| 33 | + class ValidateProcessor { |
| 34 | + -ValidationStrategy validator |
| 35 | + +ValidateProcessor(validator) |
| 36 | + +setValidator(validator) |
| 37 | + +isValidEmail(email: string) bool |
| 38 | + +isValidPassword(password: string) bool |
| 39 | + } |
| 40 | +
|
| 41 | + ValidationStrategy <|-- LooseValidation : implements |
| 42 | + ValidationStrategy <|-- StrongValidation : implements |
| 43 | + ValidateProcessor o-- ValidationStrategy : uses |
| 44 | +
|
| 45 | + LooseValidation ..> ValidateProcessor : uses |
| 46 | + StrongValidation ..> ValidateProcessor : uses |
| 47 | + ``` |
| 48 | +
|
| 49 | +- **์ ๋ต ํจํด ๊ตฌํ ๊ฒฝ๋ก** |
| 50 | +
|
| 51 | + - 1. ์ ๋ต ์ธํฐํ์ด์ค ๊ตฌํ : ์ ์ธํฐํ์ด์ค๋ #validator๋ผ๋ ๋ฉ์๋๋ฅผ ํฌํจํ๊ณ ์์ผ๋ฉฐ, ์ด ๋ฉ์๋๋ ๋ฌธ์์ด์ ์
๋ ฅ๋ฐ์ boolean ๊ฐ์ผ๋ก ์ ํจ์ฑ์ ๋ฐํํฉ๋๋ค. |
| 52 | + - 2. ๊ตฌ์ฒด์ ์ธ ์ ๋ต ํด๋์ค ๋ง๋ค๊ธฐ : ๊ฐ ํด๋์ค๋ ValidationStrategy ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ฉฐ, ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ์ ๋ํ ๊ตฌ์ฒด์ ์ธ ๊ฒ์ฆ ๋ก์ง์ ์ ๊ณตํฉ๋๋ค. |
| 53 | + ์ด ํด๋์ค๋ค์ ๊ฐ๊ฐ ์ด๋ฉ์ผ๊ณผ ๋น๋ฐ๋ฒํธ ๊ฒ์ฆ ๋ก์ง์ ์บก์ํํ๋ฉฐ, validate ๋ฉ์๋๋ฅผ ํตํด ์
๋ ฅ๋ ๋ฐ์ดํฐ์ ์ ํจ์ฑ์ ๊ฒ์ฌํฉ๋๋ค. |
| 54 | + ์ด ๋ฐฉ์์ ๊ฒ์ฆ ๋ก์ง์ ๋ณ๊ฒฝํ๊ฑฐ๋ ๋ค๋ฅธ ์ ํ์ ๊ฒ์ฆ ๋ก์ง์ผ๋ก ์ฝ๊ฒ ๊ต์ฒดํ ์ ์๊ฒ ํด์ค๋๋ค. ์๋ฅผ ๋ค์ด, ๋น๋ฐ๋ฒํธ ์ ์ฑ
์ด ๋ณ๊ฒฝ๋์ด ๊ฒ์ฆ ๋ก์ง์ ์
๋ฐ์ดํธํด์ผ ํ๋ ๊ฒฝ์ฐ, ํด๋นํ๋ LooseValidation ํด๋์ค๋ง ์์ ํ๋ฉด ๋ฉ๋๋ค. |
| 55 | + - 3. ๋น๋ฐ๋ฒํธ๊ฐ ํน์ ์กฐ๊ฑด ์ถ๊ฐ StrongValidation (์: ์ต์ ํ ๊ฐ์ ์ซ์, ํ๋์ ๋๋ฌธ์ ๋ฐ ํน์ ๋ฌธ์ ํฌํจ)์ ์ถฉ์กฑํ๋์ง ํ์ธํ๋ ์ ๋ต. |
| 56 | +
|
| 57 | +- **์ด๋ค ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ์ ํ๋์ง** |
| 58 | +
|
| 59 | + - ์ ์ฐ์ฑ ๋ถ์กฑ: ํ๋์ ์ ํจ์ฑ ๊ฒ์ฆ์ด ๋ค์ํ ์๋๋ฆฌ์ค์ ๋ง๊ฒ ์ฐ์ฐํ๊ฒ ์กฐ์ ๋์ง ๋ชปํ๋ ๋ฌธ์ (ex ๋ก๊ทธ์ธ ๋น๋ฐ๋ฒํธ ์ ํจ์ฑ์ ๋์จํ ์ ํจ์ฑ์ ์ฌ์ฉํ๋ฉฐ, ํ์๊ฐ์
์ ํจ์ฑ์ ๋์จํ ์ ํจ์ฑ์ ๋ณด์์ ์ํ ๊ฐ๋ ฅํ ํน์ ์กฐ๊ฑด ์ถ๊ฐ) |
| 60 | + - ์ฝ๋ ์ค๋ณต : ๋น์ทํ ์ ํจ์ฑ ๊ฒ์ฆ ๋ก์ง์ด ์ฌ๋ฌ ๋ถ๋ถ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉ๋์ด ์ฝ๋ ์ค๋ณต์ด ๋ฐ์ํ๋ ๋ฌธ์ |
| 61 | + - ๋ฎ์ ํ์ฅ์ฑ : ์๋ก์ด ์ ํจ์ฑ ๊ฒ์ฆ ๋ก์ง์ ์ถ๊ฐํ๊ฑฐ๋ ๊ธฐ์กด ๋ก์ง์ ์์ ํ ๋ ๊ธฐ์กด ์ฝ๋๋ฅผ ๋ง์ด ๋ณ๊ฒฝํด์ผํ๋ ๋ฌธ์ . |
| 62 | +
|
| 63 | +- **์ ํด๋น ๋์์ธ ํจํด์ ์ ํํ๋์ง** |
| 64 | +- Observer : ์ด๋ฒคํธ ๋ฐ์์ ๋ฐ๋ฅธ ์ฌ๋ฌ ๊ฐ์ฒด์ ๋ฐ์์ ๊ด๋ฆฌํ ๋ ์ฌ์ฉ๋์ด์ง๋ฏ๋ก, ๋ฐ์ดํฐ ์์ฒด์ ์ ํจ์ฑ ํ๋จ์๋ ์ ํฉํ์ง ์๋ค๊ณ ์๊ฐ๋์ด์ก์ต๋๋ค. |
| 65 | +- Singleton : ์ ์ญ์์ ํ๋์ ์ธ์คํด์ค๋ง ๋ณด์ฅํ๋ ํจํด์ด๋ฏ๋ก ์ ํ์ ์ด์ฌ์ ์ ํฉํ์ง ์๋ค๊ณ ์๊ฐ๋์์ต๋๋ค. |
| 66 | +- Factory Method์ Abstract Factory : ๊ฐ์ฒด ์์ฑ์ ์ด์ ์ ๋ง์ถ ํจํด์ผ๋ก, ์ ํจ์ฑ ๊ฒ์ฆ ๋ณด๋ค๋ ๋ก์ง์ ๊ต์ฒด์ ์คํ์ ๋ ์ด์ธ๋ฆด ๊ฒ ๊ฐ์์ ์ ์ธํ์์ต๋๋ค. |
| 67 | +- ์์ ๊ฐ์ ์ฌ๋ฌ๊ฐ์ง ํจํด๋ค ์ค์ ์ ๋ต ํจํด์ ์ ํจ์ฑ๊ณผ ๊ฐ์ด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ๊ณผ ์ ์ง๋ณด์ ํ์ฅ์ฑ์ ๋ ์ด์ธ๋ฆฌ๋ค๊ณ ์๊ฐํ์ฌ์ ํด๋น ํจํด์ ์ ํํ์์ต๋๋ค. |
| 68 | +
|
| 69 | +- **์ฝ๋** |
| 70 | +
|
| 71 | + - ๊ตฌํ ์ |
| 72 | +
|
| 73 | + ``` |
| 74 | + export const isValidEmail = (email: string): boolean => { |
| 75 | + const regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/; |
| 76 | + return regex.test(email); |
| 77 | + }; |
| 78 | +
|
| 79 | + export const isValidPassword = (password: string) => { |
| 80 | + return password.length >= 6; |
| 81 | + }; |
| 82 | +
|
| 83 | + ``` |
| 84 | +
|
| 85 | + - 1์ฐจ ๊ตฌํ |
| 86 | +
|
| 87 | + ``` |
| 88 | + export interface ValidationStrategy { |
| 89 | + validate(input: string): boolean; |
| 90 | + } |
| 91 | +
|
| 92 | + export class isValidEmail implements ValidationStrategy { |
| 93 | + validate(email: string): boolean { |
| 94 | + return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(email); |
| 95 | + } |
| 96 | + } |
| 97 | +
|
| 98 | + export class isValidPassword implements ValidationStrategy { |
| 99 | + validate(password: string): boolean { |
| 100 | + return password.length >= 6; |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + export class isValidPasswordComplex implements ValidationStrategy { |
| 105 | + validate(password: string): boolean { |
| 106 | + const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/; |
| 107 | + return regex.test(password); |
| 108 | + } |
| 109 | + } |
| 110 | + ``` |
| 111 | +
|
| 112 | + - 2์ฐจ ๊ตฌํ |
| 113 | +
|
| 114 | + ``` |
| 115 | + export interface ValidationStrategy { |
| 116 | + emailValidate(input: string): boolean; |
| 117 | + passwordValidate(input: string): boolean; |
| 118 | + } |
| 119 | +
|
| 120 | + export class LooseValidation implements ValidationStrategy { |
| 121 | + emailValidate(email: string): boolean { |
| 122 | + return /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/.test(email); |
| 123 | + } |
| 124 | + passwordValidate(input: string): boolean { |
| 125 | + return input.length >= 6; |
| 126 | + } |
| 127 | + } |
| 128 | +
|
| 129 | + export class StrongValidation implements ValidationStrategy { |
| 130 | + emailValidate(password: string): boolean { |
| 131 | + return password.length >= 6; |
| 132 | + } |
| 133 | + passwordValidate(input: string): boolean { |
| 134 | + const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/; |
| 135 | + return regex.test(input); |
| 136 | + } |
| 137 | + } |
| 138 | +
|
| 139 | + export class ValidateProcessor { |
| 140 | + #validator: ValidationStrategy; |
| 141 | + constructor(validator: ValidationStrategy) { |
| 142 | + this.#validator = validator; |
| 143 | + } |
| 144 | +
|
| 145 | + setValidator(validator: ValidationStrategy) { |
| 146 | + this.#validator = validator; |
| 147 | + } |
| 148 | +
|
| 149 | + isValidEmail(email: string) { |
| 150 | + return this.#validator.emailValidate(email); |
| 151 | + } |
| 152 | +
|
| 153 | + isValidPassword(password: string) { |
| 154 | + return this.#validator.passwordValidate(password); |
| 155 | + } |
| 156 | + } |
| 157 | + ``` |
0 commit comments