您现在的位置是:主页 > news > 购物网站哪个是正品/百度知道一下

购物网站哪个是正品/百度知道一下

admin2025/5/8 17:26:46news

简介购物网站哪个是正品,百度知道一下,做网站开发的有外快嘛,腾讯云如何建设网站【定义】 定义一组算法,不同具体实现之间可以随意插拔替换。我们可以动态地改变具体的实现。 可实现动态插拔 策略模式本身强调的是不同策略对相同数据的不同实现和处理策略,实现策略的热插拔替换。实际上,在使用过程中,策略的热…

购物网站哪个是正品,百度知道一下,做网站开发的有外快嘛,腾讯云如何建设网站【定义】 定义一组算法,不同具体实现之间可以随意插拔替换。我们可以动态地改变具体的实现。 可实现动态插拔 策略模式本身强调的是不同策略对相同数据的不同实现和处理策略,实现策略的热插拔替换。实际上,在使用过程中,策略的热…

【定义】

定义一组算法,不同具体实现之间可以随意插拔替换。我们可以动态地改变具体的实现。

可实现动态插拔 

策略模式本身强调的是不同策略对相同数据的不同实现和处理策略,实现策略的热插拔替换。实际上,在使用过程中,策略的热插拔不是靠人工判断的,而是在执行策略的前一步还有一个选择策略的过程,一般是:

1. 选择合适的策略模型

2. 填充选择的模式,使用该模型处理数据

而策略的选择可以采用工厂模式,抽象工厂模式,或者条件过滤模式。

【实现】

设计算法的模式:

public interface IChoice
{void myChoice(String s1, String s2);
}

具体实现算法1(把两个字符串转换成int数字并求和):

public class FirstChoice implements IChoice
{public void myChoice(String s1, String s2){System.out.println("You wanted to add the numbers.");int int1, int2,sum;int1=Integer.parseInt(s1);int2=Integer.parseInt(s2);sum=int1+int2;System.out.println(" The result of the addition is:"+sum);System.out.println("***End of the strategy***");}
}

具体实现算法2(字符串连接):

public class SecondChoice implements IChoice
{public void myChoice(String s1, String s2){System.out.println("You wanted to concatenate the numbers.");System.out.println(" The result of the addition is:"+s1+s2);System.out.println("***End of the strategy***");}
}

定义一个类,内部含有模式接口:

public class Context
{IChoice myC;// 为接口的具体实现提供动态插拔方法public void SetChoice(IChoice ic){myC = ic;}public void ShowChoice(String s1,String s2){myC.myChoice(s1,s2);}
}

使用:

public static void main(String [] args)
{IChoice ic = null;Context ctx = new Context();ic = new FirstChoice();ctx.setChoice(ic);ctx.showChoice("123","456");ic = new SecondChoice();ctx.setChoice(ic);ctx.showChoice("123","456");}