Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of decorative Mode of Java IO

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the principle of decorative mode of Java IO". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the principle of decorative mode of Java IO".

The Java processing flow is equivalent to wrapping and decorating the node stream (classes that start with File or Byte). The principle of the decoration pattern is as follows

Package com.sxt.io / * * simulated coffee * 1, abstract components: abstract objects that need to be decorated (interfaces or abstract parent classes) * 2, concrete components: objects that need to be decorated * 3, abstract decoration classes: contain references to abstract components and decorate common methods * 4, Specific decoration class: decorated object * * @ author TW * / public class DecorateTest02 {public static void main (String [] args) {Drink coffee = new Coffee () Drink suger = new Suger (coffee); / / decorative System.out.println (suger.info () + "-->" + suger.cost ()); Drink milk = new Milk (coffee); / / decorative System.out.println (milk.info () + "-->" + milk.cost ()); milk = new Milk (suger) / / decorate System.out.println (milk.info () + "-->" + milk.cost ());}} / / Abstract component interface Drink {double cost (); / / cost String info (); / / description} / / concrete component class Coffee implements Drink {private String name = "original coffee" @ Override public double cost () {return 10;} @ Override public String info () {return name;}} / / Abstract Decoration Class abstract class Decorate implements Drink {/ / A reference to a pair of abstract components private Drink drink Public Decorate (Drink drink) {this.drink = drink;} @ Override public double cost () {return this.drink.cost ();} @ Override public String info () {return this.drink.info () }} / / specific decoration class class Milk extends Decorate {public Milk (Drink drink) {super (drink);} @ Override public double cost () {return super.cost () * 4 } @ Override public String info () {return super.info () + "added milk";}} class Suger extends Decorate {public Suger (Drink drink) {super (drink);} @ Override public double cost () {return super.cost () * 2 } @ Override public String info () {return super.info () + "added sugar" }} Thank you for your reading, the above is the content of "the principle of Java IO Decoration Mode". After the study of this article, I believe you have a deeper understanding of the principle of Java IO decoration mode, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 266

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report