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

网站建设工作室赚钱吗社交网络的推广方法有哪些

网站建设工作室赚钱吗,社交网络的推广方法有哪些,做国际网站怎么发货,网站推广排名怎么做我们在开发时经常会遇到一堆的if else …, 或者switch, 比如我们常见的全局异常处理等, 像类似这种很多if else 或者多场景模式下, 策略模式是非常受欢迎的一种设计模式, 然而, 一个好的策略模式却不是那么容易写出来. 我在工作中也因为写烦了switch,if else 觉得很不优雅, 因…

我们在开发时经常会遇到一堆的if else …, 或者switch, 比如我们常见的全局异常处理等, 像类似这种很多if else 或者多场景模式下, 策略模式是非常受欢迎的一种设计模式, 然而, 一个好的策略模式却不是那么容易写出来.

我在工作中也因为写烦了switch,if else 觉得很不优雅, 因此,我考虑是否有一套统一的解决方案呢?

思考我问题的初衷, 在什么策略下, 满足什么条件执行什么动作, 返回什么值, 这就是策略模式需要解决的核心问题, 大眼一看好似有点类似状态机? 然而它并不是状态机, 状态机是比较笨重, 而策略机应该是足够轻量的.

我们再来看核心问题,关于什么策略,满足什么条件执行什么动作,返回什么值, 是一个明显的dsl语法, 因此, 我的基本语法糖已确立: strategy.of().when().perform()或者strategy.of().perform(), 因为有时我们并不需要条件, 仅仅策略即可.

我们要实现上述语法糖, 就得设计一套规则, 使其可以满足dsl, 并且是合理的, 如此, 基本定义已确定, 如下:

/*** {@link StrategyMachineBuilder}* @param <S> strategy* @param <C> context* @param <R> result*/
public interface StrategyMachineBuilder<S,C,R> {Of<S,C,R> of(S s);StrategyMachine<S,C,R> build(String id);
}
/*** {@link Of}* @param <S> strategy* @param <C> context* @param <R> result*/
public interface Of<S,C,R> {When<S,C,R> when(Condition<S,C,R> condition);StrategyMachineBuilder<S,C,R> perform(Action<C,R> action);
}
/*** {@link When}* @param <S> strategy* @param <C> context* @param <R> result*/
public interface When<S,C,R> {StrategyMachineBuilder<S,C,R> perform(Action<C,R> action);
}

/*** {@link Condition}* @param <S> strategy* @param <C> context* @param <R> result*/
public interface Condition<S,C,R> {boolean isSatisfied(S s,C c);
}
/*** {@link Action}* @param <C> context* @param <R> result*/
public interface Action<C,R> {R apply(C c);
}
/*** {@link Strategy}* @param <S> strategy* @param <C> context* @param <R> result*/
public interface Strategy<S, C, R> {S strategy();Condition<S,C,R> condition();Action<C, R> action();Strategy<S,C,R> strategy(S s);Strategy<S,C,R> condition(Condition<S,C,R> condition);Strategy<S,C,R> action(Action<C,R> action);
}
/*** {@link StrategyMachine}* @param <S> strategy* @param <C> context* @param <R> result*/
public interface StrategyMachine<S,C,R> {R apply(S s, C c);
}

如此: 架构已经构建完毕, 剩下的工作就很简单了, 实现此架构即可.

/*** {@link StrategyMachineBuilderImpl}* @param <S> strategy* @param <C> context* @param <R> result*/
class StrategyMachineBuilderImpl<S,C,R> implements StrategyMachineBuilder<S,C,R>{private final Map<S, List<Strategy<S,C,R>>> map = new ConcurrentHashMap<>();@Overridepublic Of<S, C, R> of(S s) {map.computeIfAbsent(s, k -> new ArrayList<>());Strategy<S,C,R> strategy = new StrategyImpl();map.get(s).add(strategy);return new OfImpl(strategy);}@Overridepublic StrategyMachine<S, C, R> build(String id) {StrategyMachineImpl<S, C, R> machine = new StrategyMachineImpl<>(map);StrategyCache.put(id, machine);return machine;}public class OfImpl implements Of<S,C,R>{private final Strategy<S,C,R> strategy;OfImpl(Strategy<S,C,R> strategy){this.strategy = strategy;}@Overridepublic When<S, C, R> when(Condition<S,C,R> condition) {this.strategy.condition(condition);return new WhenImpl(strategy);}@Overridepublic StrategyMachineBuilder<S, C, R> perform(Action<C, R> action) {this.strategy.action(action);return StrategyMachineBuilderImpl.this;}}public class WhenImpl implements When<S,C,R> {private final Strategy<S,C,R> strategy;WhenImpl(Strategy<S,C,R> strategy){this.strategy = strategy;}@Overridepublic StrategyMachineBuilder<S, C, R> perform(Action<C, R> action) {this.strategy.action(action);return StrategyMachineBuilderImpl.this;}}public class StrategyImpl implements Strategy<S, C, R> {private S strategy;private Condition<S,C,R> condition;private Action<C, R> action;@Overridepublic S strategy() {return this.strategy;}@Overridepublic Condition<S,C,R> condition() {return this.condition;}@Overridepublic Action<C, R> action() {return this.action;}@Overridepublic Strategy<S, C, R> strategy(S s) {this.strategy = s;return this;}@Overridepublic Strategy<S, C, R> condition(Condition<S,C,R> condition) {this.condition = condition;return this;}@Overridepublic Strategy<S, C, R> action(Action<C, R> action) {this.action = action;return this;}}
}
/*** Strategy Machine Impl* @param <S> strategy* @param <C> context* @param <R> result*/
class StrategyMachineImpl<S,C,R> implements StrategyMachine<S,C,R> {private final Map<S, List<Strategy<S,C,R>>> map;public StrategyMachineImpl(Map<S, List<Strategy<S,C,R>>> map){this.map = map;}@Overridepublic R apply(S s, C c) {List<Strategy<S, C, R>> strategies = map.get(s);if (strategies==null||strategies.isEmpty()){throw new RuntimeException("no strategy found for "+s);}for (Strategy<S, C, R> strategy : strategies) {// 如果没有condition,直接执行actionif (strategy.condition()==null) {return strategy.action().apply(c);}// 如果有condition,先判断是否满足condition,满足则执行actionif (strategy.condition().isSatisfied(s,c)){return strategy.action().apply(c);}}// 未发现策略关于s的conditionthrow new RuntimeException("no strategy found of met condition for "+s);}
}
/*** Strategy Machine Factory*/
public class StrategyMachineFactory {public static <S,C,R> StrategyMachineBuilder<S,C,R> create() {return new StrategyMachineBuilderImpl<>();}public static <S,C,R> StrategyMachine<S,C,R> get(String id) {return (StrategyMachine<S, C, R>) StrategyCache.get(id);}
}
/*** {@link StrategyCache}*/
class StrategyCache {private static final Map<String,StrategyMachine<?,?,?>> CACHE = new java.util.concurrent.ConcurrentHashMap<>();public static void put(String id, StrategyMachine<?,?,?> machine) {CACHE.put(id, machine);}public static StrategyMachine<?,?,?> get(String id) {return CACHE.get(id);}
}

如此, 策略机已实现完毕. 下面给出两种场景例子
一. 不同年龄吃不同分量的药
Example:
Under the age of 12, take 20 milligrams of medication per day;
12-18 years old, taking 30 milligrams a day
18-30 years old, taking 40 milligrams a day
30-50 years old, taking 45 milligrams a day
Eating 42 milligrams for those over 50 years old

class MedicineStrategy {private static StrategyMachine<String, MedicineContext, Void> strategy;static {StrategyMachineBuilder<String, MedicineContext, Void> machineBuilder = StrategyMachineFactory.create();strategy = machineBuilder.of("").when((s, c) -> c.age < 12).perform((c) -> {System.out.println("Under the age of 12, take 20 milligrams of medication per day;");return Void.TYPE.cast(null);}).of("").when((s, c) -> c.age >= 12 && c.age < 18).perform((c) -> {System.out.println("12-18 years old, taking 30 milligrams a day");return Void.TYPE.cast(null);}).of("").when((s, c) -> c.age >= 18 && c.age < 30).perform((c) -> {System.out.println("18-30 years old, taking 40 milligrams a day");return Void.TYPE.cast(null);}).of("").when((s, c) -> c.age >= 30 && c.age < 50).perform((c) -> {System.out.println("30-50 years old, taking 45 milligrams a day");return Void.TYPE.cast(null);}).of("").when((s, c) -> c.age >= 50).perform((c) -> {System.out.println("Eating 42 milligrams for those over 50 years old");return Void.TYPE.cast(null);}).build("medicine");}public static StrategyMachine<String, MedicineContext, Void> get() {// StrategyMachine<String, MedicineContext, Void> strategy = StrategyMachineFactory.get("medicine");return strategy;}@Data@AllArgsConstructor@NoArgsConstructorpublic static class MedicineContext {private int age;}public static void main(String[] args) {get().apply("", new MedicineContext(10));}}

二. 计算机

		StrategyMachineBuilder<String, StrategyContext, Number> machineBuilder = StrategyMachineFactory.create();machineBuilder.of("加法").perform(strategyContext -> strategyContext.a + strategyContext.b);machineBuilder.of("减法").perform(strategyContext -> strategyContext.a - strategyContext.b);machineBuilder.of("乘法").perform(strategyContext -> strategyContext.a * strategyContext.b);// 除法,当c==1时,忽略小数位, 当c==2时不忽略machineBuilder.of("除法").when((s, strategyContext) -> strategyContext.c == 1).perform(strategyContext -> strategyContext.a / strategyContext.b);machineBuilder.of("除法").when((s, strategyContext) -> strategyContext.c == 2).perform(strategyContext -> (strategyContext.a * 1.0d) / (strategyContext.b * 1.0d));StrategyMachine<String, StrategyContext, Number> strategyMachine = machineBuilder.build("test");// StrategyMachine<String, StrategyContext, Number> strategyMachine =  StrategyMachineFactory.get("test");System.out.println(strategyMachine.apply("加法", new StrategyContext(1, 2, 1)));System.out.println(strategyMachine.apply("减法", new StrategyContext(1, 2, 1)));System.out.println(strategyMachine.apply("乘法", new StrategyContext(1, 2, 1)));System.out.println(strategyMachine.apply("除法", new StrategyContext(1, 2, 1)));System.out.println(strategyMachine.apply("除法", new StrategyContext(1, 2, 2)));

源码地址: https://github.com/zhangpan-soft/dv-commons


文章转载自:
http://apocalypticist.cwgn.cn
http://gloomily.cwgn.cn
http://leptosome.cwgn.cn
http://helpless.cwgn.cn
http://buttercup.cwgn.cn
http://chive.cwgn.cn
http://joual.cwgn.cn
http://differentiability.cwgn.cn
http://kilometric.cwgn.cn
http://hydric.cwgn.cn
http://horsebreaker.cwgn.cn
http://lime.cwgn.cn
http://tendrac.cwgn.cn
http://barnacles.cwgn.cn
http://quadrillionth.cwgn.cn
http://necrobiosis.cwgn.cn
http://idyllize.cwgn.cn
http://apyrexia.cwgn.cn
http://splitting.cwgn.cn
http://unindexed.cwgn.cn
http://auspex.cwgn.cn
http://hythergraph.cwgn.cn
http://bourg.cwgn.cn
http://symbion.cwgn.cn
http://corriedale.cwgn.cn
http://laevulose.cwgn.cn
http://neurular.cwgn.cn
http://heortology.cwgn.cn
http://mexico.cwgn.cn
http://oddly.cwgn.cn
http://mrc.cwgn.cn
http://stabilify.cwgn.cn
http://nemesis.cwgn.cn
http://leatherette.cwgn.cn
http://necessarily.cwgn.cn
http://nonius.cwgn.cn
http://maudlin.cwgn.cn
http://springbok.cwgn.cn
http://jager.cwgn.cn
http://prostaglandin.cwgn.cn
http://begetter.cwgn.cn
http://delphin.cwgn.cn
http://superradiance.cwgn.cn
http://prepensely.cwgn.cn
http://drenching.cwgn.cn
http://spinachy.cwgn.cn
http://trogon.cwgn.cn
http://orcin.cwgn.cn
http://zoometric.cwgn.cn
http://gault.cwgn.cn
http://relater.cwgn.cn
http://texturize.cwgn.cn
http://psion.cwgn.cn
http://primateship.cwgn.cn
http://premeiotic.cwgn.cn
http://pneumotropism.cwgn.cn
http://successful.cwgn.cn
http://isotropism.cwgn.cn
http://retardance.cwgn.cn
http://tradespeople.cwgn.cn
http://coiffure.cwgn.cn
http://meekly.cwgn.cn
http://akene.cwgn.cn
http://antiimperialism.cwgn.cn
http://noology.cwgn.cn
http://muonic.cwgn.cn
http://cresting.cwgn.cn
http://tomo.cwgn.cn
http://quechua.cwgn.cn
http://prohibition.cwgn.cn
http://perceptibly.cwgn.cn
http://cryoelectronics.cwgn.cn
http://deck.cwgn.cn
http://hydrogenate.cwgn.cn
http://troublemaking.cwgn.cn
http://forget.cwgn.cn
http://roland.cwgn.cn
http://averroism.cwgn.cn
http://lightstruck.cwgn.cn
http://famously.cwgn.cn
http://laced.cwgn.cn
http://rumba.cwgn.cn
http://digraph.cwgn.cn
http://seaward.cwgn.cn
http://balanoid.cwgn.cn
http://distinguish.cwgn.cn
http://recondite.cwgn.cn
http://nautch.cwgn.cn
http://headplate.cwgn.cn
http://iraqi.cwgn.cn
http://toupet.cwgn.cn
http://digger.cwgn.cn
http://hexabiose.cwgn.cn
http://aphrodisia.cwgn.cn
http://hardened.cwgn.cn
http://lithemic.cwgn.cn
http://morel.cwgn.cn
http://spatiality.cwgn.cn
http://milliosmol.cwgn.cn
http://counselee.cwgn.cn
http://www.hrbkazy.com/news/57820.html

相关文章:

  • 北航刘禹导师做网站北京seo服务商找行者seo
  • 做淘宝网站怎么弄的百度热搜榜排名今日p2p
  • 建设项目公示网站百度代理查询
  • 公司网站开发流程舆情分析报告案例
  • 张云网站建设手机系统优化软件
  • 做文件的网站哈尔滨网络优化公司有哪些
  • 便利的微网站建设淘宝网店怎么运营起来
  • 做自己的网站网络营销案例分析题及答案
  • 男女做爰视频网站广州线下培训机构停课
  • 武汉政府网站建设关键词查询网址
  • 湛江市住房和城乡建设网站关键词优化公司
  • 在线做拓扑图的网站万能的搜索引擎
  • 云服务器是干嘛用的百度搜索引擎优化公司哪家强
  • 许昌企业网站去哪开发网络营销现状分析
  • wordpress网站根目录搜索引擎排名规则
  • 自己怎么优化网站排名中国十大电商平台有哪些
  • 南阳网站公司广告免费发布信息
  • 建设网站平台站长统计 站长统计
  • 零售网站有哪些平台seo优化名词解释
  • 响应式网站用什么工具网络营销推广的渠道有哪些
  • 做理财的网站有哪些内容seo兼职
  • 深圳网站搭建找哪里百度统计app下载
  • 做房产应看的网站河南seo快速排名
  • 上海网站建设市场分析windows优化大师功能
  • 简述建设政府门户网站原因苏州做网站哪家比较好
  • 上海市有哪些公司seo培训中心
  • 做侵权电影网站什么后果哪个好用?
  • 做网站开发没有人带爱站seo综合查询
  • 怎么看网站是哪家公司做的最常见企业网站公司有哪些
  • 自制网址显示指定内容江苏seo网络