实例一:多重加密系统
某系统提供了一个数据加密功能,可以对字符串进行加密。最简单的加密算法通过对字母进行移位来实现,同时还提供了稍复杂的逆向输出加密,还提供了更为高级的求模加密。用户先使用最简单的加密算法对字符串进行加密,如果觉得还不够可以对加密之后的结果使用其他加密算法进行二次加密,当然也可以进行第三次加密。现使用装饰模式设计该多重加密系统。
public interface Cipher
{
public String encrypt(String plainText);
}
public final class SimpleCipher implements Cipher {
public String encrypt(String plainText) {
String str = "";
for (int i = 0; i < plainText.length(); i++) {
char c = plainText.charAt(i);
if (c >= 'a' && c <= 'z') {
c += 6;
if (c > 'z') c -= 26;
if (c < 'a') c += 26;
}
if (c >= 'A' && c <= 'Z') {
c += 6;
if (c > 'Z') c -= 26;
if (c < 'A') c += 26;
}
str += c;
}
return str;
}
}
public class CipherDecorator implements Cipher {
private Cipher cipher;
public CipherDecorator(Cipher cipher) {
this.cipher = cipher;
}
public String encrypt(String plainText) {
return cipher.encrypt(plainText);
}
}
public class ComplexCipher extends CipherDecorator {
public ComplexCipher(Cipher cipher) {
super(cipher);
}
public String encrypt(String plainText) {
String result = super.encrypt(plainText);
result = this.reverse(result);
return result;
}
public String reverse(String text) {
String str = "";
for (int i = text.length(); i > 0; i--) {
str += text.substring(i - 1, i);
}
return str;
}
}
public class AdvancedCipher extends CipherDecorator {
public AdvancedCipher(Cipher cipher) {
super(cipher);
}
public String encrypt(String plainText) {
String result = super.encrypt(plainText);
result = mod(result);
return result;
}
public String mod(String text) {
String str = "";
for (int i = 0; i < text.length(); i++) {
String c = String.valueOf(text.charAt(i) % 6);
str += c;
}
return str;
}
}
public class Client {
public static void main(String args[]) {
String password = "sunnyLiu";
String cpassword;
Cipher sc, ac, cc;
sc = new SimpleCipher();
cpassword = sc.encrypt(password);
System.out.println(cpassword);
System.out.println("---------------------");
cc = new ComplexCipher(sc);
cpassword = cc.encrypt(password);
System.out.println(cpassword);
System.out.println("---------------------");
ac = new AdvancedCipher(cc);
cpassword = ac.encrypt(password);
System.out.println(cpassword);
System.out.println("---------------------");
}
}
实例二:经典举例咖啡店模式
(咖啡馆订单项目:1)、咖啡种类:Espresso、ShortBlack、LongBlack、Decaf2)、调料(装饰者):Milk、Soy、Chocolate),类图如上
被装饰的对象和装饰者都继承自同一个超类
public abstract class Drink {
public String description="";
private float price=0f;;
public void setDescription(String description)
{
this.description=description;
}
public String getDescription()
{
return description+"-"+this.getPrice();
}
public float getPrice()
{
return price;
}
public void setPrice(float price)
{
this.price=price;
}
public abstract float cost();
}
被装饰的对象,不用去改造。原来怎么样写,现在还是怎么写。
public class Coffee extends Drink {
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice();
}
}
装饰者不仅要考虑自身,还要考虑被它修饰的对象,它是在被修饰的对象上继续添加修饰。例如,咖啡里面加牛奶,再加巧克力。加糖后价格为coffee+milk。再加牛奶价格为coffee+milk+chocolate。
public class Decorator extends Drink {
private Drink Obj;
public Decorator(Drink Obj) {
this.Obj = Obj;
};
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice() + Obj.cost();
}
@Override
public String getDescription() {
return super.description + "-" + super.getPrice() + "&&" + Obj.getDescription();
}
}
装饰者实例化(加牛奶)。这里面要对被修饰的对象进行实例化。
public class Milk extends Decorator {
public Milk(Drink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("Milk");
super.setPrice(2.0f);
}
}
coffee店:初始化一个被修饰对象,修饰者实例需要对被修改者实例化,才能对具体的被修饰者进行修饰。
public class CoffeeBar {
public static void main(String[] args) {
Drink order;
order = new Decaf();
System.out.println("order1 price:" + order.cost());
System.out.println("order1 desc:" + order.getDescription());
System.out.println("****************");
order = new LongBlack();
order = new Milk(order);
order = new Chocolate(order);
order = new Chocolate(order);
System.out.println("order2 price:" + order.cost());
System.out.println("order2 desc:" + order.getDescription());
}
}