JAVA設計模式之【裝飾者模式】
裝飾模式 對新房進行裝修並沒有改變房屋的本質,但它可以讓房子變得更漂亮、更溫馨、更實用。 在軟件設計中,對已有對象(新房)的功能進行擴展(裝修)。 把通用功能封裝在裝飾器中,用到的地方進行調用。 裝飾模式是一種用於替代繼承的技術,使用對象之間的關聯關系取代類之間的繼承關系。引入裝飾類,擴充新功能。 角色 抽象構件 具體構件 抽象裝飾類 具體裝飾類
案例一,窗體裝飾
1.組件類
package Decorator; // 裝飾者模式
/**
* Created by Jiqing on 2016/10/13.
*/
abstract class Component {
public abstract void display();
}
2.組件裝飾者
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class ComponentDecorator extends Component{
private Component component; // 維持對抽象構件類型對象的引用
public ComponentDecorator(Component component){
this.component = component;
}
public void display() {
component.display();
}
}
3.繼承類ListBox
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class ListBox extends Component{
public void display() {
System.out.println("顯示列表框!");
}
}
4.繼承類TextBox
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class TextBox extends Component{
public void display() {
System.out.println("顯示文本框!");
}
}
5.繼承類Window
package Decorator;
/**
* Created by Jiqing on 2016/10/13.
*/
public class Window extends Component{
public void display() {
System.out.println("顯示窗體!");
}
}
6.黑框裝飾者
package Decorator;
/**
* Created by Jiqing on 2016/10/14.
*/
public class BlackBoarderDecorator extends ComponentDecorator{
public BlackBoarderDecorator(Component component) {
super(component);
}
public void display() {
this.setBlackBoarder();
super.display();
}
public void setBlackBoarder() {
System.out.println("為構件增加黑色邊框!");
}
}
7.滾動條裝飾者
package Decorator;
/**
* Created by Jiqing on 2016/10/14.
*/
public class ScrollBarDecorator extends ComponentDecorator{
public ScrollBarDecorator (Component component) {
super(component); // 調用父類構造函數
}
public void display() {
this.setScrollBar();
super.display();
}
public void setScrollBar() {
System.out.println("為構件增加滾動條!");
}
}
8.客戶端調用
package Decorator; // 裝飾者模式
/**
* Created by Jiqing on 2016/10/14.
*/
public class Client {
public static void main(String args[]) {
Component component,componentSB,componentBB;
component = new Window();
componentSB = new ScrollBarDecorator(component);
componentSB.display();
System.out.println("--------------------");
componentBB = new BlackBoarderDecorator(componentSB);
componentBB.display();
}
}
執行結果
為構件增加滾動條!
顯示窗體!
--------------------
為構件增加黑色邊框!
為構件增加滾動條!
顯示窗體!
案例二,密文裝飾
1.密文接口
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public interface Cipher // 密文接口
{
public String encrypt(String plainText);
}
2.密文裝飾者
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public class CipherDecorator implements Cipher{
private Cipher cipher;
public CipherDecorator(Cipher cipher) {
this.cipher = cipher;
}
public String encrypt(String plainText) {
return cipher.encrypt(plainText);
}
}
3.密文接口實現類
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
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;
}
}
4.復雜加密裝飾者
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
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;
}
}
5.先進加密裝飾者
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
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;
}
}
6.客戶端
package Decorator.sample2;
/**
* Created by Jiqing on 2016/10/14.
*/
public class Client {
public static void main(String args[])
{
String password="Jiqing9006"; //明文
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("---------------------");
}
}
執行結果
Powotm9006
---------------------
6009mtowoP
---------------------
0003123532
---------------------