Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The principle of decorative Mode of Java IO

Shulou Source: shulou.com Published: 2022-06-02 05:14:47 10月29日 Update

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!

Tags: Components principles patterns objects learning content coffee original flavor that is beginning ideas situations interfaces articles methods more milk knowledge knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Redmi Docker MariaDB