Skip to content

Instantly share code, notes, and snippets.

@mtov mtov/Main.java
Last active Sep 15, 2019

Embed
What would you like to do?
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 {
protected 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.channel.send(m);
}
public String receive() {
String m = super.channel.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
You can’t perform that action at this time.