This is the 26th day of my participation in the August More Text Challenge


Design Patterns series: Design Patterns column


Static agent
  • In fact, something as abstract as design patternstechnologyIt’s really hard to write in code, because the real business is always a hundred, thousand times more complex than our example!
  • But design patterns can come into play when you feel like you’re doing the same thing all the time and want to optimize your code.
  • Overall, learning design patterns is a cumulative process, not one that can be learned overnight.
  • Here will only provide a way of thinking to everyone, mainly rely on their own in the actual development can think more, try to bring this mode of thinking into the code! That’s how we get it together!
1.1. Case 1: Renting a house

① In life, we will inevitably need to rent a house because of work, life and other reasons. This is all too familiar to you!

  • Landlord and intermediary have a common character, rent a house to go out namely!
public interface Rent {
    / / rent
    void rent(a);
}
Copy the code

(2) the landlord:

/** * Need to implement rent interface, because there is a house to rent! * /
public class Landlord implements Rent {

    @Override
    public void rent(a) {
        System.out.println("Landlord: I have a house to rent!"); }}Copy the code

(3) mediation:

/** * This is an agent role, which is equivalent to the agent * it also implements the rental interface, because the agent rents the house for the landlord! * /
public class ProxyUser implements Rent {
    // Help the landlord rent the house
    private Landlord landlord;

    public ProxyUser(a) {}// You can help different landlords rent out their houses according to the parametric structure
    public ProxyUser(Landlord landlord) {
        this.landlord = landlord;
    }

    @Override
    public void rent(a) {
        // The landlord's house is for rent
        landlord.rent();

    }
    / / see
    public void seeHouse(a){
        System.out.println("Agent: show the house!");
    }
    // Sign the contract for the landlord
    public void sinContract(a){
        System.out.println("Agent: Sign the contract!"); }}Copy the code

(4) the customer:

public class Customer {
    private String name;

    public String getName(a) {
        System.out.println(name+"I need to rent a house.");
        return name;
    }

    public void setName(String name) {
        this.name = name; }}Copy the code

⑤ Test class:

public class Test {
    public static void main(String[] args) {
        Customer customer = new Customer();
        customer.setName("Big big big big.");
        customer.getName();
        Landlord landlord = new Landlord();
        ProxyUser proxyUser = newProxyUser(landlord); proxyUser.rent(); proxyUser.seeHouse(); proxyUser.sinContract(); }}Copy the code

⑥ Execution result:His conclusion:

  • Originally rent a house is to need the client to find the landlord.
  • Now the landlord does not want to worry about it, so he directly gives the house to the intermediary, and the intermediary can pay the landlord rent every month.
  • Agents rent out houses to clients.
  • Advantage is, see 10 suites originally need to look for a landlord, need to look for an intermediary only now can!
  • An intermediary is an agent!
1.2. Case 2: Horizontal expansion

① Service abstract interface:

public interface UserService {
    void add(a);
    void delete(a);
    void update(a);
    void selete(a);
}
Copy the code

② Business implementation class:

public class UserServiceImpl implements UserService {
    @Override
    public void add(a) {
        System.out.println("Add users");
    }

    @Override
    public void delete(a) {
        System.out.println("Delete user");
    }

    @Override
    public void update(a) {
        System.out.println("Update user");
    }

    @Override
    public void selete(a) {
        System.out.println("Query user"); }}Copy the code

③ Proxy implementation class:

public class ProxyUserService implements UserService{

    private UserServiceImpl userService;

    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }

    / / log
    public void log(a){
        System.out.println("Log print!");
    }

    @Override
    public void add(a) {
        log();
        userService.add();
    }

    @Override
    public void delete(a) {
        log();
        userService.delete();
    }

    @Override
    public void update(a) {
        log();
        userService.update();
    }

    @Override
    public void selete(a) { log(); userService.selete(); }}Copy the code

④ Test class:

public class TestProxy {
    public static void main(String[] args) {
        // Real business
        UserServiceImpl userService = new UserServiceImpl();
        / / the proxy class
        ProxyUserService proxy = new ProxyUserService();
        // Implement the proxy to add logs
        proxy.setUserService(userService);

        proxy.add();
        System.out.println("= = = = = = = = =");
        proxy.delete();
        System.out.println("= = = = = = = = =");
        proxy.update();
        System.out.println("= = = = = = = = ="); proxy.selete(); }}Copy the code

⑤ Execution results:

6. Conclusion:

  • Well, emulating this code might seem more cumbersome, but it’s essentially just another layer of encapsulation!
  • Why is that? What if each business has a very large amount of code? The risk of transformation is too great, but use proxy mode to slice transformation! That’s the advantage!
  • Horizontal development!!

The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah