packagecn.imcompany.decorator.encrypt;/**
* Created by tomyli on 2018/6/25.
* Github: https://github.com/peng051410
*/publicinterfaceEncryptor{voidencrypt();}
具体加密类,实现加密处理接口
1
2
3
4
5
6
7
8
9
10
11
12
13
packagecn.imcompany.decorator.encrypt;/**
* Created by tomyli on 2018/6/25.
* Github: https://github.com/peng051410
*/publicclassConcreteEncryptorimplementsEncryptor{@Overridepublicvoidencrypt(){System.out.println("base encrypt!");}}
抽象加密装饰器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
packagecn.imcompany.decorator.encrypt;/**
* Created by tomyli on 2018/6/25.
* Github: https://github.com/peng051410
*/publicabstractclassEncryptDecoratorimplementsEncryptor{privateEncryptorencrypt;publicEncryptDecorator(Encryptorencrypt){this.encrypt=encrypt;}@Overridepublicvoidencrypt(){encrypt.encrypt();}}
packagecn.imcompany.decorator.encrypt;/**
* Created by tomyli on 2018/6/25.
* Github: https://github.com/peng051410
*/publicclassModEncryptDecoratorextendsEncryptDecorator{publicModEncryptDecorator(Encryptorencrypt){super(encrypt);}@Overridepublicvoidencrypt(){super.encrypt();modEncrypt();}publicvoidmodEncrypt(){System.out.println("取模加密!");}}packagecn.imcompany.decorator.encrypt;/**
* Created by tomyli on 2018/6/25.
* Github: https://github.com/peng051410
*/publicclassConverseEncryptDecoratorextendsEncryptDecorator{publicConverseEncryptDecorator(Encryptorencrypt){super(encrypt);}@Overridepublicvoidencrypt(){super.encrypt();converseEncrypt();}publicvoidconverseEncrypt(){System.out.println("逆向加密!");}}
packagecn.imcompany.decorator.encrypt;/**
* Created by tomyli on 2018/6/25.
* Github: https://github.com/peng051410
*/publicclassEncryptDecoratorTest{publicstaticvoidmain(String[]args){Encryptorencryptor=newConcreteEncryptor();//进行一次简单加密
EncryptorencryptDecorator=newSimpleEncryptDecorator(encryptor);encryptDecorator.encrypt();//对上一次加密进行二次加密(反转加密)
ConverseEncryptDecoratorconverseEncryptDecorator=newConverseEncryptDecorator(encryptDecorator);converseEncryptDecorator.encrypt();}}