Decorator mode in Java Design Mode

Source: Internet
Author: User

Decorator mode in Java Design Mode
To implement the modifier mode, pay attention to the following points: 1. the modifier class must implement the same interface of the real class. 2. the modifier class contains a reference to a real object (which can be passed in through the constructor of the modifier class). 3. the decoration Class Object accepts the request in the main class and sends the request to the real object (equivalent to the actual object that has passed the reference to the decoration class) 4. the decorator can add some additional functions after passing in the real object (because the decoration object and the real object both have the same method, some operations can be added to the decoration object to call the method of the real object, or call the method of the real object first, and then add your own method) 5. don't inherit, first talk to the instance, and then the specific decorator pattern assumes that you want to create a steamed bread with the addition of cookies and colorants: 1. produce a normal steamed bread 2. to save costs (not using corn flour), add the dyeing agent to the normal steamed bread. and finally produce a steamed bread .. first, the IBread interface is implemented to prepare materials, dough, steamed bread, and steamed bread (that is, call the previous three steps). 1. package decoration mode; 2. public interface IBread {4 5 public void prepair (); 6 7 public void kneadFlour (); 8 9 public void steamed (); 10 11 public void process (); 12} II. make the regular steamed bread 1 package decoration mode; 2 3 public class NormalBread implements IBread {4 5 @ Override 6 public void prepair () {7 8 System. out. println ("Prepare flour, water and baking powder... "); 9} 10 11 @ Override12 public void kneadFlour () {13 14 System. out. println ("and... "); 15} 16 17 @ Override18 public void steamed () {1 9 20 System. out. println ("steamed bread... "); 21} 22 23 @ Override24 public void process () {25 26 prepair (); 27 kneadFlour (); 28 steamed (); 29} 30 31} 3. the abstract class that defines the production of bread implements the IBread interface for the production of bread, and instances that contain the IBread interface are not required, it can be completely reprocessed when an IBread interface instance is passed in. However, for the sake of universality, adding the abstract class corresponds to the preceding 2nd points of attention: there is a real object reference 1 package decoration mode in the decoration class; 2 3 public abstract class AbstractBread implements IBread {4 5 private final IBread bread; 6 7 public AbstractBre Ad (IBread bread) {8 super (); 9 this. bread = bread; 10} 11 @ Override12 public void prepair () {13 this. bread. prepair (); 14} 15 @ Override16 public void kneadFlour () {17 this. bread. kneadFlour (); 18} 19 @ Override20 public void steamed () {21 this. bread. steamed (); 22} 23 24 @ Override25 public void process () {26 prepair (); 27 kneadFlour (); 28 steamed (); 29} 30 31} 4. the "Corn steamed bread" with colorant production inherits the AbstarctBread class, so we can choose to cover normal production. Steamed Bread Production method, and add the original information of the original method, you can also add your own method in the modifier mode here the most critical, corresponding to the above 1st points of attention: the modifier class must implement the same interface 1 package decoration mode as the real class; 2 3 public class CornDecorator extends AbstractBread {4 5 public CornDecorator (IBread bread) {6 7 super (bread ); 8} 9 10 public void paint () {11 12 System. out. println ("add lemon yellow colorant"); 13} 14 @ Override15 public void kneadFlour () {16 // Add the colorant and 17 this. paint (); 18 super. kneadFlour (); 19} 20 21 22} 5. produce "sweet steamed bread" with sweetness and Four identical 1 package decoration mode; 2 3 public class SweetDecorator extends AbstractBread {4 5 public SweetDecorator (IBread bread) {6 7 super (bread); 8} 9 10 public void paint () {11 12 System. out. println ("Add sweet... "); 13} 14 15 @ Override16 public void kneadFlour () {17 // Add the sweetness and dough 18 this. paint (); 19 super. kneadFlour (); 20} 21 22} 6. start to create the steamed bread 1 package decoration mode for adding cookies and colorants; 2 3 public class Client {4 5 public static void ma In (String [] args) {6 7 System. out. println ("======= start decorating steamed bread"); 8 IBread normalBread = new NormalBread (); 9 normalBread = new SweetDecorator (normalBread ); 10 normalBread = new CornDecorator (normalBread); 11 normalBread. process (); 12 System. out. println ("======= decorative steamed bread"); 13} 14} VII. output 1 ======== start decorating steamed bread 2 prepare flour, water and baking powder... 3 add lemon yellow colorant 4 Add sweet... 5. Dough... 6 steamed bread... the Fragrant steamed bread comes with 7 ========== 4 roles in the decorative steamed bread end modifier mode (1) decorator abstract Component: Yes An interface or abstract class that defines the core object. This class is the base class of the decorator. For example, the IBread interface (2) is implemented by the decorator ConcreteComponent: this is the implementation of the Component interface or abstract class. For example, NormalBread (3) Decorator in this example: it is generally an abstract class that implements Component. It must have a reference pointing to Component, for example, in this example, AbstractBread (4) implements ConcreteDecorator1 and ConcreteDecorator2: used to decorate the most basic classes. For example, CornDecorator in this example, the CornDecorator mode in JDK is java. io uses the decorator mode. For example, FilterInputStream inherits (implements) InputStream. At the same time, BufferedInputStream inherits FilterInputStream, 1, which is an abstract component of the decorator: that is, the top-level base class InputStrea M2. The specific ConcreteComponent implemented by the decorator: FileInputStream and FileOutputStream are its implementations. 3. decorator: There is an InputStream instance and constructor in FilterInputStream to pass in the InputStream object protected volatile InputStream in; protected FilterInputStream (InputStream in) {this. in = in;} 4. constructor implementation: a constructor in BufferedInputStream is used to pass in the InputStream object. public BufferedInputStream (InputStream in, int size) {super (in); if (size <= 0) {throw new IllegalArgumen TException ("Buffer size <= 0");} buf = new byte [size];} the constructor is surprised to find that the process is exactly the same. (can be changed) 1. inputStream --> IBread (here is InputStream, there is nothing to say) 2. fileInputStream --> NormalBread3.FilterInputStream --> AbstractBread (implements Component. Here is InputStram. it must have a reference pointing to Component. This reference is the instance of InputStream.) 4. bufferedInputStream --> CornDecorator The kneadFlour () method in ecorator also has super. kneadFlour ();, but the constructor method is used in the IO stream.) This is why the decorator mode in JDK does not need to inherit from the constructor mode (this example describes the object ,) 1. if you can only add pigments or cookies separately, you only need to inherit the NormalBread and SweetDecorator from the CornDecorator, which can also overwrite the normal bread process, add additional functions to separate "Corn steamed bread" and "sweet steamed bread ". 2. in this way, if we want to create sweet corn steamed bread (Here we add sweet pigments first, and then add corn pigments), we only need to SweetDecorator to inherit NormalBread, and then CornSweetDecorator to inherit CornDecorator, there seems to be no problem. 2. but consider the following: if we want to be able to add in the middle of adding sweet pigment and corn pigment, we need What should I do when I add onions? I can hardly say that I use SweetDecorator to inherit NormalBread first, then OnionSweetDecorator to inherit SweetDecorator, and then use CornOnionSweetDecorator to inherit OnionSweetDecorator ?? Obviously, it is impossible. This will lead to a low reusability of the original code and form a redundant inheritance system. 4. the above instance method completely overcomes this problem. To add onions, you only need to perform steps similar to SweetDecorator, finally, you can implement this function in the Client class. 1. when you need to add a new function or role to an existing object, you can consider using the decoration Mode 2. the roles and responsibilities of an object change frequently or need to be dynamically added to avoid inheritance and expansion methods to adapt to such changes.

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.