๊ฐ€์งœ ์ค‘๋ณต ์ฝ”๋“œ์™€ ๊ทธ ํ•ด๊ฒฐ ๋ฐฉ์•ˆ (With ์˜ค๋ธŒ์ ํŠธ)

2024. 8. 10. 15:21ใ†ํšŒ๊ณ 

๊ฐ€์งœ ์ค‘๋ณต์ด๋ž€?

๊ฐ€์งœ ์ค‘๋ณต(Accidental Duplication)์ด๋ž€ ๋‘ ๊ฐœ์˜ ์ฝ”๋“œ ์˜์—ญ์ด ์‹ค์ œ๋กœ๋Š” ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ํ•˜์ง€๋งŒ, ๊ฒ‰๋ณด๊ธฐ์—๋Š” ์ค‘๋ณต์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ์ƒํ™ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐ€์งœ ์ค‘๋ณต์„ ์ œ๋Œ€๋กœ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ณ  ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋กœ ๋ฌถ๊ฒŒ ๋˜๋ฉด, ์„œ๋กœ ๋‹ค๋ฅธ ๋งฅ๋ฝ์„ ๊ฐ€์ง„ ์ฝ”๋“œ๊ฐ€ ๊ฐ™์€ ๊ณต๊ฐ„์— ๋’ค์„ž์—ฌ ์žˆ๊ฒŒ ๋˜์–ด ์œ ์ง€๋ณด์ˆ˜์˜ ๋ณต์žก์„ฑ์ด ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด ์ด๋Ÿฐ ๊ฐ€์งœ ์ค‘๋ณต์ฝ”๋“œ๋ฅผ ์ง„์งœ ์ค‘๋ณต์ฝ”๋“œ๋กœ ํŒ๋ณ„ํ•˜์—ฌ ๋” ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ค์šด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

์˜ˆ์‹œ ์ƒํ™ฉ

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

 

๋”ฐ๋ผ์„œ ์ด ๋‘˜์„ ๊ณตํ†ต๋กœ์ง์œผ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

 

 

AS-IS: ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ

 

scwin.fn.createGridView = (searchType) => {
    // ๊ทธ๋ฆฌ๋“œ id ์ƒ์„ฑ
    
    // ๊ทธ๋ฆฌ๋“œ ํ—ค๋”, Body ์ƒ์„ฑ
    if (searchType == "๊ธฐ์ค€๋…„๋„") {
        // ...
    } else if (searchType == "์‚ฌ์—…๊ณ„ํš") {
        // ...
    }   
    
    // ๊ทธ๋ฆฌ๋“œ subTotal ์ƒ์„ฑ
    
    // ๊ทธ๋ฆฌ๋“œ footer ์ƒ์„ฑ
    if (searchType == "๊ธฐ์ค€๋…„๋„") {
        // ...
    } else if (searchType == "์‚ฌ์—…๊ณ„ํš") {
        // ...
    }   
}

 

 

์ด ๋ฐฉ์‹์˜ ๋ฌธ์ œ์ :

  1. ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ถ”๊ฐ€๋œ๋‹ค๋ฉด if (searchType == "์‚ฌ์—…๊ณ„ํš" && isGroup == true && ...) ์™€ ๊ฐ™์ด ๋ณต์žก์„ฑ์ด ์ฆ๊ฐ€ํ•œ๋‹ค.
  2. ์ถ”ํ›„ ์‚ฌ์—…๊ณ„ํš๋งŒ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ๊ธฐ์ค€๋…„๋„์— ๋Œ€ํ•œ ๋กœ์ง์„ ๊ฑด๋“œ๋ฆด ์œ„ํ—˜์ด ์žˆ์–ด ์ˆ˜์ •์ด ์–ด๋ ค์›Œ์ง„๋‹ค.
  3. ์ƒˆ๋กœ์šด ์กฐํšŒ ๊ธฐ์ค€์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด, if๋ฌธ ๊ฐœ์ˆ˜๋งŒํผ ๋ถ„๊ธฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๊ณ  ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ ์•ˆ์— ๋ชจ๋“  ์กฐํšŒ ๊ธฐ์ค€ ๋กœ์ง์ด ์žˆ์–ด ๋ณ€๊ฒฝ์˜ ์—ฌํŒŒ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.

 

TO-BE: ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„๊ณผ ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•œ๋‹ค.

 

scwin.fn.createGridView = (searchType) => {
    // ๊ณตํ†ต๋กœ์ง
    // 1. ๊ทธ๋ฆฌ๋“œ id ์ƒ์„ฑ
    // 2. ๊ทธ๋ฆฌ๋“œ css ์„ค์ •
    // 3. ๊ทธ๋ฆฌ๋“œ ๋ ˆ์ด์•„์›ƒ ์„ค์ •
    
    if (searchType == "๊ธฐ์ค€๋…„๋„") {
         ({ header, body, footer_row } = scwin.fn.getBaseYearGridViewTemplate(service, isGroup));
    } else if (searchType == "์‚ฌ์—…๊ณ„ํš") {
        ({ header, body, footer_row } = scwin.fn.getGoalYearGridViewTemplate(service, isGroup));
    }
    
    // ๊ณตํ†ต๋กœ์ง
    // 4. ๊ทธ๋ฆฌ๋“œ ์ƒ์„ฑ
}

scwin.fn.getBaseYearGridViewTemplate = (service, isGroup) => {
    // ๊ทธ๋ฆฌ๋“œ ํ—ค๋”, ๋ฐ”๋””, ํ‘ธํ„ฐ ์ƒ์„ฑ ํ›„ ๋ฐ˜ํ™˜
}

scwin.fn.getGoalYearGridViewTemplate = (service, isGroup) => {
    // ๊ทธ๋ฆฌ๋“œ ํ—ค๋”, ๋ฐ”๋””, ํ‘ธํ„ฐ ์ƒ์„ฑ ํ›„ ๋ฐ˜ํ™˜
}

 

 

์ด ๋ฐฉ์‹์˜ ์žฅ์ :

  1. ์‚ฌ์—…๊ณ„ํš์— ๋Œ€ํ•œ ์ˆ˜์ •์€ getGoalYearGridViewTemplate ๋ฉ”์„œ๋“œ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.
  2. ๊ธฐ์ค€๋…„๋„์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์—ฌํŒŒ๋ฅผ ์‹ ๊ฒฝ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  3. ์ƒˆ๋กœ์šด ์กฐํšŒ ๊ธฐ์ค€ ์ถ”๊ฐ€ ์‹œ ๋‹ค๋ฅธ ๊ธฐ์ค€์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์—ฌํŒŒ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

Java์—์„œ์˜ ์ ์šฉ: ๋‹คํ˜•์„ฑ ํ™œ์šฉ

Java์—์„œ๋Š” ๋‹คํ˜•์„ฑ์„ ํ™œ์šฉํ•˜์—ฌ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹คํ˜•์„ฑ์ด๋ž€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํƒ€์ž…์„ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ค.

 

abstract class ComparisonRuleService {
    public void getGridView() {
        // ๊ณตํ†ต๋กœ์ง ์‹คํ–‰
        
        // ์ถ”์ƒ๋ฉ”์„œ๋“œ getGridViewTemplate ํ˜ธ์ถœ.
    }
    
    public abstract void getGridViewTemplate();
}

class BaseYearRuleService extends ComparisonRuleService {
    @Override
    public void getGridViewTemplate() {
        // ๊ธฐ์ค€๋…„๋„ ๊ทธ๋ฆฌ๋“œ ๋ทฐ ๋ฐ˜ํ™˜
    }
}

class GoalYearRuleService extends ComparisonRuleService {
    @Override
    public void getGridViewTemplate() {
        // ์‚ฌ์—…๊ณ„ํš ๊ทธ๋ฆฌ๋“œ ๋ทฐ ๋ฐ˜ํ™˜
    }
}

 

 

ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ

 

class Client {
    private final ComparisonRuleService comparisonRuleService;
        
    public Client(ComparisonRuleService service) {
        // Public ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ.
        service.getGridView();
    }   
}

 

 

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

์ด๋Ÿฌํ•œ ํ˜•ํƒœ๋ฅผ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ํŒจํ„ด์ด๋ผ๊ณ  ํ•œ๋‹ค.

๊ฒฐ๋ก 

์š”๊ตฌ์‚ฌํ•ญ์„ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” ์ตœ์†Œํ•œ์˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ๊ณผ ํ™•์žฅ ๊ฐ€๋Šฅ์„ฑ์„ ์—ผ๋‘์— ๋‘๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๋ฏธ๋ž˜์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋” ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ๊ณ , ์ฝ”๋“œ์˜ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.