In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to implement the strategy pattern in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Here we design a scene about football, which is temporarily divided into two actions: passing and shooting.
At first you might think that designing an abstract class (Attact), passing and shooting are defined respectively, and the subclasses will have something of their own personality. Such as player number, coach name and so on.
Later you find that there are many kinds of passing and shooting, passing can be divided into short pass and long pass, shooting can be divided into Bhatti-style shooting and Inzaghi-style grabbing points. In this way, they can not all be written in this abstract class (Attact). For example, some players are engineers (like AC Milan's Gennaro Gattuso) who constantly steals and passes the ball and rarely takes part in shooting. In this way, defining several subclasses to Attact will not meet the requirements.
We can abstract actions such as passing and shooting. When combined into this abstract class, you only need to call the concrete method in it.
Define it like this: (where Passable and Shootable are behavior interfaces)
Package strategy; / * @ author edison * @ date 2009-9-24 * / public abstract class Attact {Passable pass; Shootable shoot; public abstract void display (); public void ownPass () {pass.action ();} public void ownShoot () {shoot.action ();} public void setPass (Passable pass) {this.pass = pass;} public void setShoot (Shootable shoot) {this.shoot = shoot }}
Here we use the strategy model, the passing and shooting actions are defined as standards, encapsulated, so that they can be combined and replaced with each other, so that the specific operation and implementation can be effectively separated.
The above paragraph can also say the following:
The policy pattern defines the algorithm families, which are encapsulated separately, so that they can be replaced with each other, and this pattern makes the changes of the algorithm independent of the customers who use the algorithm.
Get several design principles:
1. Find possible changes in the application, separate them from the beginning, and don't mix them with code that doesn't need to change.
two。 Program against the interface, not against the implementation.
3. Use more combinations and less inheritance.
Class diagram:
The above is a simple example of a strategic model.
Thank you for reading! This is the end of the article on "how to implement the strategic pattern in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.