손 훑 기 golang 구조 설계 원칙 개폐 원칙

3482 단어
golang 구조 설계 원칙 개폐 원칙
발단
최근 디자인 모드 복습 담 용 덕 의 < > 이 책 은 자바 언어 로 흔 한 디자인 모드 를 연 기 했 습 니 다.
개폐 원칙
  • 개폐 원칙 (Open - Closed Principle, OCP) 은 클래스, 모듈, 함수 와 같은 소프트웨어 실체 가 확장 에 개방 되 고 수정 에 대해 닫 아야 한 다 는 것 을 말한다.개폐 란 두 행 위 를 확장 하고 수정 하 는 원칙 이다.
  • 개폐 원칙 을 실현 하 는 핵심 사상 은 추상 적 인 프로 그래 밍 이다.

  • 장면
  • 모 온라인 학습 플랫폼 에서 시리즈 과정 제품 (인터페이스: ICourse)
  • 을 제공한다.
  • 각 과정 은 id, name, price 등 속성
  • 이 있다.
  • 현재 플랫폼 판 촉, 골 랑 과정 (골 랑 코스) 60% 할인
  • 할인 수업 은 어떻게 합 니까?원 골 랑 과정의 가격 을 직접 수정 하 시 겠 습 니까? 아니면 할인 후 골 랑 과정 을 늘 리 시 겠 습 니까?

  • 사고의 방향
  • 개폐 원칙 은 수정 을 피하 고 확장 하 는 방식 으로 시스템 기능 의 증 가 를 실현 하 는 것 이다
  • .
  • '할인' 인터페이스 추가 - IDiscount
  • '접 은 골 랑 과정' - Discounted 골 랑 코스 를 추가 하고 과정 인터페이스 와 할인 인터페이스
  • 를 실현 합 니 다.
  • Discounted Golang Course 는 Golang Course 에서 계승 하여 할인 인 터 페 이 스 를 추가 하고 ICourse. price () 방법
  • 을 덮어 씁 니 다.
    ICourse.go
    principles/open_close / ICourse. go 과정 인터페이스
    package open_close
    
    type ICourse interface {
        ID() int
        Name() string
        Price() float64
    }

    GolangCourse.go
    principles/open_close / Golang Course. gogolang 과정 류, ICourse 인터페이스 실현
    package open_close
    
    type GolangCourse struct {
        iID int
        sName string
        fPrice float64
    }
    
    
    func NewGolangCourse(id int, name string, price float64) ICourse {
        return &GolangCourse{
            iID: id,
            sName: name,
            fPrice: price,
        }
    }
    
    func (me *GolangCourse) ID() int {
        return me.iID
    }
    
    func (me *GolangCourse) Name() string {
        return me.sName
    }
    
    func (me *GolangCourse) Price() float64 {
        return me.fPrice
    }

    IDiscount.go
    principles/open_close / IDiscount. go 할인 인터페이스
    package open_close
    
    type IDiscount interface {
        Discount() float64
    }

    DiscountedGolangCourse.go
    principles/open_close / Discounted Golang Course. go 이 과정 은 ICourse 와 IDiscount 인 터 페 이 스 를 동시에 실현 합 니 다.
    package open_close
    
    type DiscountedGolangCourse struct {
        GolangCourse
        fDiscount float64
    }
    
    func NewDiscountedGolangCourse(id int, name string, price float64, discount float64) ICourse {
        return &DiscountedGolangCourse{
            GolangCourse: GolangCourse{
                iID:    id,
                sName:  name,
                fPrice: price,
            },
    
            fDiscount : discount,
        }
    }
    
    // implements IDiscount.Discount
    func (me *DiscountedGolangCourse) Discount() float64 {
        return me.fDiscount
    }
    
    // overwrite ICourse.Price
    func (me *DiscountedGolangCourse) Price() float64 {
        return me.fDiscount * me.GolangCourse.Price()
    }

    open_close_test.go
    main/open_close_test. go 과정 인터페이스 테스트 사례
    package main
    
    import (
        "testing"
    )
    import (ocp "learning/gooop/principles/open_close")
    
    func Test_open_close(t  *testing.T) {
        fnShowCourse := func(it ocp.ICourse) {
            t.Logf("id=%v, name=%v, price=%v
    ", it.ID(), it.Name(), it.Price()) } c1 := ocp.NewGolangCourse(1, "golang ", 100) fnShowCourse(c1) c2 := ocp.NewDiscountedGolangCourse(2, "golang ", 100, 0.6) fnShowCourse(c2) }

    테스트
    $> go test -v main/open_close_test.go 
    === RUN   Test_open_close
        open_close_test.go:10: id=1, name=golang  , price=100
        open_close_test.go:10: id=2, name=golang    , price=60
    --- PASS: Test_open_close (0.00s)
    PASS
    ok      command-line-arguments  0.001s
    

    좋은 웹페이지 즐겨찾기