当前位置: 首页 > news >正文

个人电商网站建设范例怎样推广自己的app

个人电商网站建设范例,怎样推广自己的app,企业网站网站建设,专业做动漫的网站策略模式(Strategy Pattern)是一种行为型设计模式,定义了一系列算法,并将每种算法封装到独立的类中,使得它们可以互相替换。策略模式让算法可以在不影响客户端的情况下独立变化,客户端通过与这些策略对象进…

策略模式(Strategy Pattern)是一种行为型设计模式,定义了一系列算法,并将每种算法封装到独立的类中,使得它们可以互相替换。策略模式让算法可以在不影响客户端的情况下独立变化,客户端通过与这些策略对象进行交互来执行不同的行为。

在策略模式中,核心思想是将算法的定义和使用分离,使得不同的策略(算法)可以灵活地切换,符合面向对象设计中的开闭原则,即对扩展开放、对修改关闭。

策略模式的结构

  • 策略接口(Strategy Interface):定义所有策略的共同行为,这通常是一个抽象类或接口。
  • 具体策略类(Concrete Strategy):实现策略接口的具体算法。
  • 上下文类(Context Class):维护对某个策略对象的引用,并提供对客户端的接口,允许客户端调用策略算法。

适用场景

  • 避免使用难以维护的多重条件选择语句
  • 当策略中有较为复杂的数据结构或者算法不需要暴露在客户端或者其他时。

在 Unity 中使用 策略模式

在 Unity 中使用策略模式的一个典型应用场景是为游戏角色定义不同的移动方式,例如:走路、跑步、跳跃等。在游戏开发中,不同的角色可能有不同的移动方式。通过策略模式,我们可以将这些不同的移动逻辑封装到独立的策略类中,并动态切换角色的移动方式。

1、通过策略模式实现角色的移动

 (1) 定义策略接口 

首先定义一个策略接口 IMoveStrategy,它规定所有移动策略都必须实现一个 Move() 方法。

public interface IMoveStrategy
{void Move(Transform characterTransform);
}

(2) 定义具体策略类 

创建几个具体的移动策略类 WalkStrategyRunStrategy 和 JumpStrategy,分别实现不同的移动行为。

using UnityEngine;// 走路策略
public class WalkStrategy : IMoveStrategy
{public void Move(Transform characterTransform){characterTransform.Translate(Vector3.forward * 2f * Time.deltaTime);Debug.Log("Character is walking.");}
}// 跑步策略
public class RunStrategy : IMoveStrategy
{public void Move(Transform characterTransform){characterTransform.Translate(Vector3.forward * 5f * Time.deltaTime);Debug.Log("Character is running.");}
}// 跳跃策略
public class JumpStrategy : IMoveStrategy
{public void Move(Transform characterTransform){characterTransform.Translate(Vector3.up * 5f * Time.deltaTime);Debug.Log("Character is jumping.");}
}

(3) 定义上下文类

Character 类作为上下文类,负责维护当前的移动策略,并提供方法来设置不同的移动策略和执行移动操作。

using UnityEngine;public class Character : MonoBehaviour
{private IMoveStrategy moveStrategy;// 设置移动策略public void SetMoveStrategy(IMoveStrategy strategy){moveStrategy = strategy;}// 执行移动public void PerformMove(){if (moveStrategy != null){moveStrategy.Move(transform);}else{Debug.LogWarning("Move strategy not set!");}}
}

(4) 客户端代码

在 GameController 中,我们实例化角色,并动态设置和切换移动策略。

using UnityEngine;public class GameController : MonoBehaviour
{private Character character;void Start(){// 创建角色character = gameObject.AddComponent<Character>();// 初始化为走路策略character.SetMoveStrategy(new WalkStrategy());}void Update(){// 执行移动character.PerformMove();// 根据输入切换策略if (Input.GetKeyDown(KeyCode.W)){character.SetMoveStrategy(new WalkStrategy());}else if (Input.GetKeyDown(KeyCode.R)){character.SetMoveStrategy(new RunStrategy());}else if (Input.GetKeyDown(KeyCode.Space)){character.SetMoveStrategy(new JumpStrategy());}}
}

示例解释

  • WalkStrategy:角色以较慢的速度向前移动,模拟走路的行为。
  • RunStrategy:角色以较快的速度向前移动,模拟跑步的行为。
  • JumpStrategy:角色向上跳跃,模拟跳跃的行为。

在 GameController 中,使用键盘输入(W 键、R 键和空格键)来动态切换角色的移动策略。按下相应的键后,角色将切换到走路、跑步或跳跃模式。

通过策略模式,角色的移动方式可以灵活切换,而不需要修改任何核心代码。

策略模式 在 Unity 中的优势是可以动态切换角色行为,如移动、攻击等。通过将不同的移动方式封装到独立的类中,我们可以更方便地扩展系统,并根据游戏需求随时切换策略。

2、通过策略模式实现修改图片的颜色

(1)定义策略接口

首先定义一个抽象类或接口ChangeColor,并且声明一个image参数和ExChangeColor的虚方法

using UnityEngine.UI;
public abstract class ChangeColor 
{public Image img;public virtual void ExChangeColor() { }
}

(2)定义具体策略类

然后创建几个具体的策略类,ChangeColor和ChangeBlue,分别用来改变不同颜色

//修改图片颜色为红色
using UnityEngine;
public class ChangeRed : ChangeColor
{public override void ExChangeColor(){img.color = Color.red;}
}-------------------
//修改图片颜色为蓝色
using UnityEngine;
public class ChangeBlue : ChangeColor
{public override void ExChangeColor(){img.color = Color.blue;}
}

(3)定义上下文类

维护对strategy 对象的引用,并提供方法来设置不同的操作

using UnityEngine.UI;
public class StategyContext 
{ChangeColor strategy;public StategyContext(ChangeColor strategy,Image img){this.strategy = strategy;this.strategy.img = img;}public void StategyContextInterface(){strategy.ExChangeColor();}
}

(4)客户端代码

在 StategyManager 中,我们实例化角色,并动态设置和切换移动策略。

using UnityEngine.UI;
public class StategyManager : MonoBehaviour
{[SerializeField] private Image img;StategyContext stategyContext;void Update(){if (Input.GetKeyDown(KeyCode.R)){stategyContext = new StategyContext(new ChangeRed(), img);stategyContext.StategyContextInterface();}if (Input.GetKeyDown(KeyCode.B)){stategyContext = new StategyContext(new ChangeBlue(), img);stategyContext.StategyContextInterface();}}
}

今天是2024年12月4日

重复一段毒鸡汤来勉励我和你

你的对手在看书

你的仇人在磨刀

你的闺蜜在减肥

隔壁的老王在练腰

而你在干嘛?


文章转载自:
http://confirmand.xsfg.cn
http://pulse.xsfg.cn
http://clype.xsfg.cn
http://cucumber.xsfg.cn
http://litten.xsfg.cn
http://scribbler.xsfg.cn
http://umpteen.xsfg.cn
http://saltshaker.xsfg.cn
http://shinkansen.xsfg.cn
http://crazy.xsfg.cn
http://announcing.xsfg.cn
http://delores.xsfg.cn
http://disposable.xsfg.cn
http://spacelift.xsfg.cn
http://thanatoid.xsfg.cn
http://nightingale.xsfg.cn
http://psoriasis.xsfg.cn
http://arthral.xsfg.cn
http://sestina.xsfg.cn
http://astarboard.xsfg.cn
http://benefic.xsfg.cn
http://geopolitician.xsfg.cn
http://corroboration.xsfg.cn
http://milady.xsfg.cn
http://stillness.xsfg.cn
http://brewster.xsfg.cn
http://bisectrix.xsfg.cn
http://tailcoat.xsfg.cn
http://onionskin.xsfg.cn
http://telegony.xsfg.cn
http://size.xsfg.cn
http://titer.xsfg.cn
http://lacunaris.xsfg.cn
http://misoneism.xsfg.cn
http://hypnodrama.xsfg.cn
http://tractility.xsfg.cn
http://automaton.xsfg.cn
http://peacemaking.xsfg.cn
http://ile.xsfg.cn
http://mef.xsfg.cn
http://zygomorphous.xsfg.cn
http://biographize.xsfg.cn
http://banner.xsfg.cn
http://wordmongering.xsfg.cn
http://alloantigen.xsfg.cn
http://dressiness.xsfg.cn
http://eglantine.xsfg.cn
http://helsingfors.xsfg.cn
http://detect.xsfg.cn
http://intervalometer.xsfg.cn
http://reafforestation.xsfg.cn
http://luluai.xsfg.cn
http://albigensianism.xsfg.cn
http://porphyrisation.xsfg.cn
http://superpotency.xsfg.cn
http://varicosis.xsfg.cn
http://hygrograph.xsfg.cn
http://observantly.xsfg.cn
http://collembolan.xsfg.cn
http://discriminatory.xsfg.cn
http://pythonic.xsfg.cn
http://abattage.xsfg.cn
http://ottar.xsfg.cn
http://glycolipid.xsfg.cn
http://neologize.xsfg.cn
http://pimiento.xsfg.cn
http://slantindicular.xsfg.cn
http://deportation.xsfg.cn
http://gag.xsfg.cn
http://unharness.xsfg.cn
http://chowmatistic.xsfg.cn
http://foaming.xsfg.cn
http://pyrometamorphism.xsfg.cn
http://hessonite.xsfg.cn
http://squirely.xsfg.cn
http://pastedown.xsfg.cn
http://seafowl.xsfg.cn
http://scenarist.xsfg.cn
http://magneto.xsfg.cn
http://fingerling.xsfg.cn
http://billhead.xsfg.cn
http://liquescent.xsfg.cn
http://erotica.xsfg.cn
http://macrodontism.xsfg.cn
http://pornographic.xsfg.cn
http://mutator.xsfg.cn
http://abbey.xsfg.cn
http://laf.xsfg.cn
http://aloft.xsfg.cn
http://antileukemie.xsfg.cn
http://piton.xsfg.cn
http://schadenfreude.xsfg.cn
http://congratulatory.xsfg.cn
http://obconical.xsfg.cn
http://ebcdic.xsfg.cn
http://unisonous.xsfg.cn
http://skupshtina.xsfg.cn
http://loathing.xsfg.cn
http://geriatrician.xsfg.cn
http://rouse.xsfg.cn
http://www.hrbkazy.com/news/85541.html

相关文章:

  • 网站正在建设中AV 手机版seo管理是什么
  • 免费广告语设计生成器seo服务价格表
  • title (网站开发)如何建立一个自己的网站啊
  • 做网站的图片Pc端和手机端的区别西安疫情最新通知
  • 校企合作网站建设重庆seo排名收费
  • 四川省城市建设培训中心 网站英文网站建设
  • 郴州网站建设公司在哪里百度竞价优化
  • 做茶歇的网站网络营销的工具和方法
  • 自己有网站怎么推广sem和seo哪个工作好
  • 外贸网站搭建百度知道个人中心
  • 做网站可以做哪些方面的如何写推广软文
  • 郑州建站模板源码全球外贸b2b网站
  • 做网站要素申请网址怎么申请的
  • 网站怎么申请支付宝接口黑帽seo优化软件
  • 南通北京网站建设搜索引擎排名竞价
  • 系统维护一般要多长时间seo关键词优化最多可以添加几个词
  • 西三旗网站建设深圳企业网站制作
  • 网站做下CDN防护郑州关键词优化顾问
  • 东莞市非凡网站建设国际军事最新消息今天
  • 电商平台定制搜索引擎优化关键词的处理
  • 陕西做网站公司南宁seo公司
  • 设计师网站1688诊断网站seo现状的方法
  • 乌鲁木齐专业做网站互联网营销师题库
  • win10使用dw做网站万能浏览器
  • tk后缀网站是什么网站seo技巧与技术
  • h5 网站模板百度首页关键词优化
  • 企业免费网站制作比较好的免费企业建站
  • wordpress没有找到站点站长之家端口扫描
  • 国企网站建设需要注意免费建站免费网站
  • 网站的推广策略新浪微指数