A blog to understand the design pattern --- delegation pattern

A blog to understand the design pattern - delegation pattern

The delegation mode may not sound familiar to you, but it is very useful in code development. Let me introduce it from several aspects

what: what is it?
Delegation mode : As the name implies, we entrust other objects or instances to help us complete tasks. Since there are delegations, there are two roles: entrusting classes and entrusting classes . The relationship between these two roles in society is equal, similar to our reality In the relationship between the product manager and ordinary employees, the product manager entrusts employees to help him complete the task, and the product manager only cares about the result : are you doing well? As for how to do it, he doesn't care, the typical work is that you (ordinary employee) success is mine (project manager) , and the delegation model only cares about the result.

why: why this mode appears: in order to hide some specific implementation logic

Difference:
How is it different from other modes?
Factory mode: In order to ensure the diversity of results, there is only one method for users. It is a delegation mode with a fixed mode . The factory mode has some fixed templates of its own, as follows :

insert image description here
If you don’t understand, you can refer to my other blog: A blog to understand the design pattern-factory pattern

**Proxy mode: **Although the proxy mode is also designed for the relationship between two objects, and the proxy object must hold a reference to the proxied object, the proxy mode is more concerned with the execution process, while the delegation mode only cares about the result , and the status of the two objects in the delegation mode is equal, and both must implement the same interface!

Here is an example to give you a better understanding of the delegation model:
first implement an interface Executor;

public interface Executor {
    
    
	void executing();
}

Implement two delegate classes:

public class StaffA implements Executor {
    
    
	@Override
	public void executing() {
    
    
		System.out.println("staff A executor task!");
	}
}
public class StaffB implements Executor {
    
    
	@Override
	public void executing() {
    
    
		System.out.println("staffB execute task");
	}
}

Add another delegate class:

public class Boss implements Executor {
    
    
	private  Executor executor;

	//这个构造方法是关键
	Boss(Executor executor){
    
    
		this.executor = executor;
	}
	@Override
	public void executing() {
    
    
		System.out.println("boss start dispatcher job!");
		//看上去好像是我们的项目经理在干活
		//但实际干活的人是普通员工
		//这就是典型,干活是我的,功劳是你的
		executor.executing();
	}
}

Finally add a test class:

public class TestExecutor {
    
    
	public static void main(String[] args) {
    
    
		Boss boss = new Boss(new StaffA());
		boss.executing();

		Boss boss1 = new Boss(new StaffB());
		boss1.executing();
	}
}

From the above test class, we can see that although the Boss appears to be working on the surface, it is actually the corresponding employee working (the employee who needs to work is passed in through the constructor)

mission completed!
Welcome to leave a message and like it! !

Guess you like

Origin blog.csdn.net/weixin_37766296/article/details/106301389