(面向对象)业务的分层架构处理,看了这一篇文章就大致了解了。

 

例子如下:

模拟组装2台电脑。

-----抽象层:   有显卡Card方法display,有内存Memory方法storage,有处理器CPU方法calculate

-----实现层:   有intel因特尔公司,产品有(显卡、内存、cpu),有Kingston公司,产品有(内存3),有NVIDIA公司,产品有(显卡)

-----逻辑层:    1.组装一台intel系列的电脑,并运行。   2组装一台intel cpu Kingston内存NVIDIA显卡的电脑,并运行

/*
    模拟组装2台电脑
    --- 抽象层 ---
    有显卡Card  方法display
    有内存Memory 方法storage
    有处理器CPU   方法calculate

    --- 实现层层 ---
    有 Intel因特尔公司 、产品有(显卡、内存、CPU)
    有 Kingston 公司, 产品有(内存3)
    有 NVIDIA 公司, 产品有(显卡)

    --- 逻辑层 ---
    1. 组装一台Intel系列的电脑,并运行
    2. 组装一台 Intel CPU  Kingston内存 NVIDIA显卡的电脑,并运行
*/
package main

import "fmt"

//------  抽象层 -----
type Card interface{
    Display()
}

type Memory interface {
    Storage()
}

type CPU interface {
    Calculate()
}

type Computer struct {
    cpu CPU
    mem Memory
    card Card
}

func NewComputer(cpu CPU, mem Memory, card Card) *Computer{
    return &Computer{
        cpu:cpu,
        mem:mem,
        card:card,
    }
}

func (this *Computer) DoWork() {
    this.cpu.Calculate()
    this.mem.Storage()
    this.card.Display()
}

//------  实现层 -----
//intel
type IntelCPU struct {
    CPU 
}

func (this *IntelCPU) Calculate() {
    fmt.Println("Intel CPU 开始计算了...")
}

type IntelMemory struct {
    Memory
}

func (this *IntelMemory) Storage() {
    fmt.Println("Intel Memory 开始存储了...")
}

type IntelCard struct {
    Card
}

func (this *IntelCard) Display() {
    fmt.Println("Intel Card 开始显示了...")
}

//kingston
type KingstonMemory struct {
    Memory
}

func (this *KingstonMemory) Storage() {
    fmt.Println("Kingston memory storage...")
}

//nvidia
type NvidiaCard struct {
    Card
}

func (this *NvidiaCard) Display() {
    fmt.Println("Nvidia card display...")
}



//------  业务逻辑层 -----
func main() {
    //intel系列的电脑
    com1 := NewComputer(&IntelCPU{}, &IntelMemory{}, &IntelCard{})
    com1.DoWork()

    //杂牌子
    com2 := NewComputer(&IntelCPU{}, &KingstonMemory{}, &NvidiaCard{})
    com2.DoWork()
}

如果这个不行,那就在来一个例子:

package main

import "fmt"

// ===== >   抽象层  < ========
type Car interface {
    Run()
}

type Driver interface {
    Drive(car Car)
}

// ===== >   实现层  < ========
type BenZ struct {
    //...
}

func (benz * BenZ) Run() {
    fmt.Println("Benz is running...")
}

type Bmw struct {
    //...
}

func (bmw * Bmw) Run() {
    fmt.Println("Bmw is running...")
}

type Zhang_3 struct {
    //...
}

func (zhang3 *Zhang_3) Drive(car Car) {
    fmt.Println("Zhang3 drive car")
    car.Run()
}

type Li_4 struct {
    //...
}

func (li4 *Li_4) Drive(car Car) {
    fmt.Println("li4 drive car")
    car.Run()
}


// ===== >   业务逻辑层  < ========
func main() {
    //张3 开 宝马
    var bmw Car
    bmw = &Bmw{}

    var zhang3 Driver
    zhang3 = &Zhang_3{}

    zhang3.Drive(bmw)

    //李4 开 奔驰
    var benz Car
    benz = &BenZ{}

    var li4 Driver
    li4 = &Li_4{}

    li4.Drive(benz)
}

上面讲的的业务接口的一个设计,下面开始讲(小白的)业务逻辑是什么:

业务逻辑就是每个公司都有自己的处理业务的一个思维;比如说:我买一辆车,买宝马,是不是先去数据库中查询一下这辆车是否存在,如果存在就把价格,生产日期什么的都给查询出来,在进行支付,在扣除钱款,在把车辆库存给扣除一辆,在进行一张表买车人的信息登记关联到已经卖过这辆车的那张表。  (这个简单的例子就是买车的业务逻辑啊,所以说需求不一样,业务逻辑就不一样,有的公司有流程图你看一下就明白了,剩下的就是增删改查了)