Implementing policy Patterns in Java

Source: Internet
Author: User

Policy patterns in Java

I. Overview

Second, through the base class to implement the policy model

Third, through the interface to implement the policy model

Iv. Summary

I. Overview

The so-called strategy pattern is that when a behavior has multiple implementations and there may be more implementations over time, these specific implementations can inherit from a base class or implement a common interface, so that the implementation is not considered when invoking the policy (implementation). The policy pattern is a good way to realize the separation between behavior and behavior, making the invocation of the behavior more stable (no need to change the call as the implementation changes). For example, to implement a sorting algorithm, the existing sorting algorithm has bubbling, selection, fast sorting, bucket sorting, and so on, this can be defined a sort interface, and then let bubble, select and so on to implement this interface, so you can call the interface to implement the specific algorithm calls (with the implementation of the common interface class to instantiate the interface).

Second, through the base class to implement the policy model

Suppose there are three kinds of bee,bird,butterfly animals, they will have a flight behavior before the attack, now we use the Strategy mode to achieve the attack of three kinds of flying animals, so that the client calls do not have to consider the specific object and implementation. (Note: For demonstration convenience, the abstract policy class and the specific policy class access permissions are set to default, in practice, abstract policy classes and specific policy classes are generally defined as public to be called on the client)

  

Abstract policy Classes
Abstract class attack{
public void Fly () {
System.out.println ("Attack Fly ()");
}
public abstract void Attack ();
}

Specific policy class-bee
Class Bee extends attack{
public void Attack () {
System.out.println ("Bee attack ()");
}
}

Specific policy class-bird
Class Bird extends attack{
public void Attack () {
System.out.println ("Bird attack ()");
}
}

Specific policy class-butterfly
Class Buterfly extends attack{
public void Attack () {
System.out.println ("Butterfly attack ()");
}
}

public class attacktest{
Private Attack A;

public static void Main (string[] args) {
Instantiate an abstract base class based on specific requirements
A =new Bird ();

Implement an attack
A.attack ();
}
}
Three, through the interface to implement the policy mode

Suppose we need to implement a sort function now, the existing sorting algorithm has selection, bubbling, bucket sorting, etc. we use the sort interface to implement the strategy mode;

Abstract Sort Interface
public interface isort{
void sort (int[] a);
}

Specific sort algorithm-bubble sort
public class Maopaosort implements isort{
void sort (int[] a) {
...//specific implementation ignored
}
}

Specific sorting algorithm-bucket sequencing
public class Tongsort implements isort{
void sort (int[] a) {
...//specific implementation ignored
}
}

Client Invoke Sort algorithm
public class sorttest{
Instantiate an interface as needed
Private Sort s=new tongsort ();

static void Sorttest (int[] a) {
S.sort (a);
}

public static void Main (string[] args) {
Int[] a={1,2,5,4,2};
Sorttest (a);
}
}
Iv. Summary

In general, the main function of the policy model is to separate the invocation of the method from the specific implementation, ensuring that the client calls do not need to change even if the implementation changes. As to whether to choose either abstract class or interface to implement the policy mode is the case, in general, if you determine a few specific implementations have a common implementation of the part, such as printing function, whether it is printed on the console printing or printing on the printer will be printed string, then the printing policy can use abstract class to implement So that the part of the code that generates the string is implemented directly in the abstract class to avoid repeating the code in the implementation, otherwise the interface is used to implement the policy pattern.

  

Implementing policy patterns in Java

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.