- 1、本文档共25页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 8、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
策略模式是一个比较容易理解和使用的设计模式,策略模式是对算法的封装,它把算法的责任和算法本身分割开,委派给不同的对象管理。策略模式通常把一个系列的算法封装到一系列的策略类里面,作为一个抽象策略类的子类。用一句话来说,就是“准备一组算法,并将每一个算法封装起来,使得它们可以互换”。 在策略模式中,应当由客户端自己决定在什么情况下使用什么具体策略角色。 策略模式仅仅封装算法,提供新算法插入到已有系统中,以及老算法从系统中“退休”的方便,策略模式并不决定在何时使用何种算法,算法的选择由客户端来决定。这在一定程度上提高了系统的灵活性,但是客户端需要理解所有具体策略类之间的区别,以便选择合适的算法,这也是策略模式的缺点之一,在一定程度上增加了客户端的使用难度。 * Strategy模式的角色:Strategy????策略(算法)抽象。ConcreteStrategy????各 种策略(算法)的具体实现。Context????策略的外部封装类,或者说策略的容器类。根据不同 策略执行不同的行为。策略由外部环境决定。 * A few weeks ago, we were looking at the Strategy Design Pattern, which gets far less attention than it deserves. Gerhard Radatz from Alcatel Austria, asked whether the hashCode() function is an example of the Strategy. I do not think it is, but the question triggered an idea. I have written many hashCode() and equals() functions and they usually follow the same pattern. In IntelliJ IDEA, it even autogenerates them for you. But this is dumb copy + paste code, even if it is generated, so why could we not replace that with a Strategy instead, and how would it perform? Here is an example of IntelliJ IDEA generated equals() and hashCode() functions: public class PersonNormal { private final String name; private final String address; private final int age; public PersonNormal(String name, String address, int age) { = name; this.address = address; this.age = age; } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof PersonNormal)) return false; final PersonNormal personNormal = (PersonNormal) o; if (age != personNormal.age) return false; if (address != null ? !address.equals(personNormal.address) : personNormal.address != null) return false; if (name != null ? !name.equals(personN) : personN != null) return false; return true; } public int hashCode() { int result; result = (name != null ? name.hashCode() : 0); result = 29 * result + (address != null ? address.hashCode()
文档评论(0)