Strategy in Design Mode)

Source: Internet
Author: User

Strategy refers to the object behavior-type mode in the design mode. It mainly defines a series of performance algorithms and converts these algorithms into a single class.

Stratrgy should be more extensive than others. For example, if the company's business changes, there may be two practical methods, one is bar. These two algorithms can be implemented using strategy.

This example uses a string as an example. There is a file in which we need to replace the corresponding changes and then export them. There may be multiple ways to replace the change data, which determines the requirements for using the change data. Therefore, we need to modify the change data substitution scheme.

First, we establish an abstract class to define some common variables and methods:

Public abstract class reptemprule {

Protected string oldstring = "";
Public void setoldstring (string oldstring ){
This. oldstring = oldstring;
}

Protected string newstring = "";
Public String getnewstring (){
Return newstring;
}

Public abstract void Replace () throws exception;

}

In the reptemprule, there is an abstract method that needs to be fully validated. This replace statement is actually a substitute for the specific method.
We now have two character substitution schemes,
1. Replace AAA with BBB in the text;
2. Replace AAA with CCC in the text;

The corresponding class is reptempruleone reptempruletwo.

Public class reptempruleone extends reptemprule {

Public void Replace () throws exception {

// Replacefirst is a new feature of jdk1.4.
Newstring = oldstring. replacefirst ("AAA", "BBBB ")
System. Out. println ("this is replace one ");
  
}

}

 

Public class reptempruletwo extends reptemprule {

Public void Replace () throws exception {

Newstring = oldstring. replacefirst ("AAA", "CCC ")
System. Out. println ("this is replace two ");
  
}

}

So far, we have completed the design and programming of different types of products. The call is as follows:

Public class test {

......

Public void testreplace (){

// Use the first scheme for replacement.
Reptemprule rule = new reptempruleone ();
Rule. setoldstring (record );
Rule. Replace ();

}

.....

}

In fact, the core part of the entire strategy is to abstract the usage of different classes. When the strategy mode is used, the changes need to be made when the strategy mode is used, and the modification volume is very small and fast.

There are some similarities between strategy and factory, and strategy is easy to understand.

Strategy merge:

1. save files in different formats;

2. Use different algorithms to compile the sequence file;

3. Use different algorithms to intercept blurred images;

4. Export the metadata of the same item in different formats, such as the curve or frame bar.

 

First, we establish an abstract class to define some common variables and methods:

Public abstract class reptemprule {

Protected string oldstring = "";
Public void setoldstring (string oldstring ){
This. oldstring = oldstring;
}

Protected string newstring = "";
Public String getnewstring (){
Return newstring;
}

Public abstract void Replace () throws exception;

}

In the reptemprule, there is an abstract method that needs to be fully validated. This replace statement is actually a substitute for the specific method.
We now have two character substitution schemes,
1. Replace AAA with BBB in the text;
2. Replace AAA with CCC in the text;

The corresponding class is reptempruleone reptempruletwo.

Public class reptempruleone extends reptemprule {

Public void Replace () throws exception {

// Replacefirst is a new feature of jdk1.4.
Newstring = oldstring. replacefirst ("AAA", "BBBB ")
System. Out. println ("this is replace one ");
  
}

}

 

Public class reptempruletwo extends reptemprule {

Public void Replace () throws exception {

Newstring = oldstring. replacefirst ("AAA", "CCC ")
System. Out. println ("this is replace two ");
  
}

}

So far, we have completed the design and programming of different types of products. The call is as follows:

Public class test {

......

Public void testreplace (){

// Use the first scheme for replacement.
Reptemprule rule = new reptempruleone ();
Rule. setoldstring (record );
Rule. Replace ();

}

.....

}

In fact, the core part of the entire strategy is to abstract the usage of different classes. When the strategy mode is used, the changes need to be made when the strategy mode is used, and the modification volume is very small and fast.

There are some similarities between strategy and factory, and strategy is easy to understand.

Strategy merge:

1. save files in different formats;

2. Use different algorithms to compile the sequence file;

3. Use different algorithms to intercept blurred images;

4. Export the metadata of the same item in different formats, such as the curve or frame bar.

Related Article

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.