JAVA設計模式之【裝飾者模式】

來源:互聯網
上載者:User

標籤:角色   表單   static   extend   system   sub   private   裝飾器   display   

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();
    }
    }
    執行結果
為構件增加捲軸!
顯示表單!

為構件增加黑色邊框!
為構件增加捲軸!
顯示表單!
422101-20161014225513875-1745462407.png

 案例二,密文裝飾

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("---------------------");
}
}
執行結果

Powotm90066009mtowoP0003123532

422101-20161014225522812-515626629.png

JAVA設計模式之【裝飾者模式】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.