"Decorator Mode" in Java design mode

Source: Internet
Author: User

"Decorator Mode" in Java design mode

Decoration mode The decoration of the new house does not change the nature of the house, but it can make the building more beautiful, more warm and more practical. In software design, the functions of existing objects (new houses) are extended (renovated). The common function is encapsulated in the adorner, where it is used to make the call. Adornment mode is a technique used in place of inheritance, which uses an association relationship between objects to replace the inheritance relationship between classes. Introduce the decoration class, expand the new function. Role Abstract Component concrete component abstract decoration class specific decoration class
Case one, form decoration

1. Component Classes

Package Decorator; Decorator mode

/**

    • Created by jiqing on 2016/10/13.
      */
      Abstract class Component {
      public abstract void display ();
      }
      2. Component Decorators

Package Decorator;

/**

    • Created by jiqing on 2016/10/13.
      */
      public class Componentdecorator extends component{
      Private Component Component; Maintain a reference to an abstract artifact type Object
      Public Componentdecorator (Component Component) {
      This.component = component;
      }

public void display () {
Component.display ();
}

}
3. Inheriting a class ListBox

Package Decorator;

/**

    • Created by jiqing on 2016/10/13.
      */
      public class ListBox extends component{
      public void display () {
      System.out.println ("Show list box! ");
      }
      }
      4. Inheriting class TextBox

Package Decorator;

/**

    • Created by jiqing on 2016/10/13.
      */
      public class TextBox extends component{
      public void display () {
      System.out.println ("Show text box! ");
      }
      }
      5. Inherit Class window

Package Decorator;

/**

    • Created by jiqing on 2016/10/13.
      */
      public class Window extends component{
      public void display () {
      System.out.println ("Show form! ");
      }
      }
      6. Black box Decorator

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 ("Add a black border to the widget! ");

}
}
7. Scroll bar Decorator

Package Decorator;

/**

    • Created by jiqing on 2016/10/14.
      */
      public class Scrollbardecorator extends componentdecorator{
      Public Scrollbardecorator (Component Component) {
      Super (component); Calling the parent class constructor
      }

public void display () {
This.setscrollbar ();
Super.display ();
}

public void Setscrollbar () {
SYSTEM.OUT.PRINTLN ("Add scroll bar for widget! ");
}
}
8. Client Calls

Package Decorator; Decorator mode

/**

    • 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 ();
      }
      }
      Execution results
Add scroll bars to the widget!
Show Form!

Add a black border to the widget!
Add scroll bars to the widget!
Show Form!
422101-20161014225513875-1745462407.png

Case two, the redaction decoration

1. Cipher-Text interface

Package decorator.sample2;

/**

    • Created by jiqing on 2016/10/14.
      */
      public interface Cipher//ciphertext interface
      {
      public string Encrypt (string plaintext);
      }
      2. The redaction decorator

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. Ciphertext interface Implementation Class

Package decorator.sample2;

/**

    • Created by jiqing on 2016/10/14.
      */
      Public final class Simplecipher implements Cipher//Simple ciphertext inherits cipher text
      {
      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. Complex Crypto Decorator

Package decorator.sample2;

/**

    • Created by jiqing on 2016/10/14.
      */
      public class Complexcipher extends Cipherdecorator//complex redaction
      {
      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. Advanced Encryption Decorator

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) {//encryption processing
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. Client

Package decorator.sample2;

/**

    • Created by jiqing on 2016/10/14.
      */
      public class Client {
      public static void Main (String args[])
      {
      String password= "Jiqing9006"; Plaintext
      String Cpassword; Ciphertext
      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 ("---------------------");
}
}
Execution results

powotm90066009mtowop0003123532

422101-20161014225522812-515626629.png

"Decorator Mode" in Java design mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.