Skip to content

Instantly share code, notes, and snippets.

@mtov
Last active October 30, 2021 15:54
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mtov/c8d65378a2904af01c20c53922f5ae1d to your computer and use it in GitHub Desktop.
Channel Decorator (Design patterns)
interface Channel {
void send(String msg);
String receive();
}
class TCPChannel implements Channel {
public void send(String m) {
System.out.println("Enviando via TCP > " + m);
}
public String receive() {
System.out.println("Recebendo via TCP");
return "Hello!";
}
}
class ChannelDecorator implements Channel {
private Channel channel;
public ChannelDecorator(Channel channel) {
this.channel = channel;
}
public void send(String m) {
channel.send(m);
}
public String receive() {
return channel.receive();
}
}
class ZipChannel extends ChannelDecorator {
public ZipChannel(Channel c) {
super(c);
}
public void send(String m) {
System.out.println("Compactando > " + m);
super.send(m);
}
public String receive() {
String m = super.receive();
System.out.println("Descompactando < " + m);
return m;
}
}
public class Main {
public static void main(String args[]) {
Channel c = new ZipChannel(new TCPChannel());
c.send("Hello, World");
String r = c.receive();
System.out.println(r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment