当前位置:网站首页>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 .
边栏推荐
- 【剑指 Offer】59 - I. 滑动窗口的最大值
- The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
- PTA 1102 teaching Super Champion volume
- 10 schemes to ensure interface data security
- Will low code help enterprises' digital transformation make programmers unemployed?
- 【塔望方法论】塔望3W消费战略 - U&A研究法
- 6.关于jwt
- 脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
- How many times is PTA 1101 B than a
- 3.关于cookie
猜你喜欢

Wechat web debugging 8.0.19 replace the X5 kernel with xweb, so the X5 debugging method can no longer be used. Now there is a solution

小试牛刀之NunJucks模板引擎

2022上半年朋友圈都在传的10本书,找到了

面试唯品会实习测试岗、抖音实习测试岗【真实投稿】

我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
![[C language] string function](/img/6c/c77e8ed5bf383b7c656f45b361940f.png)
[C language] string function

The highest level of anonymity in C language

静态路由配置

In 2021, the national average salary was released. Have you reached the standard?

嵌入式C语言程序调试和宏使用的技巧
随机推荐
50亿,福建又诞生一只母基金
3.关于cookie
Charles+Postern的APP抓包
ip netns 命令(备忘)
线程池和单例模式以及文件操作
【Base64笔记】「建议收藏」
Redis
Redis publishing and subscription
SQLite SQL exception near "with": syntax error
将模型的记忆保存下来!Meta&UC Berkeley提出MeMViT,建模时间支持比现有模型长30倍,计算量仅增加4.5%...
脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
3. About cookies
如何选择合适的自动化测试工具?
go语言的字符串类型、常量类型和容器类型
How much does it cost to develop a small program mall?
Basic operation of chain binary tree (implemented in C language)
【HDU】5248-序列变换(贪心+二分)「建议收藏」
数据验证框架 Apache BVal 再使用
String type, constant type and container type of go language
[unity shader] insert pass to realize the X-ray perspective effect of model occlusion