1

Head First 设计模式 - 代理模式

 11 months ago
source link: https://tuncle.blog/Ch%2011%20the%20Proxy%20Pattern/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Head First 设计模式 - 代理模式

发表于 2023-05-21
字数总计:354|阅读时长:1 分钟 | 阅读量:1

代理模式(Proxy Pattern)为对象提供一个代理进而控制对其的访问。

例如我们需要加载一张图片,但加载图片是个访问网络或 IO 的操作,我们不希望这个这个操作阻塞 UI 线程,于是我们可以定义一个代理来进行多线程的加载,并在加载完成后显示图片。

public interface Icon
{
void PrintIconWidthAndHeight();
}

抽象接口实现

public class ImageIcon : Icon
{
private int width, height;
public ImageIcon()
{
Thread.Sleep(5000);//Pretend there is some hard work to load the image
width = 800;
height = 1000;
}
public void PrintIconWidthAndHeight()
{
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Width is " + width + ",height is " + height);
}
}
public class ImageProxyIcon : Icon
{
private ImageIcon icon = null;
private bool isLoading = false;

public ImageProxyIcon() { }

public void PrintIconWidthAndHeight()
{
if (icon != null)
icon.PrintIconWidthAndHeight();
else if (!isLoading)
{
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Is Loading Image...");
isLoading = true;
new Thread(() =>
{
icon = new ImageIcon();
icon.PrintIconWidthAndHeight();
}).Start();
}
}
}

测试代码及结果

ImageProxyIcon proxyIcon = new ImageProxyIcon();
proxyIcon.PrintIconWidthAndHeight();

运行结果:

代理模式运行结果

代理模式运行结果
代理模式与装饰模式很像,不同的是装饰模式的目的是在原先的类外扩展某些功能,而代理模式只是控制原先类中某些接口的访问。例如上例子中,`ImageProxyIcon`并没有为`ImageIcon`拓展什么功能,只是用了多线程来访问访问其中的函数。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK