Design pattern--23, strategy pattern analysis and its application in the set frame

Source: Internet
Author: User

Strategy Mode (Strategy pattern)

embodies the principles of two very basic object-oriented design: The concept of encapsulation change and the use of interfaces in programming, rather than the implementation of interfaces.

1, the definition of the policy model:

Defines a set of algorithms that encapsulate each algorithm and allow them to be interchangeable. The policy pattern enables these algorithms to change in a way that does not affect each other when the client invokes them.

2, the significance of the strategic model:

The policy model enables developers to develop software that consists of many replaceable parts, with weak connections between the parts, and a weak connection that makes the software more scalable, easier to maintain, and, more importantly, greatly improves the reusability of the software.

3, the composition of the strategic model

-Abstract policy role: policy class, usually implemented by an interface or abstract class. Corresponds to the Comparator interface

-Specific policy roles: wrapping related algorithms and behaviors. Corresponds to the implementation of comparator, as we write our own Mycomparator class

-Environment role: holds a reference to a policy class that is eventually invoked by the client. Corresponds to TreeSet or treemap, which hold a comparer when they are constructed. That is, holding a reference to a policy class.

4, the implementation of the Strategy model

– The intent of the policy pattern is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms so that they can be replaced by each other.
– The policy pattern allows the algorithm to change without affecting the client. Using a policy pattern separates behavior from the environment.
– Environment classes are responsible for maintaining and querying behavior classes, and various algorithms are provided in specific strategies. Because the algorithm and the environment are independent, the modification of the algorithm will not affect the environment and the client

5, the policy mode of writing steps:

-1 defines a public interface for the policy object.

-2 Write a policy class that implements the public interface above

-3 Save a reference to a policy object in a class that uses a policy object .

4) in a class that uses a policy object, implement the Set and Get Methods (injection) of the policy object or use the constructor method to complete the assignment.

6, the concrete realization: To do a calculation of the strategy model implementation, first define a public interface, define the method of calculation, and then write the implementation of the interface of the policy class (calculated by adding, subtract, multiply), and finally define the environment class, which holds the policy object reference, the last client calls.

-1 defines policy public interface

Public interface Strategy
{public
	int calculate (int a,int b);
}


-2 The specific strategy class, there are three specific policy classes, addstrategy,substrategy,multiplystrategy, respectively, to achieve the addition and subtraction of the calculation strategy.

Public class Addstrategy implements strategy
{public
	int calculate (int a, int b)
	{return		
		a + b;
	}

}

Public class Substrategy implements strategy
{public
	int calculate (int a, int b)
	{return
		a-b;
  
   
{Public

class Multiplystrategy implements strategy
{public
	int calculate (int a, int b)
	{ C22/>return A * b;
	}
}
  


-3 Define Environment class

public class Enveronment
{
	private strategy strategy;
	
	Public enveronment (Strategy strategy)
	{
		this.strategy = strategy;
	}

	Public Strategy Getstrategy ()
	{return
		strategy;
	}

	public void Setstrategy (strategy strategy)
	{
		this.strategy = strategy;
	}
	
	public int calculate (int a,int b)
	{return
		strategy.calculate (A, b);
	}
}


-4) The client calls

public class Client
{public
	static void Main (string[] args)
	{
		Addstrategy addstrategy = new Addstrategy ();
		
		Enveronment enveronment = new Enveronment (addstrategy);
		
		System.out.println (Enveronment.calculate (3, 4));
		
		Substrategy substrategy = new Substrategy ();
		
		Enveronment.setstrategy (substrategy);
		
		System.out.println (Enveronment.calculate (3,4));
		
		Multiplystrategy multiplystrategy = new Multiplystrategy ();
		
		Enveronment.setstrategy (multiplystrategy);
		
		System.out.println (Enveronment.calculate (3, 4));
	}

Provides a get and set method for applying a variable to a policy in the environment role class so that the client can change the policy used by the method.

7, the shortcomings of the strategy model
–1) The client must know all the policy classes and decide which policy class to use.
–2) creates a number of policy classes.

The solution is to adopt a factory approach.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.