Chain of Responsibility Pattern

What Is a Chain of Responsibility Pattern

The Chain of Responsibility pattern performs a lot like it sounds. If you have a group of classes that are all interdependent on each other and each performs a particular piece of processing, this pattern is a very useful tool. It makes sure each member in a chain controls when and to what class it hands its processing to. The pattern controls the code flow by using inheritance and by allowing instances of its predecessors to be loaded one inside the other, forming in effect a chain of classes that hand off to the next class in the chain at the end of its turn.

The Chain of Responsibility pattern has a single main component: the Handler. The handler object encapsulates the entire chain of handler instances inside each other instance. In other words, the first link in the chain contains the next and that link contains each consecutive link. As you move through each link’s process and that process finishes, it automatically checks for the existence of the next consecutive link and calls the process of that next link. This can happen no matter which level of link in the chain you call and will continue down the chain until the chain ends.
UML for Chain of Responsibility pattern

Comparison to Similar Patterns
Depending on whether you wish to provide a set operational cycle or you want to change that cycle on the fly might influence your decision to use the Chain of Responsibility pattern or a Command pattern. The Command pattern holds an order of operations in its command queue, but allows ad-hoc calls to any operation desired via the command object. In contrast, the Chain of Responsibility pattern more rigidly defines the order of operations. Your particular usage depends on which is more suitable to your needs. The Chain of Responsibility also is like the Composite pattern in that parsing through the class chain is like moving through each collection class in the composite. Both patterns move through class methods in a serial fashion.

What We Have Learned
The Chain of Responsibility pattern seems to be quite a good way to link classes that have interdependence on each other in an order of operations. You could implement the pattern in an approval cycle, where people needed to approve specific requests in turn. You could use it to determine an order of operations for executing nearly any kind of code. This order can easily be changed at run time or in different code instances, depending on the need.
### Unity 设计模式与最佳实践 #### 单例模式 (Singleton Pattern) 单例模式确保一个类只有一个实例,并提供全局访问点。这在游戏开发中特别有用,用于管理资源和服务。 ```csharp public class GameManager : MonoBehaviour { private static GameManager _instance; public static GameManager Instance { get { if (_instance == null) { _instance = FindObjectOfType<GameManager>(); if(_instance == null){ GameObject singletonObject = new GameObject(); _instance = singletonObject.AddComponent<GameManager>(); singletonObject.name = "GameManager"; } } return _instance; } } void Awake() { if (_instance != this && _instance != null) { Destroy(gameObject); } else { _instance = this; DontDestroyOnLoad(gameObject); } } } ``` 此实现方式可以防止多个 `GameManager` 实例存在并确保其在整个应用程序生命周期内保持有效[^2]。 #### 责任链模式 (Chain of Responsibility Pattern) 责任链模式允许对象将请求沿处理者链条传递给其他对象直到有一个能处理该请求的对象为止。这种设计非常适合事件响应机制,在Unity中可用于输入管理和消息分发系统。 #### 工厂方法模式 (Factory Method Pattern) 工厂方法模式定义了一个创建对象的接口,但让子类决定实例化哪一个类。这种方法使得代码更加灵活易于扩展而不需要修改现有逻辑结构。 ```csharp using System; public abstract class EnemySpawner : MonoBehaviour { protected virtual IEnemy CreateEnemy(Vector3 position) => throw new NotImplementedException(); } // Concrete Factory public class BasicEnemySpawner : EnemySpawner { override protected IEnemy CreateEnemy(Vector3 position) { var enemyObj = Instantiate(Resources.Load<GameObject>("BasicEnemy"), position, Quaternion.identity); return enemyObj.GetComponent<IEnemy>(); } } ``` 这段代码展示了如何通过继承来定制具体的敌人生成行为而不改变基础框架. #### 组合模式 (Composite Pattern) 组合模式使客户端能够一致地对待单独对象和对象容器。对于构建层次化的场景图或UI组件树非常有帮助。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值