Last updated
// FileOut 인터페이스를 구현한 Decorator 추상 클래스
public abstract class Decorator implements FileOut {
private FileOut delegate; // 위임 대상
public Decorator(FileOut delegate) {
this.delegate = delegate;
}
protected void doDelegate(byte[] data) {
delegate.write(data); // delegate에 쓰기 위임
}
}
// Decorator 추상 클래스를 상속한 확장 클래스
public class EncryptionOut extends Decorator {
public EncryptionOut(FileOut delegate) {
super(delegate);
}
public void write(Byte[] data) {
byte[] encryptedData = encrypt(data);
super.doDelegate(encryptedData);
}
private byte[] encrypt(byte[] data){
...
}
}
// FileOut 인스턴스에 암호화 기능 추가
public static void main(String[] args) {
FileOut delegate = new FileOutImpl();
FileOut fileOut = new EncryptionOut(delegate);
fileOut.write(data);
}