GO orientado a objetos (ser un experto en CRUD) tres: caso de implementación orientada a objetos

Código:

package domain

// 价格计算器接口
type ItemPriceCalculator interface {
   Price() int
}

type Items []*Item

// 基类商品
type Item struct {
   ID       int
   Category int
   Title    string
   Stock    int

   PriceMarket int
}

func (dom *Item) OfInstance() interface{} {
   switch dom.Category {
   case ItemCategoryRebate:
      return dom.OfInstanceRebate()
   case ItemCategoryDiscount:
      return dom.OfInstanceDiscount()
   }

   return nil
}

// 折扣商品
type ItemDiscount struct {
   *Item
}

// 实现价格计算器接口 封装变化
func (dom *ItemDiscount) Price() int {
   return dom.PriceMarket / 2
}

// 返利商品
type ItemRebate struct {
   *Item
}

// 实现价格计算器接口 封装变化
func (dom *ItemRebate) Price() int {
   return dom.PriceMarket
}

response模型: 
package response

type Item struct {
   ID          int    `json:"id"`
   Category    int    `json:"category"`
   Title       string `json:"title"`
   Stock       int    `json:"stock"`
   PriceMarket int    `json:"priceMarket"`
   Price       int    `json:"price"`
}

func (resp *Item) Mapping(dom *domain.Item) {
   resp.ID = dom.ID
   resp.Category = dom.Category
   resp.Title = dom.Title
   resp.Stock = dom.Stock
   resp.PriceMarket = dom.PriceMarket

   instance := dom.OfInstance()
   // 断言计算价格 针对接口(或抽象)编程,而不是针对细节(或实现)编程
   if priceCalculator, ok := instance.(domain.ItemPriceCalculator); ok {
      resp.Price = priceCalculator.Price()
   }
}

Análisis de código: se agregó la interfaz ItemPriceCalculator. El producto de reembolso ItemRebate y el producto de descuento ItemDiscount implementan la interfaz ItemPriceCalculator a través de su propia función de precio y calculan el precio del producto a través de la interfaz de afirmación durante la conversión de datos.

Orientado a procesos vs Orientado a objetos:

Variedad dirigido Código intuitivo
orientado al proceso si no cambio dividido para detalles Intuitivo y fácil de entender.
orientado a objetos Cambios en el aislamiento de la jerarquía de clases para la interfaz Resumen difícil de entender

Pensamiento: Orientado a procesos, un problema que puede resolverse de otra manera, el método orientado a objetos agrega una clase de interfaz y dos objetos, ¿es rentable?

Luego implementaremos el cálculo del monto del reembolso y si el precio de mercado inicial está oculto.

package domain

// 返利计算器接口
type ItemRebateCalculator interface {
   Rebate() *int
}

// 市场价是否显示接口
type ItemPriceMarketDisplay interface {
   PriceMarketDisplay() bool
}

// 实现返利计算器接口 封装变化
func (dom *ItemRebate) Rebate() *int {
   rebate := dom.PriceMarket * 5 / 100
   return &rebate
}

// 实现市场价显示接口 封装变化
func (dom *ItemRebate) PriceMarketDisplay() bool {
   return true
}


package response

// 断言计算返利
if rebateCalculator, ok := instance.(domain.ItemRebateCalculator); ok {
   resp.Rebate = rebateCalculator.Rebate()
}

// 断言市场价是否显示
if rebateCalculator, ok := instance.(domain.ItemPriceMarketDisplay); ok {
   resp.PriceMarketHidden = rebateCalculator.PriceMarketDisplay()
}

Enlace fuente

Supongo que te gusta

Origin juejin.im/post/7120509778471682085
Recomendado
Clasificación