System Components & Patterns
Used to construct complex metadata objects step by step.
Copy public class FileMetadata {
private final String fileId;
private final String fileName;
private final String fileType;
private final long fileSize;
private final String ownerId;
private final String storagePath;
private FileMetadata(FileMetadataBuilder builder) {
this.fileId = builder.fileId;
this.fileName = builder.fileName;
this.fileType = builder.fileType;
this.fileSize = builder.fileSize;
this.ownerId = builder.ownerId;
this.storagePath = builder.storagePath;
}
public static class FileMetadataBuilder {
private String fileId;
private String fileName;
private String fileType;
private long fileSize;
private String ownerId;
private String storagePath;
public FileMetadataBuilder setFileId(String fileId) {
this.fileId = fileId;
return this;
}
public FileMetadataBuilder setFileName(String fileName) {
this.fileName = fileName;
return this;
}
public FileMetadataBuilder setFileType(String fileType) {
this.fileType = fileType;
return this;
}
public FileMetadataBuilder setFileSize(long fileSize) {
this.fileSize = fileSize;
return this;
}
public FileMetadataBuilder setOwnerId(String ownerId) {
this.ownerId = ownerId;
return this;
}
public FileMetadataBuilder setStoragePath(String storagePath) {
this.storagePath = storagePath;
return this;
}
public FileMetadata build() {
return new FileMetadata(this);
}
}
}
2. File Storage Service (Strategy + Factory + Singleton)
Copy // Strategy Interface
public interface StorageStrategy {
void storeFile(String fileName, byte[] data) throws IOException;
byte[] retrieveFile(String fileName) throws IOException;
}
// Concrete Strategy 1: Local Storage
public class LocalStorageStrategy implements StorageStrategy {
private static final String STORAGE_DIR = "local_storage/";
@Override
public void storeFile(String fileName, byte[] data) throws IOException {
Files.write(Paths.get(STORAGE_DIR + fileName), data);
}
@Override
public byte[] retrieveFile(String fileName) throws IOException {
return Files.readAllBytes(Paths.get(STORAGE_DIR + fileName));
}
}
// Concrete Strategy 2: Cloud Storage (AWS S3)
public class CloudStorageStrategy implements StorageStrategy {
@Override
public void storeFile(String fileName, byte[] data) {
System.out.println("Uploading " + fileName + " to AWS S3...");
}
@Override
public byte[] retrieveFile(String fileName) {
System.out.println("Downloading " + fileName + " from AWS S3...");
return new byte[0]; // Simulated response
}
}
// Factory Pattern for Storage Selection
public class StorageFactory {
public static StorageStrategy getStorageStrategy(String type) {
if ("LOCAL".equalsIgnoreCase(type)) {
return new LocalStorageStrategy();
} else if ("CLOUD".equalsIgnoreCase(type)) {
return new CloudStorageStrategy();
}
throw new IllegalArgumentException("Unknown storage type");
}
}
// Singleton File Storage Service
public class FileStorageService {
private static FileStorageService instance;
private StorageStrategy storageStrategy;
private FileStorageService(StorageStrategy strategy) {
this.storageStrategy = strategy;
}
public static FileStorageService getInstance(StorageStrategy strategy) {
if (instance == null) {
instance = new FileStorageService(strategy);
}
return instance;
}
public void uploadFile(String fileName, byte[] data) throws IOException {
storageStrategy.storeFile(fileName, data);
}
public byte[] downloadFile(String fileName) throws IOException {
return storageStrategy.retrieveFile(fileName);
}
}
3. File Sharing Service (Observer Pattern)
Used to notify users when a file is shared.
Copy import java.util.ArrayList;
import java.util.List;
interface Observer {
void update(String message);
}
class User implements Observer {
private String name;
public User(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received notification: " + message);
}
}
class FileSharingService {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void shareFile(String fileId, String userName) {
System.out.println("File " + fileId + " shared with " + userName);
notifyObservers("File " + fileId + " shared with you.");
}
private void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
4. Access Control (Proxy Pattern)
Restricts unauthorized file access.
Copy interface FileAccess {
void accessFile(String fileName, String userId);
}
class RealFileAccess implements FileAccess {
@Override
public void accessFile(String fileName, String userId) {
System.out.println(userId + " is accessing " + fileName);
}
}
class FileAccessProxy implements FileAccess {
private RealFileAccess realFileAccess;
private boolean isAuthenticated;
public FileAccessProxy(boolean isAuthenticated) {
this.realFileAccess = new RealFileAccess();
this.isAuthenticated = isAuthenticated;
}
@Override
public void accessFile(String fileName, String userId) {
if (isAuthenticated) {
realFileAccess.accessFile(fileName, userId);
} else {
System.out.println("Access Denied for " + userId);
}
}
}