Strategy in design pattern - Resume articles
The cousin immediately graduated from college, looking for a job to write resumes, so he will ask me about this issue. I told him that it is best to write two types of resumes. One is written in Chinese. One is written in English. If it is a state-owned enterprise, it will vote for Chinese resume. If it is a foreign company, it will vote for English resume. (EnglishResume), hey, the reason is not necessary to say more here.
Let's take a look at how this process is implemented?
1. Let's define the resume interface class:
Public interface resume {
Public void writetext ();
}
2, define the specific implementation of the resume interface:
A: Chinese resume (chineseresume)
Public class chineseresume implements resume {
Public void WriteText () {
System.out.println ("written in Chinese!");
}
}
B: English resume (EnglishResume)
Public class englishresume implements resume {
Public void WriteText () {
System.out.println ("Resume written in English!");
}
}
3. Define the delivery strategy (Strategy):
Public class statulegy {
Private resume resume;
Public Strategy (Resume ResMe) {// Strategy using your resume
THIS.RESUME = Resume;
}
Public void postResume () {// delivery resume
System.out.println ("delivery");
Resume.writetext ();
}
}
4, write test classes:
Public class test {
Public static void main (string args []) {
// If it is a state-owned enterprise
Resume BrotherResume = new ChineseResume (); // Click the resume written by Chinese
Strategy Strategy = New Strategy (BrotherResume); // Use resume written by Chinese
Strategy.postResume (); // deliver the resume to the state-owned enterprises
// If it is a private company
BrotherResume = new EnglishResume (); // Click on the resume written by English
Strategy = New Strategy (BrotherResume); // Use the resume written in English
Strategy.postResume (); // Deliver the resume to private companies
}
}
5. Description:
A: Strategy mode is mainly to define a series of algorithms to encapsulate these algorithms into separate classes.
B: In this example, Chinese resume and English resume are equivalent to two algorithms, and we define it into two separate classes.