코틀린 개인 과제

챌린지반 3주차 첫번째 과제: 디자인 패턴 구현

songyooho 2024. 7. 9. 20:17

과제 링크:https://teamsparta.notion.site/3-dc9daa742d604be4b7f795b458b1b6af

 

3주차 과제 | Notion

과제

teamsparta.notion.site

 

1. Singleton

object Logger{
    fun log(message:String){
        println(message)
    }
}

-object키워드를 이용해 콘솔창에 메시지를 출력하는 싱글톤 클래스를 만들음

 

2. Strategy

interface TextAlignmentStrategy{
    fun alignment(str:String,int:Int):String
}

class LeftAligment:TextAlignmentStrategy{
    override fun alignment(str: String,int: Int):String {
        return str.padEnd(int)
    }
}

class RightAlignment:TextAlignmentStrategy{
    override fun alignment(str: String, int: Int):String {
        return str.padStart(int)
    }
}

class CenterAlignment:TextAlignmentStrategy{
    override fun alignment(str: String, int: Int):String {
        return str.padStart((int+str.length)/2).padEnd(int)
    }
}

class TextEditor(){
    lateinit var txtAlignmentStrategy: TextAlignmentStrategy

    fun alignment(str: String,int: Int):String{
        return txtAlignmentStrategy.alignment(str,int)
    }

    fun setTextAlignmentStrategy(textAlignmentStrategy: TextAlignmentStrategy){
        this.txtAlignmentStrategy=textAlignmentStrategy
    }
}

-TextAlginmentStrategy 인터페이스를 생성하고 해당 인터페이스를 상속받아 좌, 우, 가운데 정렬을 하는 클래스를 생성함

-TextEditor클래스를 만들은 뒤 해당 클래스에 프로퍼티로 TextAlginmentStrategy타입의 프로퍼티를 넣은 후, 이 프로퍼티를 이용해 정렬하는 메소드를 만들음.

 

3. Observer

interface Observer{
    fun notification()
}

class Subscriber(val name:String):Observer{
    override fun notification() {
        println(this.name+": 뉴스 레터 발행 알림")
    }
}

class Newsletter{
    val subscribers=ArrayList<Observer>()

    fun registerObserver(observer: Observer){
        subscribers+=observer
    }

    fun removeObserver(observer: Observer){
        subscribers.remove(observer)
    }

    fun notification(){
        subscribers.forEach{it.notification()}
    }
}

-Observer 인터페이스를 작성 후 해당 인터페이스를 상속받는 Subscriber 클래스 생성

-Subscriber클래스엔 알림을 오도록하는 메소드 작성

-Newsletter클래스에는 Observer들의 리스트를 저장하는 프로퍼티를 만들고 Observer들의 추가, 제거를 관리하는 메소드를 작성함

-각 Oberver에 알림을 보내는 메소드 작성

 

4. Decorator

interface Coffee{
    fun deco()
}

class SimpleCoffee:Coffee{
    override fun deco() {
        println("기본 커피")
    }
}

abstract class Decorator(val coffee: Coffee):Coffee{
    override fun deco() {
        coffee.deco()
    }
}

class MilkDecorator(coffee: Coffee):Decorator(coffee){
    override fun deco() {
        super.deco()
        milk()
    }

    fun milk(){
        println("우유 추가")
    }
}

class SyrupDecorator(coffee: Coffee):Decorator(coffee){
    override fun deco() {
        super.deco()
        syrup()
    }

    fun syrup(){
        println("시럽 추가")
    }
}

class WhipCreamDecorator(coffee: Coffee):Decorator(coffee){
    override fun deco() {
        super.deco()
        whipCream()
    }

    fun whipCream(){
        println("휘핑크림 추가")
    }
}

-커피 인터페이스와 기본 베이스역할을 하는 SimpleCoffee 인터페이스를 만들음

-이후 데코레이터는 그냥 Coffee를 상속받아도 되지만 깔끔하게 하기위해서 추상클래스를 만들은 뒤 우유,시럽,휘핑크림에 해당하는 데코레이터 클래스를 만들음.

-데코레이터는 Coffee를 인자로 받아서 deco실행시 인자로 받은 Coffee객체의 deco를 먼저 실행되도록 함.

'코틀린 개인 과제' 카테고리의 다른 글

AppleMarket 구현 과제  (0) 2024.07.15
회원가입 MVVM 과제  (0) 2024.07.14
로그인 앱 제작-2  (0) 2024.06.25
로그인 앱 제작  (0) 2024.06.19
키오스크 제작  (0) 2024.06.13