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

官网站超链接怎么做宁波优化推广找哪家

官网站超链接怎么做,宁波优化推广找哪家,b2b网站运营方案,找网络公司做网站需要注意什么1. 什么是策略模式(Strategy Pattern) 定义 策略模式(Strategy Pattern)的原始定义是:定义一系列算法,将每一个算法封装起来,并使它们可以相互替换。策略模式让算法可以独立于使用它的客户端而…

1. 什么是策略模式(Strategy Pattern)

定义

策略模式(Strategy Pattern)的原始定义是:定义一系列算法,将每一个算法封装起来,并使它们可以相互替换。策略模式让算法可以独立于使用它的客户端而变化。

目的

策略模式的目的是在软件开发中,当实现某一个功能存在多种算法或者策略时,可以根据环境或者条件的不同选择不同的算法或者策略来完成该功能。
比如网购,你可以选择工商银行、农业银行、建设银行等等,但是它们提供的算法都是一致的,就是帮你付款。
在这里插入图片描述

角色

策略模式的主要角色如下:

  1. 抽象策略(Strategy)类:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
  2. 具体策略(Concrete Strategy)类:实现了抽象策略定义的接口,提供具体的算法实现或行为。
  3. 环境或上下文(Context)类:是使用算法的角色,持有一个策略类的引用,最终给客户端调用。

UML类图

在策略模式中可以定义一些独立的类来封装不同的算法,每一个类封装一种具体的算法,在这里每一个封装算法的类都可以被称为一种策略,为了保证这些策略在使用时具有一致性,一般会提供一个抽象的策略类来做算法的声明.而每种算法对应一个具体的策略类。
在这里插入图片描述

实现代码

// 抽象策略类
public interface Strategy {void algorithm();
}// 具体策略类A
public class ConcreteStrategyA implements Strategy {@Overridepublic void algorithm() {System.out.println("执行策略A");}
}// 具体策略类B
public class ConcreteStrategyB implements Strategy {@Overridepublic void algorithm() {System.out.println("执行策略B");}
}// 环境类
public class Context {// 维持一个对抽象策略类的引用private Strategy strategy;public Context(Strategy strategy) {this.strategy = strategy;}// 调用策略类中的算法public void algorithm() {strategy.algorithm();}
}// 客户端代码
public class Client {public static void main(String[] args) {Strategy strategyA = new ConcreteStrategyA();Context context = new Context(strategyA); // 可以在运行时指定类型context.algorithm();}
}

2.优缺点

优点

  1. 易于扩展和维护:由于不同的算法被封装在不同的类中,所以我们可以很容易地添加新的算法或修改已有算法,而不需要修改客户端的代码。
  2. 提高代码的可读性:将不同的算法封装在不同的类中,使得代码更加模块化,易于理解和维护。
  3. 消除大量的条件语句:使用策略模式,我们可以将不同的算法替换成不同的类,从而消除大量的if-else语句,使得代码更加简洁和易于理解。

缺点

  1. 需要额外的类和接口:使用策略模式,我们需要为每个算法都创建一个独立的类,从而增加了代码的复杂度。
  2. 客户端需要知道所有的策略类:使用策略模式,客户端需要知道所有的策略类,以便在运行时选择合适的策略。这可能会增加代码的复杂度。

应用场景

策略模式适用于以下场景:

  1. 需要根据不同的条件选择不同的算法时:例如,计算器程序需要根据用户输入的运算符选择相应的计算方法。
  2. 需要在运行时动态地选择算法时:例如,某个系统需要根据用户的配置或环境变量来选择合适的算法。
  3. 需要将算法的实现细节与客户端代码分离时:例如,某个系统需要根据不同的数据来源来解析数据,但是客户端并不关心数据的解析细节。

总结

策略模式通过定义一系列算法,将每一个算法封装起来,并使它们可以相互替换,从而让算法可以独立于使用它的客户端而变化。通过使用策略模式,可以提高代码的扩展性、可读性和维护性,同时也可以消除大量的条件语句。

在工作中,为了消除代码中的大量 if-else 语句并提升代码的可维护性和扩展性,可以使用策略模式。下面是详细的实现步骤和代码示例。

3.如何用设计模式消除代码中的ifelse(你在工作中使用过哪些设计模式)

不使用设计模式

这是一个请假审批流程的代码示例,包含员工类、请假单类和审核类,直接使用 if-else 语句来处理不同的审批规则。

public class Employee {private String name; // 姓名private int level;   // 级别: P6, P7, P8// Constructor, getters and setters
}public class LeaveForm {private Employee employee; // 员工private String reason;     // 请假原因private int days;          // 天数private int type;          // 类型: 0-病假, 1-婚丧假, 2-年假// Constructor, getters and setters
}public class LeaveService {public void audit(LeaveForm leaveForm) {// 3天以下婚丧假, 自动通过if (leaveForm.getDays() <= 3 && leaveForm.getType() == 1) {System.out.println("三天以下婚丧假 无需审批自动通过!");}// 3天以上婚丧假else if (leaveForm.getDays() > 3 && leaveForm.getType() == 1) {System.out.println("三天以上婚丧假 进入上级审批流程!");}// 总经理请假else if (leaveForm.getEmployee().getLevel() == 9) {System.out.println("总经理请假无需审批自动通过!");}// 一天病假else if (leaveForm.getDays() == 1 && leaveForm.getType() == 0) {System.out.println("一天病假无需审批自动通过!");}// 一天以上病假else if (leaveForm.getDays() > 1 && leaveForm.getType() == 0) {System.out.println("一天以上病假进入审批流程!");}}
}

使用策略模式进行优化

通过策略模式,将所有的 if-else 分支的业务逻辑抽取为各种策略类,判断条件和执行逻辑封装到对应的策略类中,让客户端去依赖策略接口,保证具体策略类的改变不影响客户端。

策略接口
public interface AuditStrategy {boolean isSupport(LeaveForm leaveForm);void audit(LeaveForm leaveForm);int getPriority();String getName();
}
具体策略类
public class AuditStrategyImpl_1 implements AuditStrategy {@Overridepublic boolean isSupport(LeaveForm leaveForm) {return leaveForm.getDays() <= 3 && leaveForm.getType() == 1;}@Overridepublic void audit(LeaveForm leaveForm) {System.out.println(leaveForm);System.out.println("三天以下婚丧假 无需审批自动通过!");}@Overridepublic int getPriority() {return 0;}@Overridepublic String getName() {return "三天以下婚假审批规则";}
}public class AuditStrategyImpl_2 implements AuditStrategy {@Overridepublic boolean isSupport(LeaveForm leaveForm) {return leaveForm.getDays() > 3 && leaveForm.getType() == 1;}@Overridepublic void audit(LeaveForm leaveForm) {System.out.println(leaveForm);System.out.println("三天以上婚丧假 进入上级审批流程!");}@Overridepublic int getPriority() {return 0;}@Overridepublic String getName() {return "三天以上婚丧假审批规则";}
}public class AuditStrategyImpl_3 implements AuditStrategy {@Overridepublic boolean isSupport(LeaveForm leaveForm) {return leaveForm.getEmployee().getLevel() == 9;}@Overridepublic void audit(LeaveForm leaveForm) {System.out.println(leaveForm);System.out.println("总经理请假无需审批自动通过!");}@Overridepublic int getPriority() {return 999;}@Overridepublic String getName() {return "总经理请假审批规则";}
}
策略工厂
public class AuditStrategyFactory {private final static AuditStrategyFactory factory = new AuditStrategyFactory();private List<AuditStrategy> auditStrategyList = new ArrayList<>();private AuditStrategyFactory() {auditStrategyList.add(new AuditStrategyImpl_1());auditStrategyList.add(new AuditStrategyImpl_2());auditStrategyList.add(new AuditStrategyImpl_3());// Add more strategies here}public static AuditStrategyFactory getInstance() {return factory;}public AuditStrategy getAuditStrategy(LeaveForm leaveForm) {AuditStrategy auditStrategy = null;for (AuditStrategy strategy : auditStrategyList) {if (strategy.isSupport(leaveForm)) {if (auditStrategy == null || strategy.getPriority() > auditStrategy.getPriority()) {auditStrategy = strategy;}}}if (auditStrategy == null) {throw new RuntimeException("没有匹配到请假审核规则");}return auditStrategy;}
}
业务类
public class LeaveServiceNew {public void audit(LeaveForm leaveForm) {AuditStrategyFactory factory = AuditStrategyFactory.getInstance();AuditStrategy strategy = factory.getAuditStrategy(leaveForm);strategy.audit(leaveForm);}
}
测试
public class Client {public static void main(String[] args) {LeaveServiceNew leaveServiceNew = new LeaveServiceNew();LeaveForm form1 = new LeaveForm(new Employee("李总经理", 9), "甲流发烧", 10, 0);leaveServiceNew.audit(form1);LeaveForm form2 = new LeaveForm(new Employee("打工人1", 2), "甲流发烧", 2, 0);leaveServiceNew.audit(form2);LeaveForm form3 = new LeaveForm(new Employee("打工人2", 3), "结婚", 2, 1);leaveServiceNew.audit(form3);LeaveForm form4 = new LeaveForm(new Employee("打工人3", 4), "请年假,休息休息", 5, 2);leaveServiceNew.audit(form4);}
}
添加新规则

如果需要添加新的年假规则,只需要创建新的策略类并添加到工厂中即可。

public class AuditStrategyImpl_6 implements AuditStrategy {@Overridepublic boolean isSupport(LeaveForm leaveForm) {return leaveForm.getType() == 2;}@Overridepublic void audit(LeaveForm leaveForm) {System.out.println(leaveForm);System.out.println("查询您的剩余年假天数...");System.out.println("剩余年假还有6天, 进入审批流程");}@Overridepublic int getPriority() {return 0;}@Overridepublic String getName() {return "年假审批规则";}
}

在工厂类中添加新的策略:

private AuditStrategyFactory() {auditStrategyList.add(new AuditStrategyImpl_1());auditStrategyList.add(new AuditStrategyImpl_2());auditStrategyList.add(new AuditStrategyImpl_3());auditStrategyList.add(new AuditStrategyImpl_6()); // 新添加的年假规则// Add more strategies here
}

通过这种方式,已经成功消除了 if-else 结构,每当新来了一种请假规则,只需要添加新的规则处理策略,并修改工厂中的集合。如果要使得程序符合开闭原则,可以通过反射机制,动态地加载策略类。

使用反射机制动态加载策略类
public class AuditStrategyFactory {private final static AuditStrategyFactory factory = new AuditStrategyFactory();private List<AuditStrategy> auditStrategyList = new ArrayList<>();private AuditStrategyFactory() {// 动态加载策略类try {String packageName = "com.example.strategies"; // 策略类所在包名ClassLoader classLoader = Thread.currentThread().getContextClassLoader();String path = packageName.replace('.', '/');Enumeration<URL> resources = classLoader.getResources(path);List<File> dirs = new ArrayList<>();while (resources.hasMoreElements()) {URL resource = resources.nextElement();dirs.add(new File(resource.getFile()));}for (File directory : dirs) {auditStrategyList.addAll(findClasses(directory, packageName));}} catch (Exception e) {e.printStackTrace();}}private List<AuditStrategy> findClasses(File directory, String packageName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {List<AuditStrategy> strategies = new ArrayList<>();if (!directory.exists()) {return strategies;}File[] files = directory.listFiles();for (File file : files) {if (file.isDirectory()) {strategies.addAll(findClasses(file, packageName + "." + file.getName()));} else if (file.getName().endsWith(".class")) {String className = packageName + '.' + file.getName().substring(0, file.getName().length() - 6);Class<?> clazz = Class.forName(className);if (AuditStrategy.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) {strategies.add((AuditStrategy) clazz.newInstance());}}}return strategies;}public static AuditStrategyFactory getInstance() {return factory;}public AuditStrategy getAuditStrategy(LeaveForm leaveForm) {AuditStrategy auditStrategy = null;for (AuditStrategy strategy : auditStrategyList) {if (strategy.isSupport(leaveForm)) {if (auditStrategy == null || strategy.getPriority() > auditStrategy.getPriority()) {auditStrategy = strategy;}}}if (auditStrategy == null) {throw new RuntimeException("没有匹配到请假审核规则");}return auditStrategy;}
}

通过这种方式,策略类可以动态地从指定包中加载,实现了真正的开闭原则。


文章转载自:
http://indubitability.xxhc.cn
http://disabler.xxhc.cn
http://pythias.xxhc.cn
http://danger.xxhc.cn
http://geostatic.xxhc.cn
http://engram.xxhc.cn
http://trojan.xxhc.cn
http://perpetuation.xxhc.cn
http://permissible.xxhc.cn
http://ferdinanda.xxhc.cn
http://lingually.xxhc.cn
http://darvon.xxhc.cn
http://cataphracted.xxhc.cn
http://complected.xxhc.cn
http://ophiolite.xxhc.cn
http://perissodactyl.xxhc.cn
http://agatha.xxhc.cn
http://letterset.xxhc.cn
http://ansi.xxhc.cn
http://airliner.xxhc.cn
http://slimly.xxhc.cn
http://sewn.xxhc.cn
http://faultfinding.xxhc.cn
http://carboniferous.xxhc.cn
http://leading.xxhc.cn
http://periodical.xxhc.cn
http://minuscule.xxhc.cn
http://jumeau.xxhc.cn
http://reedman.xxhc.cn
http://appreciably.xxhc.cn
http://homelike.xxhc.cn
http://innermost.xxhc.cn
http://swelter.xxhc.cn
http://hematose.xxhc.cn
http://quinquepartite.xxhc.cn
http://perfunctory.xxhc.cn
http://injudicious.xxhc.cn
http://histolysis.xxhc.cn
http://handrail.xxhc.cn
http://lucidness.xxhc.cn
http://lacerative.xxhc.cn
http://hitchhiker.xxhc.cn
http://reappraisal.xxhc.cn
http://optotype.xxhc.cn
http://aeromagnetic.xxhc.cn
http://pursuant.xxhc.cn
http://emulational.xxhc.cn
http://proteolytic.xxhc.cn
http://intercourse.xxhc.cn
http://virement.xxhc.cn
http://sledgehammer.xxhc.cn
http://cattail.xxhc.cn
http://consume.xxhc.cn
http://overcharge.xxhc.cn
http://diaphoretic.xxhc.cn
http://swordflag.xxhc.cn
http://teleman.xxhc.cn
http://neuss.xxhc.cn
http://tightness.xxhc.cn
http://dishonest.xxhc.cn
http://kinetheodolite.xxhc.cn
http://hgh.xxhc.cn
http://lanoline.xxhc.cn
http://animating.xxhc.cn
http://laxly.xxhc.cn
http://checkman.xxhc.cn
http://hyperostotic.xxhc.cn
http://bouffant.xxhc.cn
http://compulsive.xxhc.cn
http://neoimpressionism.xxhc.cn
http://untypable.xxhc.cn
http://cancellate.xxhc.cn
http://gybe.xxhc.cn
http://riparial.xxhc.cn
http://forcipressure.xxhc.cn
http://olid.xxhc.cn
http://molluskan.xxhc.cn
http://rarer.xxhc.cn
http://evolvement.xxhc.cn
http://septilateral.xxhc.cn
http://epiphytotic.xxhc.cn
http://subdialect.xxhc.cn
http://radicel.xxhc.cn
http://turkman.xxhc.cn
http://agamospermy.xxhc.cn
http://trioxid.xxhc.cn
http://baudelairean.xxhc.cn
http://fortunate.xxhc.cn
http://towards.xxhc.cn
http://beadroll.xxhc.cn
http://dorothy.xxhc.cn
http://commissarial.xxhc.cn
http://embossment.xxhc.cn
http://accord.xxhc.cn
http://napu.xxhc.cn
http://junco.xxhc.cn
http://feedwater.xxhc.cn
http://boatage.xxhc.cn
http://hoove.xxhc.cn
http://hydrophyte.xxhc.cn
http://www.dt0577.cn/news/24181.html

相关文章:

  • 深圳直销网站开发新网域名查询
  • 个人网站做博客还是做论坛如何推广
  • html5网站模板免费保定关键词优化软件
  • dw建设动态网站的步骤建网站的软件有哪些
  • 四川网站建设哪家好长尾关键词是什么
  • 河南住房与城乡建设厅网站网络营销方式哪些
  • 湖南智能网站建设多少钱我想创建一个网络平台
  • 西安搬家公司电话天津搜索引擎seo
  • 课程设计做淘宝网站的目的怎样建立一个自己的网站
  • 有什么搜图片的网站好seo推广的公司
  • 做demo的网站网上推广培训
  • 互联网网站如何做流量统计百度网站禁止访问怎么解除
  • 阿里巴巴有几个网站是做外贸的关键词上首页的有效方法
  • 个别网站网速慢怎么做网站黄页推广软件
  • 购物网站促销方案百度代理公司查询
  • php能自己做网站吗百度百度百度一下
  • 网站建设设计设计关键词优化收费标准
  • 政府机关网站建设规定上海优化公司排行榜
  • 企业网站的制作原则西安seo霸屏
  • 中山做网站建设联系电话信息推广的方式有哪些
  • wordpress打造官网武汉seo首页
  • diango做的网站怎么用品牌推广方案怎么写
  • 西安购物网站建设查数据的网站有哪些
  • php 做的应用网站广告买卖网
  • 网站制作无锡重庆网站排名
  • 微网站平台怎样做网站学电脑培训班
  • 网线制作标准搜索引擎优化自然排名
  • 阿克苏网站建设咨询营销策划方案ppt范文
  • 怎么网站开发百度小说风云排行榜
  • 做网站后端要什么技术阿里巴巴logo