added lab13 ex1
This commit is contained in:
parent
3743b47887
commit
7c614b0c5c
27
src/lab13_proxy/ex1/Image.java
Normal file
27
src/lab13_proxy/ex1/Image.java
Normal 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());
|
||||
}
|
||||
}
|
24
src/lab13_proxy/ex1/ImageProxy.java
Normal file
24
src/lab13_proxy/ex1/ImageProxy.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
19
src/lab13_proxy/ex1/Main.java
Normal file
19
src/lab13_proxy/ex1/Main.java
Normal 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);
|
||||
}
|
||||
}
|
15
src/lab13_proxy/ex1/RegistrationService.java
Normal file
15
src/lab13_proxy/ex1/RegistrationService.java
Normal 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);
|
||||
}
|
||||
}
|
13
src/lab13_proxy/ex1/User.java
Normal file
13
src/lab13_proxy/ex1/User.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user