ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • #14 ๋งค์ผ_๋กœ๊ทธ
    About Lemon/๋งค์ผ์˜_์ผ์ง€ 2020. 1. 14. 22:45

    #์ƒํ™œ_๋กœ๊ทธ

    ๊ผผ๊ผผํ•˜๊ฒŒ ๋””์ž์ธ์„ ์ฒดํฌํ•˜์ง€ ์•Š์•„์„œ ... ๊ทธ ๋Œ“๊ฐ€๋ฅผ ํ˜น๋…ํ•˜๊ฒŒ ์น˜๋ฅด๊ณ  ์žˆ๋‹คใ…œ ์š”์ƒˆ ๋‚˜ ์™œ์ด๋Ÿฌ๋‹ˆ ์ •์‹ ์ฐจ๋ ค! ์ฐธ ์—ฌ๋Ÿฌ๋ชจ๋กœ ๋‚˜ ์ž์‹ ์—๊ฒŒ ์‹ค๋งํ•˜๊ณ  ์†์ƒํ•œ ํ•˜๋ฃจ์˜€๋‹ค...  ๊ผผ๊ผผํ•˜๊ณ  ์ •๋ง ํ•„์š”ํ•  ๋•Œ๋งŒ ์˜์‚ฌ์†Œํ†ต์„ ํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋˜๊ณ  ์‹ถ๋‹ค... ใ… .ใ…  ์†์ƒํ•ด

     

    #์˜ค๋Š˜์˜_๊ฐœ๋ฐœ

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

     

    #์˜ค๋Š˜์˜_๋…์„œ

     

    #๋ฆฌํŒฉํ† ๋ง

    ์–ด์ œ switch ๋ฌธ์„ ์ œ๊ฑฐํ•˜๊ณ  ๊ฐ์ฒด์ง€ํ–ฅ ์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ฐ•์ ์ธ ๋‹คํ˜•์„ฑ์„ ์‚ด๋ฆฌ๊ธฐ ์œ„ํ•ด, Type Code ( ๋ณดํ†ต์€ Enum์œผ๋กœ ๊ตฌํ˜„)์„ ํด๋ž˜์Šค/ํ•˜์œ„ํด๋ž˜์Šค/์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฒ•์„ ๋ฐฐ์› ๋‹ค. 

     

    ์ด์ œ ์ •๋ง๋กœ switch๋ฅผ ์ œ๊ฑฐํ•  ์ฐจ๋ก€๋‹ค.

     

    ์กฐ๊ฑด๋ฌธ์„ ์žฌ์ •์˜๋กœ ์ „ํ™˜ Replace Conditiaonal with Polymorphism

    ๋ง ๊ทธ๋Œ€๋กœ if ๋ฌธ๊ณผ switch ๋ฌธ์„, ๊ทธ ์กฐ๊ฑด๊ณผ switch ์— ์‚ฌ์šฉ๋˜๋Š” ํƒ€์ž…์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์„ ์žฌ์ •์˜๋กœ ์ •์˜ํ•ด์„œ ๋ฐ”๊พผ๋‹ค. ์›๋ž˜๋Š” ์ด๋ฆฌ์ €๋ฆฌ ๋ถ„๊ธฐ๋ฅผ ํƒ€๋˜ ์ฝ”๋“œ๊ฐ€ ์ž˜ ๋ถ„๋ฆฌ ๋œ ํด๋ž˜์Šค๋“ค๊ณผ, ํด๋ž˜์Šค์˜ ๊ณตํ†ต ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœํ•˜๋Š”  ํ•œ ์ค„์˜ ์ฝ”๋“œ๋กœ ๋ฐ”๋€Œ๋Š” ๋งค์ง์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค๋Š” ๋งŽ์•„์ง€์ง€๋งŒ, ๊ฐ€๋…์„ฑ์€ UP!

     

    ๋ณ€๊ฒฝ ์ „. ์–ธ์–ด๋Š” Swift

    enum Job {
        case engineer
        case designer
        case systemArchitector
    }
    
    class Person
    {
        var job : Job
        
        init(aJob: Job) {
            job = aJob
        }
        
        func selfIntroduction() {
            
            //์ง€๊ธˆ์€ ๋ช‡ ๊ฐœ ์—†์–ด์„œ ๊ดœ์ฐฎ์ง€๋งŒ, ๋‚˜์ค‘์— ๊ธฐ๋Šฅ์ด ๋งŽ์•„์ง€๊ฑฐ๋‚˜ ๋ณต์žกํ•˜๋ฉด ์•Œ์•„๋ณด๊ธฐ๊ฐ€ ํž˜๋“ค์–ด ์งˆ ๊ฒƒ์ด๋‹ค :(
            switch self.job {
            case .engineer:
                print("I'm an engineer")
                self.doEngineer()
            case .designer:
                print("I'm a designer")
                print("here's my portpolio")
                self.doDesign()
            case .systemArchitector:
                print("I'm a systemArchitector")
                print("Hmm... What could we do for Our Customer")
                self.doMakeBluePrint()
            }
        }
        
        func doEngineer()
        {
            //do somehing awesome
        }
        
        func doDesign()
        {
            //do somehing awesome
        }
        
        func doMakeBluePrint()
        {
            //do somehing awesome
        }
    }
    
    
    
    let eng1 = Person(aJob: .engineer)
    let des1 = Person(aJob: .designer)
    let sys1 = Person(aJob: .systemArchitector)
    
    eng1.selfIntroduction()
    des1.selfIntroduction()
    sys1.selfIntroduction()
    

     

    ๋ณ€๊ฒฝ ํ›„

    //Swift๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ protocol๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. 
    protocol JobClass
    {
        func selfIntroduction()
    }
    
    //ํŒŒ์ผ ๋‹จ์œ„๋กœ ๊ตฌ๋ถ„ ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ ์กฐ๊ธˆ ๋” ์ข‹์„ ๊ฑฐ ๊ฐ™๋‹ค.
    class Engineer : JobClass
    {
        func selfIntroduction() {
            print("I'm an engineer")
        }
        
    }
    
    class Designer : JobClass
    {
        func selfIntroduction() {
            print("I'm a designer")
            print("here's my portpolio")
        }
        
        
    }
    
    //์ด์ œ ๋ณ€๊ฒฝ์ด ์ƒ๊ฒจ๋„ ๊ทธ๊ฑธ ๊ตณ์ด Person2 ํด๋ž˜์Šค๊ฐ€ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค.
    class SysmtemArchitect : JobClass
    {
        func selfIntroduction() {
            print("I'm a systemArchitector")
                      print("Hmm... What could we do for Our Customer")
            
            print("WOW! Something awesome changes")
        }
        
        
    }
    
    
    class Person2
    {
        var job : JobClass
        
        init(aJob: JobClass) {
            job = aJob
        }
        
        //์จ˜! ์งง์•„์กŒ๋‹ค.
        func selfIntroduction() {
            self.job.selfIntroduction()
        }
    }
    
    let eng2 = Person2(aJob: Engineer())
    let des2 = Person2(aJob: Designer())
    let sys2 = Person2(aJob: SysmtemArchitect())
    

     

    #์˜ค๋Š˜์˜_๊ฒฝ์ œ

    ์˜ค๋Š˜์€ ์Šคํ‚ตํ–ˆ๋‹ค... ์ค‘๊ฐ„ ์ค‘๊ฐ„ ๋ดค๋˜ ๋‰ด์Šค์—์„œ ๋‘ ๊ฐ€์ง€๋งŒ ๊ฑด์กŒ๋‹ค.

    CES 2020 ๋‰ด์Šค์—์„œ ๋กœ๋ด‡๊ณผ mobility

    ๋Œ“๊ธ€

Designed by Tistory. @Bola Kim