Agent mode and factory method mode

1. Agency model

According to the example of the big talk design pattern

Code without proxy:

//SchoolGirl,被追求者类
public class SchoolGirl {
    
    
    private String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }  
}
//追求者类
public class Pursuit {
    
    
    SchoolGirl schoolGirl;

    public Pursuit(SchoolGirl schoolGirl) {
    
    
        this.schoolGirl = schoolGirl;
    }
    public void GiveDolls() {
    
    
        System.out.println(schoolGirl.getName() + "送你洋娃娃");
    }
    public void GiveFlowers() {
    
    
        System.out.println(schoolGirl.getName() + "送你鲜花");
    }
    public void GiveChocolate() {
    
    
        System.out.println(schoolGirl.getName() + "送你巧克力");
    }  
}

//测试类写法
public class Test {
    
    
    public static void main(String[] args) {
    
    
        SchoolGirl schoolGirl = new SchoolGirl();
        schoolGirl.setName("大话设计模式");
        Pursuit pursuit = new Pursuit(schoolGirl);//此时被追求者根本不认识追求者
        pursuit.GiveDolls();
        pursuit.GiveChocolate();
        pursuit.GiveFlowers();
        System.out.println("测试一结束");
    }
}

Only proxy code

//定义送礼物的接口
public interface IGirlGift {
    
    
    void GiveDolls();
    void GiveFlowers();
    void GiveChocolate();
}
//被追求者类
public class SchoolGirl {
    
    
    private String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }   
}
//让代理和追求者都实现这个接口
public class Pursuit implements IGirlGift {
    
    
    SchoolGirl schoolGirl;

    public Pursuit(SchoolGirl schoolGirl) {
    
    
        this.schoolGirl = schoolGirl;
    }
    public void GiveDolls() {
    
    
        System.out.println(schoolGirl.getName() + "送你洋娃娃");
    }
    public void GiveFlowers() {
    
    
        System.out.println(schoolGirl.getName() + "送你鲜花");
    }
    public void GiveChocolate() {
    
    
        System.out.println(schoolGirl.getName() + "送你巧克力");
    }   
}
//代理者
public class Proxy implements IGirlGift {
    
    
    Pursuit pursuit;

    public Proxy(SchoolGirl schoolGirl) {
    
    
        pursuit = new Pursuit(schoolGirl);
    }
    public void GiveDolls() {
    
    
        pursuit.GiveDolls();
    }
    public void GiveFlowers() {
    
    
        pursuit.GiveFlowers();
    }
    public void GiveChocolate() {
    
    
        pursuit.GiveChocolate();
    }
}
//测试类
public class Test {
    
    
    public static void main(String[] args) {
    
    
        SchoolGirl schoolGirl = new SchoolGirl();
        schoolGirl.setName("大话设计模式");
        Proxy proxy = new Proxy(schoolGirl);
        proxy.GiveChocolate();
        proxy.GiveDolls();
        proxy.GiveFlowers();
        System.out.println("简单代理实现");
    }
}

2. Factory method pattern

  • Simple factory pattern
    • Advantages: The factory class contains the necessary logical judgments, and the related classes are dynamically instantiated according to the client's selection conditions. For the client, the dependence on specific products is removed
    • Disadvantages: The client needs to decide which factory to instantiate to implement the calculation class. The problem of selection judgment still exists. The factory method transfers the internal logic judgment of the simple factory to the client code for execution. When you need to add features, you originally need to change the factory class, but now you need to modify the client.

Steps for usage:

Step 1: Create an abstract factory class and define the public interface of the concrete factory;
Step 2: Create an abstract product class and define the public interface of the concrete product;
Step 3: Create a concrete product class (inherit the abstract product class) & define the specific product produced;
Step 4: Create a concrete factory class (inherit the abstract factory class) and define the method to create corresponding concrete product instances;
Step 5: The outside world creates instances of different ** concrete product classes by calling the methods of the concrete factory class

Guess you like

Origin blog.csdn.net/qq_37771811/article/details/103811005