Proxy Design Pattern

Shimaa Yasser
Mar 7, 2023

In proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern. In proxy pattern, we create object having original object to interface its functionality to outer world.

abstract class VideoDownloader{
void getVideo(String videoName);
}

class RealVideoDownloader implements VideoDownloader{
@override
void getVideo(String videoName){
print("Connecting to http://www.$videoName");
print("Downloading $videoName Video");
print("Retriving $videoName Video");
print("___________________________________");
}
}

class ProxyVideoDownloader implements VideoDownloader{
final VideoDownloader realVideoDownloader = RealVideoDownloader();
List<String> downloadedVideos = [];

@override
void getVideo(String videoName){
if(downloadedVideos.contains(videoName)){
int copyNum = checkCopyNum(videoName);
videoName = "$videoName${generateNewVideoName(copyNum)}";
}
downloadedVideos.add(videoName);
realVideoDownloader.getVideo(videoName);
}

int checkCopyNum(String videoName){
int copyNum = 1;
int startingIndex = downloadedVideos.indexOf(videoName)+1;
for(int i = startingIndex;i<=(downloadedVideos.length-1);i++){
if(downloadedVideos[i].compareTo("${videoName}_copy($copyNum)")==0){
copyNum ++;
}
}
return copyNum;
}

String generateNewVideoName(int copyNum){
return "_copy($copyNum)";
}
}

void main() {
ProxyVideoDownloader p = ProxyVideoDownloader();
p.getVideo("sh");
p.getVideo("oosh");
p.getVideo("sh");
p.getVideo("sh");
p.getVideo("sh");
p.getVideo("sh");
p.getVideo("shiio");
p.getVideo("sher");
}

--

--