Design Mode-Python Java modifier Mode

Source: Internet
Author: User

Design Mode-Python Java modifier Mode

 

The modifier mode defines dynamically attaching a responsibility to an object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.

 

Implementation class diagram

    

 

Characteristics of the modifier Mode

 

A. decorator and decorated objectHas the same super type. (Note that this method uses inheritance to achieve "type matching", instead of using inheritance to obtain "behavior". The behavior comes from the decoration and basic components, or the combination between the behavior and other decorations.)

B can wrap an object with one or more decorators.

C because the decorator and the decorator have the same type, any occasion that requires the original object can be replaced by the decorated object.

DThe decorator Can add his/her own behavior before and after the behavior of the entrusted decorator to achieve a specific purpose.

E objects can be decorated at any time, so you can use your favorite decorator to decorate objects dynamically and unlimited during runtime.

 

Decorator mode implementation example
Interface Widget {void draw ();} // 1. "lowest common denominator" class TextField implements Widget {// 3. "Core" class with "isa" rel private int width, height; public TextField (int w, int h) {width = w; height = h;} public void draw () {System. out. println ("TextField:" + width + "," + height) ;}// 2. second level base classabstract class Decorator implements Widget {// with "isa" relationship private Widget wid; // 4. "hasa" relationship public Decorator (Widget w) {wid = w;} public void draw () {wid. draw ();} // 5. delegation} class BorderDecorator extends Decorator {// 6. optional embellishment public BorderDecorator (Widget w) {super (w);} public void draw () {super. draw (); // 7. delegate to base class System. out. println ("BorderDecorator"); // and add extra stuff} class ScrollDecorator extends Decorator {// 6. optional embellishment public ScrollDecorator (Widget w) {super (w);} public void draw () {super. draw (); // 7. delegate to base class System. out. println ("ScrollDecorator"); // and add extra stuff} // The decorator Can add his/her own behavior before or after the behavior of the decorator entrusted, for specific purposes, public class DecoratorDemo {public static void main (String [] args) {// 8. client has the responsibility to compose desired configurations Widget aWidget = new BorderDecorator (new ScrollDecorator (new TextField (80, 24); aWidget. draw ();}}

Output result: TextField: 80, 24
ScrollDecorator
BorderDecorator
BorderDecorator

 

A similar program implemented by the python decoration Mode

 

def bread(func):    def wrapper():        print "
 "        func()        print "<\______/>"    return wrapperdef ingredients(func):    def wrapper():        print "#tomatoes#"        func()        print "~salad~"    return wrapper@bread@ingredientsdef sandwich(food="--ham--"):    print foodif (__name__=="__main__"):    sandwich()

 

The java I/O class uses the modifier mode to implement many reading methods. InputStream is an abstract component of the modifier. FilterInputStream is an abstract decorator. LIneNumberInputStream is a specific decorator. With the ability to calculate the number of rows, BufferInputStream is a specific decorator with the buffer input function and readline () method.

The following example shows how to write your own Java I./O decorator to convert all uppercase characters in the input stream to lowercase letters.

 

Import java. io. *; class LowerCaseInputStream extends FilterInputStream {public LowerCaseInputStream (InputStream in) {super (in) ;}// you must implement two read methods, one for byte and one for byte array public int read () throws IOException {int c = super. read (); return (c =-1? C: Character. toLowerCase (char) (c);} public int read (byte [] B, int offset, int len) throws IOException {int result = super. read (B, offset, len); for (int I = offset; I <offset + result; I ++) {B [I] = (byte) Character. toLowerCase (char) B [I]);} return result ;}} public class InputTest {public static void main (String [] args) {int c; try {InputStream in = new LowerCaseInputStream (new BufferedInputStream (new FileInputStream ("test.txt"); while (c = in. read ()> = 0) {System. out. print (char) c);} in. close ();} catch (IOException e) {e. printStackTrace ();}}


 

 

 

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.