Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Strategy pattern of Java Design pattern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "strategic pattern case analysis of Java design pattern". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "strategic pattern case analysis of Java design pattern" can help you solve the problem.

1. Basic introduction

1) in Strategy Pattern, define algorithm families (policy groups) and encapsulate them so that they can be replaced with each other. This pattern makes the change of the algorithm independent of the customers who use the algorithm.

2) this algorithm embodies several design principles: first, to separate the changed code from the immutable code; second, to program against the interface rather than specific classes (policy interfaces are defined); and third, to use more composition / aggregation. use less inheritance (customers use policies in a combined way)

For example, aiming at environmental energy saving, three energy saving methods are put forward: baseband board energy saving, SPC energy saving, product energy saving, different energy saving ways, and different specific treatment processes of environmental equipment.

two。 Traditional way

(1) Class diagram

(2) coding implementation

1) define an EsPolicy abstract class and define the esPolicyHandler method

Public abstract class EsPolicy {public void esPolicyHandler () {}}

2) BbEsPolicy inherits EsPolicy and overrides the esPolicyHandler method

Public class BbEsPolicy extends EsPolicy {@ Override public void esPolicyHandler () {System.out.println ("= Energy Saving Strategy for processing Baseband plates = =");}}

3) SpcEsPolicy inherits EsPolicy and overrides the esPolicyHandler method

Public class SpcEsPolicy extends EsPolicy {@ Override public void esPolicyHandler () {System.out.println ("= Spc Energy Saving Policy processing =");}}

4) ProdEsPolicy inherits EsPolicy and overrides the esPolicyHandler method

Public class ProdEsPolicy extends EsPolicy {@ Override public void esPolicyHandler () {System.out.println ("= product energy saving strategy processing = =");}}

5) Test

Public class EsPolicyTest {public static void main (String [] args) {EsPolicy bbEsPolicy = new BbEsPolicy (); bbEsPolicy.esPolicyHandler (); SpcEsPolicy spcEsPolicy = new SpcEsPolicy (); spcEsPolicy.esPolicyHandler (); ProdEsPolicy prodPolicy = new ProdEsPolicy (); prodPolicy.esPolicyHandler ();}}

Execution result

Energy saving strategy for dealing with baseband plate

= = Spc Energy Saving Policy processing = =

= product energy saving strategy processing =

Problem: the traditional method is to achieve different behaviors of different subclasses by inheriting the parent class and overwriting the parent class method. The local changes to the class, especially the local changes to the superclass, will affect other parts and have spillover effects.

3. Adopt a strategic model

(1) Class diagram

(2) coding implementation

1) define policy interfaces and abstract methods

Public interface EsPolicyHandler {void esPolicyHandler ();}

2) BbEsPolicyHandler implements policy interface and esPolicyHandler method

Public class BbEsPolicyHandler implements EsPolicyHandler {@ Override public void esPolicyHandler () {System.out.println ("= baseband board energy saving strategy processing = =");}}

3) SpcEsPolicyHandler implements policy interface and esPolicyHandler method

Public class SpcEsPolicyHandler implements EsPolicyHandler {@ Override public void esPolicyHandler () {System.out.println ("= Spc Energy Saving Policy processing =");}}

4) ProdEsPolicyHandler implements policy interface and esPolicyHandler method

Public class ProdEsPolicyHandler implements EsPolicyHandler {@ Override public void esPolicyHandler () {System.out.println ("= product energy saving strategy processing = =");}}

5) define the EsPolicy abstract class and inject EsPolicyHandler as its attribute

Public abstract class EsPolicy {EsPolicyHandler esPolicyHandler; public void setEsPolicyHandler (EsPolicyHandler esPolicyHandler) {this.esPolicyHandler = esPolicyHandler;} public void esPolicyHandler () {if (null! = esPolicyHandler) {esPolicyHandler.esPolicyHandler ();}}

6) BbEsPolicy inherits EsPolicy and instantiates esPolicyHandler interface properties in the constructor

Public class BbEsPolicy extends EsPolicy {public BbEsPolicy () {esPolicyHandler = new BbEsPolicyHandler ();}}

7) SpcEsPolicy inherits EsPolicy and instantiates esPolicyHandler interface properties in the constructor

Public class SpcEsPolicy extends EsPolicy {public SpcEsPolicy () {esPolicyHandler = new SpcEsPolicyHandler ();}}

8) ProdEsPolicy inherits EsPolicy and instantiates esPolicyHandler interface properties in the constructor

Public class ProdEsPolicy extends EsPolicy {public ProdEsPolicy () {esPolicyHandler = new ProdEsPolicyHandler ();}}

9) Test

Public class EsPolicyTest {public static void main (String [] args) {BbEsPolicy bbEsPolicy = new BbEsPolicy (); bbEsPolicy.esPolicyHandler (); SpcEsPolicy spcEsPolicy = new SpcEsPolicy (); spcEsPolicy.esPolicyHandler (); ProdEsPolicy prodEsPolicy = new ProdEsPolicy (); prodEsPolicy.esPolicyHandler ();}}

10) execution result

Energy saving strategy for dealing with baseband plate

= = Spc Energy Saving Policy processing = =

= product energy saving strategy processing =

4. Considerations and details of the policy model

1) the key of the strategy model is to analyze the changing part and the invariable part of the project.

2) the core idea of the strategy model is to use more composition / aggregation and less inheritance, and to use behavioral combination rather than behavioral inheritance to be more flexible.

3) it embodies the principle of "close to modification and open to extension". The client adds behavior without modifying the original code, as long as a policy (or behavior) is added, avoiding the use of multiple transfer statements (if … Else if... Else)

4) provides a way to replace the inheritance relationship: the policy mode encapsulates the algorithm in a separate Strategy class so that you can change it independently of its Context, making it easy to switch, easy to understand, and easy to extend

5) it should be noted that every time a policy is added, a class will be added, and too many policies will lead to a large number of classes.

This is the end of the content of "Strategic pattern example Analysis of Java Design pattern". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report