added lab13 ex1

This commit is contained in:
Louis Heredero 2024-11-11 09:41:57 +01:00
parent 3743b47887
commit 7c614b0c5c
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package lab13_proxy.ex1;
public class Image {
protected final String path;
protected final String resolution;
protected boolean loaded = false;
public Image(String path, String resolution) {
this.path = path;
this.resolution = resolution;
}
public void load() {
if (!loaded) {
System.out.println("Image " + path + " is loaded in " + resolution + " resolution");
loaded = true;
}
}
public boolean isLoaded() {
return loaded;
}
public void showImage(User user) {
System.out.println("Image " + path + " is shown in " + resolution + " resolution for user " + user.getName());
}
}

View File

@ -0,0 +1,24 @@
package lab13_proxy.ex1;
public class ImageProxy extends Image {
private final Image lowResImage;
private final Image highResImage;
public ImageProxy(String path) {
super(path, "high");
lowResImage = new Image(path, "low");
highResImage = new Image(path, "high");
}
public void showImage(User user) {
System.out.println(user.getName() + " selects preview image " + path + " and wants to see its full resolution.");
if (RegistrationService.isRegistered(user)) {
highResImage.load();
highResImage.showImage(user);
} else {
System.out.println("User " + user.getName() + " is not registered. Showing preview image in low resolution");
lowResImage.load();
lowResImage.showImage(user);
}
}
}

View File

@ -0,0 +1,19 @@
package lab13_proxy.ex1;
public class Main {
public static void main(String[] args) {
User jean = new User("Jean");
User paul = new User("Paul");
User pierre = new User("Pierre");
RegistrationService.register(paul);
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");
Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");
Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");
highResolutionImage1.showImage(jean);
highResolutionImage2.showImage(paul);
highResolutionImage3.showImage(pierre);
}
}

View File

@ -0,0 +1,15 @@
package lab13_proxy.ex1;
import java.util.ArrayList;
public class RegistrationService {
private static final ArrayList<User> users = new ArrayList<>();
public static void register(User user) {
System.out.println(user.getName() + " is now registered");
users.add(user);
}
public static boolean isRegistered(User user) {
return users.contains(user);
}
}

View File

@ -0,0 +1,13 @@
package lab13_proxy.ex1;
public class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}