标签:i++ valueof 最简 生成 prot 增加 复杂度 new 对象
1 public class Decorator extends Component 2 { 3 private Component component; 4 public Decorator(Component component) 5 { 6 this.component=component; 7 } 8 public void operation() 9 { 10 component.operation(); 11 } 12 }
典型的具体装饰类代码:
1 public class ConcreteDecorator extends Decorator 2 { 3 public ConcreteDecorator(Component component) 4 { 5 super(component); 6 } 7 public void operation() //在子类的operation方法中增加一个方法 8 { 9 super.operation(); 10 addedBehavior(); 11 } 12 public void addedBehavior() 13 { 14 //新增方法 15 } 16 }
1 //抽象构建 2 public interface Transform 3 { 4 public void move(); 5 } 6 7 //具体构建 8 public final class Car implements Transform 9 { 10 public Car() 11 { 12 System.out.println("变形金刚是一辆车!"); 13 } 14 15 public void move() 16 { 17 System.out.println("在陆地上移动!"); 18 } 19 } 20 21 //抽象装饰类 22 public class Changer implements Transform 23 { 24 private Transform transform; 25 26 public Changer(Transform transform) 27 { 28 this.transform=transform; 29 } 30 31 public void move() 32 { 33 transform.move(); 34 } 35 } 36 37 //具体装饰类 38 public class Robot extends Changer 39 { 40 public Robot(Transform transform) 41 { 42 super(transform); 43 System.out.println("变成机器人!"); 44 } 45 46 public void say() 47 { 48 System.out.println("说话!"); 49 } 50 } 51 52 //具体装饰类 53 public class Airplane extends Changer 54 { 55 public Airplane(Transform transform) 56 { 57 super(transform); 58 System.out.println("变成飞机!"); 59 } 60 61 public void fly() 62 { 63 System.out.println("在天空飞翔!"); 64 } 65 } 66 67 //客户端 68 public class Client 69 { 70 public static void main(String args[]) 71 { 72 Transform camaro; 73 camaro=new Car(); 74 camaro.move(); 75 System.out.println("-----------------------------"); 76 77 Airplane bumblebee=new Airplane(camaro); 78 bumblebee.move(); 79 bumblebee.fly(); 80 } 81 }
1 //抽象构建 2 public interface Cipher 3 { 4 public String encrypt(String plainText); 5 } 6 7 //具体构建 8 public class SimpleCipher implements Cipher 9 { 10 public String encrypt(String plainText) 11 { 12 String str=""; 13 for(int i=0;i<plainText.length();i++) 14 { 15 char c=plainText.charAt(i); 16 if(c>=‘a‘&&c<=‘z‘) 17 { 18 c+=6; 19 if(c>‘z‘) c-=26; 20 if(c<‘a‘) c+=26; 21 } 22 if(c>=‘A‘&&c<=‘Z‘) 23 { 24 c+=6; 25 if(c>‘Z‘) c-=26; 26 if(c<‘A‘) c+=26; 27 } 28 str+=c; 29 } 30 return str; 31 } 32 } 33 34 //抽象装饰类 35 public class CipherDecorator implements Cipher 36 { 37 private Cipher cipher; 38 39 public CipherDecorator(Cipher cipher) 40 { 41 this.cipher=cipher; 42 } 43 44 public String encrypt(String plainText) 45 { 46 return cipher.encrypt(plainText); 47 } 48 } 49 //具体装饰类 50 public class ComplexCipher extends CipherDecorator 51 { 52 public ComplexCipher(Cipher cipher) 53 { 54 super(cipher); 55 } 56 57 public String encrypt(String plainText) 58 { 59 String result=super.encrypt(plainText); 60 result=reverse(result); 61 return result; 62 } 63 64 public String reverse(String text) 65 { 66 String str=""; 67 for(int i=text.length();i>0;i--) 68 { 69 str+=text.substring(i-1,i); 70 } 71 return str; 72 } 73 } 74 75 //具体装饰类 76 public class AdvancedCipher extends CipherDecorator 77 { 78 public AdvancedCipher(Cipher cipher) 79 { 80 super(cipher); 81 } 82 83 public String encrypt(String plainText) 84 { 85 String result=super.encrypt(plainText); 86 result=mod(result); 87 return result; 88 } 89 90 public String mod(String text) 91 { 92 String str=""; 93 for(int i=0;i<text.length();i++) 94 { 95 String c=String.valueOf(text.charAt(i)%6); 96 str+=c; 97 } 98 return str; 99 } 100 } 101 102 //客户端 103 public class Client 104 { 105 public static void main(String args[]) 106 { 107 String password="sunnyLiu"; //明文 108 String cpassword; //密文 109 Cipher sc,cc; 110 111 sc=new SimpleCipher(); 112 cpassword=sc.encrypt(password); 113 System.out.println(cpassword); 114 System.out.println("---------------------"); 115 116 cc=new ComplexCipher(sc); 117 cpassword=cc.encrypt(password); 118 System.out.println(cpassword); 119 System.out.println("---------------------"); 120 121 /* 122 ac=new AdvancedCipher(cc); 123 cpassword=ac.encrypt(password); 124 System.out.println(cpassword); 125 System.out.println("---------------------"); 126 */ 127 } 128 }
1 JList list = new JList(); 2 JScrollPane sp = new JScrollPane(list);
抽象装饰类:FilterInputStream
1 …… 2 protected volatile InputStream in; 3 protected FilterInputStream(InputStream in) { 4 this.in = in; 5 } 6 ……
1 …… 2 FileInputStream inFS=new FileInputStream("temp/fileSrc.txt"); 3 BufferedInputStream inBS=new BufferedInputStream(inFS); 4 //定义一个字节数组,用于存放缓冲数据 5 byte[] data = new byte[1024]; 6 inBS.read(data); 7 ……
1 Cipher sc,cc,ac; 2 sc=new SimpleCipher(); 3 cc=new ComplexCipher(sc); 4 ac=new AdvancedCipher(cc);
1 Transform camaro; 2 camaro=new Car(); 3 camaro.move(); 4 Robot bumblebee=new Robot(camaro); 5 bumblebee.move(); 6 bumblebee.say();
标签:i++ valueof 最简 生成 prot 增加 复杂度 new 对象
原文地址:https://www.cnblogs.com/WindSun/p/10263459.html