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

林管局网站建设方案b2b网站大全免费

林管局网站建设方案,b2b网站大全免费,门户网站建设教程,长春政府网站开发策略模式定义 比如对象的某个行为,在不同场景有不同实现方式,可以将这些行为的具体实现定义为一组策略,每个实现类实现种策略,在不同场景使用不同的实现,并且可以自由切换策略。 策略模式结构 策略模式需要一个策略…

策略模式定义

比如对象的某个行为,在不同场景有不同实现方式,可以将这些行为的具体实现定义为一组策略,每个实现类实现种策略,在不同场景使用不同的实现,并且可以自由切换策略。

策略模式结构

策略模式需要一个策略接口,不同的策略实现不同的实现类,在具体业务环境中仅持有该策略接口,根据不同的场景使用不同的实现类即可。
在这里插入图片描述

面向接口编程,而不是面向实现。

优点

1、干掉繁琐的 if、switch 判断逻辑;

2、代码优雅、可复用、可读性好;

3、符合开闭原则(对修改关闭, 对扩展开放),扩展性好、便于维护;

缺点

1、策略如果很多的话,会造成策略类膨胀;

2、使用者必须清楚所有的策略类及其用途;

策略模式代码示例

  • 基础登录接口
//基础登录接口
public interface BaseLoginService {void login(BaseLoginContext context);
}
  • 策略上下文实现
@Data
public class BaseLoginContext {private String userName;private String password;private BaseLoginService baseLoginService;public BaseLoginContext(String userName, String password, BaseLoginService baseLoginService) {this.userName = userName;this.password = password;this.baseLoginService = baseLoginService;}public void login(){baseLoginService.login(this);}
}
  • 定义具体的策略实现类
  1. 实现类1
public class SimpleLoginServiceImpl implements BaseLoginService {@Overridepublic void login(BaseLoginContext context) {System.out.println("简单登录,当前登录的人:" + context.getUserName() + ",密码:" + context.getPassword());}
}
  1. 实现类2
public class HardLoginServiceImpl implements BaseLoginService {@Overridepublic void login(BaseLoginContext context) {System.out.println("复杂登录,当前登录的人:" + context.getUserName() + ",密码:" + context.getPassword());}
}
  • 调用
public static void main(String[] args) {BaseLoginService simple = new SimpleLoginServiceImpl();BaseLoginContext simpleContext = new BaseLoginContext("simple","simple",simple);simpleContext.login();BaseLoginService hard = new HardLoginServiceImpl();BaseLoginContext hardContext = new BaseLoginContext("hard","hard",hard);hardContext.login();}
  • 结果
    在这里插入图片描述

扩展策略

方式1

在策略的算法实现上添加自己需要的数据的方式

public class CommonLoginServiceImpl implements BaseLoginService {private String userId;public CommonLoginServiceImpl(String userId) {this.userId = userId;}public String getUserId() {return userId;}@Overridepublic void login(BaseLoginContext context) {System.out.println("复杂登录,当前登录的人:" + context.getUserName() + ",账号:" + getUserId() + ",密码:" + context.getPassword());}
}
  • 调用
public static void main(String[] args) {BaseLoginService simple = new SimpleLoginServiceImpl();BaseLoginContext simpleContext = new BaseLoginContext("simple","simple",simple);simpleContext.login();BaseLoginService hard = new HardLoginServiceImpl();BaseLoginContext hardContext = new BaseLoginContext("hard","hard",hard);hardContext.login();BaseLoginService common = new CommonLoginServiceImpl("001");BaseLoginContext commonContext = new BaseLoginContext("common","common",common);commonContext.login();}
  • 结果
    在这里插入图片描述

方式2

扩展上下文的方式

public class NewLoginContext extends BaseLoginContext{private String userId;public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public NewLoginContext(String userName, String password, String userId, BaseLoginService baseLoginService) {super(userName, password, baseLoginService);this.userId = userId;}
}
  • 实现类
public class CommonLoginServiceImpl implements BaseLoginService {private String userId;public CommonLoginServiceImpl(String userId) {this.userId = userId;}public String getUserId() {return userId;}@Overridepublic void login(BaseLoginContext context) {NewLoginContext newLoginContext = (NewLoginContext)context;
//        System.out.println("common1登录,当前登录的人:" + context.getUserName() + ",账号:" + getUserId() + ",密码:" + context.getPassword());System.out.println("common2登录,当前登录的人:" + context.getUserName() + ",账号:" + newLoginContext.getUserId() + ",密码:" + context.getPassword());}
}
  • 调用
public static void main(String[] args) {BaseLoginService simple = new SimpleLoginServiceImpl();BaseLoginContext simpleContext = new BaseLoginContext("simple","simple",simple);simpleContext.login();BaseLoginService hard = new HardLoginServiceImpl();BaseLoginContext hardContext = new BaseLoginContext("hard","hard",hard);hardContext.login();//        BaseLoginService common1 = new CommonLoginServiceImpl("001");
//        BaseLoginContext commonContext1 = new BaseLoginContext("common","common",common1);
//        commonContext1.login();BaseLoginService common2 = new CommonLoginServiceImpl("001");NewLoginContext commonContext2 = new NewLoginContext("common","common","002",common2);commonContext2.login();}
  • 结果
    在这里插入图片描述
http://www.hkea.cn/news/592243/

相关文章:

  • 获取网站访客qq 原理百度投诉中心人工电话
  • 企业网站制作查询百度电话怎么转人工
  • 杭州专业网站建设怎样创建网站
  • 网站建设报价表格式淘宝关键词优化技巧
  • 高端网站建设系统百度网盘登录入口官网
  • ps做网站顶部江苏网络推广公司
  • 源码做网站手机网站百度关键词排名
  • 网站关键词分隔网站链接提交
  • 福永营销型网站多少钱中国最新消息今天
  • 做网站4000-262-263网站排名优化软件有哪些
  • 网站双链接怎么做网络舆情监测平台
  • 企业网站建设制作百度网盘下载app
  • asp做一个简单网站网络营销就是seo正确吗
  • 移动wap站点公司网站设计图
  • 网站策划建设seo搜索排名影响因素主要有
  • 大型商业广场网站建设互联网推广方案怎么写
  • p2vr做的网站上传网络广告策划书范文
  • 2022年大连黄页优化搜索引擎营销
  • 宁波有几个区昭通网站seo
  • 建设企业网站方案网站优化软件哪个好
  • 郑州做网站要搜索引擎最新排名
  • wordpress建好站了打不开首页成都关键词优化排名
  • 京东网站开发需求如何做谷歌优化
  • 微信app开发诊断网站seo现状的方法
  • 做旅行网站网站seo优化多少钱
  • 上海专业网站建设咨询网络销售怎么样
  • 奶茶网页设计图片湖南seo网站多少钱
  • 家里电脑做网站服务器如何建立网址
  • 临西做网站哪里便宜seo专业培训课程
  • 高端网站设计报价表个人网上卖货的平台