设计模式-行为型-访问者模式

1、定义

访问者模式:在不改变数据结构的前提下,增加作用于一组元素对象新功能

写法

/**
 * 受访者
 */
public interface IAccept {
    void accept(IVisitor visit);
    void showInfo();
}

/**
 * 访问者
 */
public interface IVisitor {

    void visit(ACCEPT accept);

    Kind kind(); //访问者类型

}

/**
 * 访问者类型
 */
public enum Kind {
    CEO,
    CTO,
    HR
}

/**
 * 访问者模板
 */
public abstract class Visitor implements IVisitor{
    @Override
    public void visit(IAccept accept) {
        accept.showInfo();
    }
}

public class CEOVisitor extends Visitor{

    @Override
    public Kind kind() {
        return Kind.CEO;
    }

}

public class CTOVisitor extends Visitor  {

    @Override
    public Kind kind() {
        return Kind.CTO;
    }

}

/**
 * 访问者
 */
public class HRVisitor extends Visitor{

    @Override
    public Kind kind() {
        return Kind.HR;
    }
}

/**
 * 受访者--工程师
 */
public class Engineer implements IAccept {

    private String name;
    private String kpi;
    private String line;
    private IVisitor visitor;

    public Engineer(String name, String kpi, String line) {
        this.name = name;
        this.kpi = kpi;
        this.line = line;
    }

    @Override
    public void accept(IVisitor visit) {
        visitor = visit;
        visit.visit( this );
    }

    @Override
    public void showInfo() {
        if (visitor.kind() == Kind.CEO){
            System.out.println( "Engineer{" +
                    "name='" + name + '\'' +
                    ", kpi='" + kpi + '\'' +
                    '}' );
        }else if(visitor.kind() == Kind.CTO){
            System.out.println( "Engineer{" +
                    "name='" + name + '\'' +
                    ", line='" + line + '\'' +
                    '}' );
        }else if(visitor.kind() == Kind.HR){
            System.out.println( "Engineer{" +
                    "name='" + name + '\'' +
                    '}' );
        }
    }

}

/**
 * 受访者--经理
 *
 */
public class Manage implements IAccept{

    private String name;
    private String kpi;
    private String product;
    private IVisitor visitor;

    public Manage(String name, String kpi, String product) {
        this.name = name;
        this.kpi = kpi;
        this.product = product;
    }

    @Override
    public void accept(IVisitor visit) {
        visitor = visit;
        visit.visit( this );
    }

    @Override
    public void showInfo() {
        if (visitor.kind() == Kind.CEO){
            System.out.println( "Manage{" +
                    "name='" + name + '\'' +
                    ", kpi='" + kpi + '\'' +
                    ", product='" + product + '\'' +
                    '}' );
        }else if(visitor.kind() == Kind.CTO){
            System.out.println( "Manage{" +
                    "name='" + name + '\'' +
                    ", product='" + product + '\'' +
                    '}' );
        }else if(visitor.kind() == Kind.HR){
            System.out.println( "Manage{" +
                    "name='" + name + '\'' +
                    '}' );
        }

    }

}

public class Company {

    private List acceptList = new ArrayList<>(  );

    public Company(){
        acceptList.add( new Engineer( "小明","14","1000" ) );
        acceptList.add( new Engineer( "小张","10","12300" ) );
        acceptList.add( new Engineer( "小李","19","100450" ) );
        acceptList.add( new Engineer( "小孙","16","101100" ) );

        acceptList.add( new Manage( "张总","20","花钱项目" ) );
        acceptList.add( new Manage( "马云","10","五福项目" ) );
        acceptList.add( new Manage( "王总","20","卖大米项目" ) );
        acceptList.add( new Manage( "陈总","20","到天亮项目" ) );
    }

    public void showReport(IVisitor visitor){
        for (IAccept  accept :acceptList){
            accept.accept( visitor );
        }
    }

}

/**
 * 访问者模式
 *
 */
public  class TestMain {

    public static void main(String[] args){

        //公司
        Company company = new Company();

        //访问者查看对应报告
        company.showReport( new CEOVisitor() );
        company.showReport( new HRVisitor() );
        company.showReport( new CTOVisitor() );
    }

}

代码示例

https://github.com/yinlingchaoliu/23-design-pattern

你可能感兴趣的:(设计模式-行为型-访问者模式)