"Dahua design pattern" Java implementation of the proxy model

Source: Internet
Author: User

UML diagram, in order to show the core of the Proxy mode class relationship, here the details of the section, such as Pursui and schoolgirl relationship is not drawn, the core of the proxy mode is the proxy class and the proxy class is implemented by the same interface, you can control access to a class of methods, before the call method to do the pretreatment, To do a post processing after calling a method

Code structure

public class Client {public
    static void Main (string[] args) {
        schoolgirl Jiaojiao = new Schoolgirl ("Lee charming Jiao");

        Proxy Dali = new proxy (Jiaojiao);

        Dali.givedolls ();
        Dali.giveflowers ();
    }
Public interface Igivegift {
    void Givedolls ();
    void Giveflowers ();
}
public class Proxy implements Igivegift {
    private Pursuit GG;

    Public Proxy (schoolgirl mm) {
        GG = new Pursuit (mm);
    }

    @Override public
    void Givedolls () {
        if (GG!= null) {
            gg.givedolls ()

    }} @Override public
    void Giveflowers () {
        if (GG!= null) {
            gg.giveflowers ()

}}}
public class Pursuit implements Igivegift {
    private schoolgirl schoolgirl;

    Public Pursuit (schoolgirl schoolgirl) {
        super ();
        This.schoolgirl = schoolgirl;
    }

    @Override public
    void Givedolls () {
        System.out.println (schoolgirl.getname () + ", send you A Doll");
    }

    @Override public
    void Giveflowers () {
        System.out.println (schoolgirl.getname () + ", send you Flowers");
    }

public class Schoolgirl {
    private String name;

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    Public schoolgirl (String name) {
        super ();
        this.name = name;
    }

Run results

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.