Last active
October 30, 2021 15:54
-
-
Save mtov/c8d65378a2904af01c20c53922f5ae1d to your computer and use it in GitHub Desktop.
Channel Decorator (Design patterns)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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