设计模式:静态代理

2020-09-28  本文已影响0人  Codes作业本

静态代理

第一步:首先实现钢笔的接口

interface PenInterface {
    void write();
    void draw();
}

第二步:学生的手实现钢笔接口

public class StudentHand implements PenInterface{

    @Override
    public void write() {
        System.out.println("write() 写字方法调用");
        
    }

    @Override
    public void draw() {
        System.out.println("draw() 画画方法调用");
    }

}

第三步:学生实现钢笔接口的功能

public class Students implements PenInterface {

    PenInterface mStudentHand;

    public Students(PenInterface pen) {
        mStudentHand = pen;
    }

    @Override
    public void write() {
        mStudentHand.write();
    }

    @Override
    public void draw() {
        mStudentHand.draw();
    }

}

Main方法中调用,通过学生,去实现钢笔的功能

public static void main(String[] args) {
    PenInterface pen = new StudentHand();
    Students student = new Students(pen);
    student.draw();
    student.write();
}
上一篇 下一篇

猜你喜欢

热点阅读