当前位置:网站首页>Policy mode - unity
Policy mode - unity
2022-07-07 19:05:00 【Just be interesting】
List of articles
The strategy pattern
The strategy pattern is an object behavior pattern . This mode Define a series of algorithms , Encapsulate it , Make these algorithms complete specific tasks , Can replace each other . Through a scenario class , Separate its algorithm implementation from business logic , Dynamically switch different algorithms for management .
Strategic patterns are also very common in life , Such as the way of travel , When you need to reach a destination , We need a way to achieve , Cycling , Take the bus, etc , They all achieve the same task , But the way of implementation is quite different . In the program , Sorting is very common , Different sorting methods have different specific ( Like fast platoon , Homing , Stacking and so on ), Through different scenes , We can choose these sorts flexibly .
structure

explain
- Abstract strategy (IStrategy)- Declare the general interface of the algorithm , Delegate algorithm implementation to subclasses .
- Specific strategies (Concrete Strategy)- Implement specific algorithms .
- scene (Context)- Dependent policy references , Do not realize the function , The method responsible for calling the policy interface .
The algorithm itself needs to be independent
Realization
Example : Scene: the switch (Scene Switching)
Scene: the switch ( Abstract strategy )
public interface ISceneSwitch
{
void SceneSwitch();
}
Switch algorithm ( Specific strategies )
// Fade switch
public class FadeSwitch : ISceneSwitch
{
public void SceneSwitch()
{
Debug.Log("Scene Fade Switch");
}
}
// Circular switch
public class CircularSwitch : ISceneSwitch
{
public void SceneSwitch()
{
Debug.Log("Circular Scene Switch");
}
}
scene ( scene )
public class Scene
{
private ISceneSwitch _sceneSwitch;
public ISceneSwitch SceneSwitch
{
set => _sceneSwitch = value;
}
public Scene(ISceneSwitch sceneSwitch)
{
_sceneSwitch = sceneSwitch;
}
public void SceneSwitching()
{
_sceneSwitch.SceneSwitch();
}
}
call
public class StrategyExample : MonoBehaviour
{
private void Start()
{
Scene scene = new Scene(new FadeSwitch());
scene.SceneSwitching();
scene.SceneSwitch = new CircularSwitch();
scene.SceneSwitching();
}
}

Application scenarios
- When the client needs dynamic switching algorithm , And different algorithms realize a specific task
- When a large number of conditional branch statements are used to perform tasks , We can use strategy patterns
- The algorithm itself needs to be separated from the business logic , And the algorithm implementation does not depend on context
Advantages and disadvantages
advantage
- Avoid using multiple branch statements , Such as if…else,switch…case etc. , The code is simpler .
- It satisfies the open close principle , When extending the new algorithm , There is no need to change the client code .
- Separate business logic and Algorithm
shortcoming
- Make the system more complex
- The client itself needs to understand the differences between different policy algorithms
The difference from other models
- The bridge and Strategy Very similar , And they delegate tasks to reference objects , But the essence is different , Bridging focuses on separating functions , Interact with each implementation reference through abstract classes , To complete the corresponding functions , The essence is to combine various functions into a large structure , And the idea is mainly to replace inheritance by composition to complete the separation of abstraction and implementation . The strategic model focuses on only one , That's it The algorithms can switch between each other in operation , And they don't interfere with each other , Algorithm implementation and business logic separation .
- Template method and Strategy They are all different implementations of algorithms . But the idea is different , The template method itself is on an algorithm skeleton , Rewrite specific steps , The algorithm itself is sequential . And the algorithm implementation of strategy , It can be completely different , As long as you complete a specific task . Template methods are class inheritance , Strategy is a combination of objects . Template method algorithm is static , The strategy algorithm is dynamic .
- state and Strategy It's all about function switching at runtime . Function switching of state , It is mainly the state change that makes the function switch , The strategy is to actively switch . There is no clear separation between function realization and state switching , It can cooperate with the strategic mode , Realize the separation of implementation and switching .
边栏推荐
- Nat address translation
- PIP related commands
- Mathematical analysis_ Notes_ Chapter 11: Fourier series
- 企业展厅设计中常用的三种多媒体技术形式
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- "Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
- Kubernetes DevOps CD工具对比选型
- POJ 1182 :食物链(并查集)[通俗易懂]
- Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
- 微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
猜你喜欢

3.关于cookie

Basic concepts and properties of binary tree

低代码助力企业数字化转型会让程序员失业?

SD_ DATA_ RECEIVE_ SHIFT_ REGISTER

How to choose the appropriate automated testing tools?

99% of people don't know that privatized deployment is also a permanently free instant messaging software!

虚拟数字人里的生意经

Static routing configuration

伺服力矩控制模式下的力矩目标值(fTorque)计算

Mathematical analysis_ Notes_ Chapter 11: Fourier series
随机推荐
10 schemes to ensure interface data security
云安全日报220707:思科Expressway系列和网真视频通信服务器发现远程攻击漏洞,需要尽快升级
The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t
Five network IO models
线程池的拒绝策略
静态路由配置
PIP related commands
The highest level of anonymity in C language
CVPR 2022丨学习用于小样本语义分割的非目标知识
2022年推荐免费在线接收短信平台(国内、国外)
【Unity Shader】插入Pass实现模型遮挡X光透视效果
【MIME笔记】
手把手教姐姐写消息队列
2022-07-04 matlab reads video frames and saves them
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
Review of network attack and defense
String type, constant type and container type of go language
PTA 1102 教超冠军卷
学习open62541 --- [67] 添加自定义Enum并显示名字