设计模式---桥连模式(DesignPattern_Bridge)

DesignPattern_Bridge

摘录自:设计模式与游戏完美开发

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity Technologies——Kelvin Lo、信仁软件设计创办人—— 赖信仁、资深3D游戏美术——刘明恺 联合推荐全书采用了整合式的项目教学,即以一个游戏的范例来应用23种设计模式的实现贯穿全书,让读者学习到整个游戏开发的全过程和作者想要传承的经验,并以浅显易懂的比喻来解析难以理解的设计模式,让想深入了解此领域的读者更加容易上手。

工程GitHub

源码注释及命名有所更改,个人感觉会比原版更好理解

  • 桥梁模式:将抽象化与实现化脱耦,使得二者可以独立的变化,也就是说将他们之间的强关联变成弱关联,也就是指在一个软件系统的抽象化和实现化之间使用组合/聚合关系而不是继承关系,从而使两者可以独立的变化。
using UnityEngine;
using System.Collections;

namespace DesignPattern_Bridge
{
    // 抽象操作者基类
    public abstract class Implementor
    {
        public abstract void OperationImp();
    }

    //操作者1以及先关操作
    public class ConcreteImplementor1 : Implementor
    {
        public ConcreteImplementor1(){}

        public override void OperationImp()
        {
            Debug.Log("執行Concrete1Implementor.OperationImp");
        }
    }

    //操作者2以及先关操作
    public class ConcreteImplementor2 : Implementor
    {
        public ConcreteImplementor2(){}
        
        public override void OperationImp()
        {
            Debug.Log("執行Concrete2Implementor.OperationImp");
        }
    }

    // 扩展操作基类
    public abstract class Abstraction
    {
        private Implementor m_Imp = null;

        public void SetImplementor( Implementor Imp )
        {
            m_Imp = Imp;
        }

        public virtual void Operation()
        {
            if( m_Imp!=null)
                m_Imp.OperationImp();
        }
    }

    // 扩展操作1
    public class RefinedAbstraction1 : Abstraction
    {
        public RefinedAbstraction1(){}

        public override void Operation()
        {
            Debug.Log("物件RefinedAbstraction1");
            base.Operation();
        }
    }

    // 扩展操作2
    public class RefinedAbstraction2 : Abstraction
    {
        public RefinedAbstraction2(){}

        public override void Operation()
        {
            Debug.Log("物件RefinedAbstraction2");
            base.Operation();
        }
    }

}
using UnityEngine;
using System.Collections;
using DesignPattern_Bridge;

public class BridgeTest : MonoBehaviour
{

    void Start()
    {
        UnitTest();
    }

    // 
    void UnitTest()
    {

        // 生成扩展操作1
        Abstraction theAbstraction = new RefinedAbstraction1();

        // 设定相关操作1
        theAbstraction.SetImplementor(new ConcreteImplementor1());
        theAbstraction.Operation();

        // 设定相关操作2
        theAbstraction.SetImplementor(new ConcreteImplementor2());
        theAbstraction.Operation();

        // 生成扩展操作2
        theAbstraction = new RefinedAbstraction2();

        // 设定相关操作1
        theAbstraction.SetImplementor(new ConcreteImplementor1());
        theAbstraction.Operation();

        // 设定相关操作2
        theAbstraction.SetImplementor(new ConcreteImplementor2());
        theAbstraction.Operation();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值