Press "Enter" to skip to content

Java设计模式—策略模式(strategy pattern)



abstract class PersonComing {
    abstract void process(Person person);
class FriendComing extends PersonComing {
    void process(Person friend) {
        System.out.println("say hello to " + friend);
class ThiefComing extends PersonComing {
    void process(Person thief) {
        System.out.println("call 110 to arrrest " + thief);
class Person {
    String name;
    Person(String str) {
        name = str;
    public String toString() {
        return name;
public class Test {
    static void deal(PersonComing meet, Person person) {
    public static void main(String[] args) {
        Person friend = new Person("Tom");
        Person thief = new Person("Tim");
        deal(new FriendComing(),friend); //output: say hello to Tom
        deal(new ThiefComing(),thief); //output: call 110 to arrest Tim

Be First to Comment


电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see