The decorator pattern is to create a decorator and then pass the decorator in, and it is already decorated.
For example, I pass a house in, when out of the house planted a flower, this is a flower decorator;
I passed another decorator, and came out and brushed the wall, a decorator who painted the wall.
Now we are decorated with coffee, many kinds of coffee, so we need a super class.
Decorators are a lot of spices.
Here's the code:
Public abstract class Beverage { String description = "Unknow beverage"; Public String getdescription () { return description; } public abstract double Cost ();}
Public abstract class Condimentdecorator extends beverage {public abstract String getdescription ();
public class Espresso extends beverage {public Espresso () { description = "Espresso"; } @Override public double Cost () { return 1.99; }}
public class Houseblend extends beverage {public houseblend () { description = "Houseblend"; } @Override public double Cost () { return.; }}
public class Mocha extends Condimentdecorator { beverage beverage; Public Mocha (Beverage beverage) { this.beverage = beverage; } @Override public String getdescription () { return beverage.getdescription () + ", Mocha"; } @Override public double Cost () { return beverage.cost () +. }}
public class Soy extends Condimentdecorator { beverage beverage; Public Soy (Beverage beverage) { this.beverage = beverage; } @Override public String getdescription () { return beverage.getdescription () + ", Soy"; } @Override public double Cost () { return beverage.cost () +. }}
public class Whip extends Condimentdecorator { beverage beverage; Public Whip (Beverage beverage) { this.beverage = beverage; } @Override public String getdescription () { return beverage.getdescription () + ", Whip"; } @Override public double Cost () { return beverage.cost () +. }}
The following is a test class:
public class Starbuzzcoffee { @Test public void Test () { Beverage beverage = new Espresso (); Beverage = new Soy (beverage); System.out.println (beverage.getdescription () + "$" + beverage.cost ()); Beverage Beverage1 = new Houseblend (); Beverage1 = new Mocha (beverage1); Beverage1 = new Soy (beverage1); Beverage1 = new Whip (beverage1); System.out.println (beverage1.getdescription () + "$" + beverage1.cost ());} }
Operation Result:
The point to note is:
decorators need to inherit the decorator, because the way to rewrite the decorator ...
if necessary, you need to declare a decorator inside the decorator, store the previous adorner, and store the object for the call to rewrite (not mandatory)...
I/O in the JDK is the decorator pattern used.
The following uses decorator mode to write an implementation that makes the letters in the stream all lowercase:
public class Lowercaseinputstream extends filterinputstream{public Lowercaseinputstream (InputStream in) { Super (in); } 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;} }
The test classes are as follows:
public class Inputtest { @Test public void Testinputstream () throws IOException { int c; try{ InputStream in = new Lowercaseinputstream (new Bufferedinputstream (New FileInputStream ("H:\\workspace\\idea\ \dp\\src\\test\\resource\\test.txt "))); while ((c = in.read ()) >= 0) { System.out.print ((char) c); } In.close (); } catch (IOException e) { e.printstacktrace ();}} }
To create a new Test.txt file:
Operation Result:
Decorator Mode of design pattern