java设计模式------装饰着模式
java设计模式-------装饰者模式
设计原则、模式特点、适用性
- 1. 多用组合,少用继承。
- 2. 类应设计的对扩展开放,对修改关闭。
> 装饰模式的特点
> 装饰者模式的适用性
>
装饰者模式实例
1、 需求
看装饰者模式类图
现在就是要将需求符合上面类图
再看,用beverage代替上面的component,成为所有类的超类
public abstract class Beverage {
String description = "no description";
public String getDescription (){
return description;
}
public abstract double cost();
}
//HouseHand
public class HouseHand extends Beverage {
public HouseHand() {
super();
description = "HouseHand";
}
/**
* the price of HouseHand is 8.9$ .
*/
@Override
public double cost() {
return 8.9;
}
}
//DarkRoast
public class DarkRoast extends Beverage {
public DarkRoast() {
description = "DarkRoast";
}
public double cost() {
return .99;
}
}
//Decaf
public class Decaf extends Beverage {
public DarkRoast() {
description = "DarkRoast";
}
public double cost() {
return 1.05;
}
}
//Espresso
public class Espresso extends Beverage {
public DarkRoast() {
description = "DarkRoast";
}
public double cost() {
return 1.99;
}
}
public abstract class CondimentDecorator extends Beverage {
/**
* 提供一个必须要实现的获取当前组合的描述信息的方法。
* 方便观察结果。
*/
public abstract String getDescription();
}
// MochaDecorator
public class MochaDecorator extends CondimentDecorator {
Beverage beverage;
public MochaDecorator(Beverage beverage) {
super();
this.beverage = beverage;
}
/**
* 返回被包装的组合或者组件的描述加上当前包装信息。
*/
@Override
public String getDescription() {
return beverage.getDescription() + ", Mocha";
}
@Override
public double cost() {
return beverage.cost() + 10.3;
}
}
其他的MilkDecorator、WhipDecorator、SoyDecorator采用类似实现方法
//WhipDecorator
public class WhipDecorator extends CondimentDecorator {
Beverage beverage;
public Whip(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Whip";
}
public double cost() {
return .10 + beverage.cost();
}
}
//SoyDecorator
public class SoyDecorator extends CondimentDecorator {
Beverage beverage;
public Soy(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Soy";
}
public double cost() {
return .15 + beverage.cost();
}
}
package DecoratorModel;
public class StarbuzzCoffee {
public static void main(String[] args) {
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription() + " $" + beverage.cost());
Beverage beverage2 = new DarkRoast();
beverage2 = new MochaDecorator(beverage2);
beverage2 = new MochaDecorator(beverage2);
beverage2 = new WhipDecorator(beverage2);
System.out
.println(beverage2.getDescription() + " $" + beverage2.cost());
Beverage beverage3 = new HouseBlend();
beverage3 = new SoyDecorator(beverage2);
beverage3 = new MochaDecorator(beverage2);
beverage3 = new WhipDecorator(beverage2);
System.out
.println(beverage3.getDescription() + " $" + beverage3.cost());
}
}
java IO中的装饰者模式
采用上面的方法来写一个自己的文件输入装饰者,就好比上面例子中配料 ,如:四种可以搭配的选择:Milk、Mocha、Soy、Whip。
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class LowerCaseInputStream extends FilterInputStream
{
protected LowerCaseInputStream(InputStream in)
{
super(in);
}
@Override
public int read() throws IOException
{
int c = super.read();
return (c == -1 ? c : Character.toLowerCase((char) c));
}
@Override
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;
}
}
测试写所写的装饰者
import java.io.*;
public class InputTest
{
public static void main(String[] args) throws IOException
{
int c;
try
{
InputStream in = new LowerCaseInputStream(new BufferedInputStream(
new FileInputStream("test.txt")));//这里的test.txt为文件路径,这里还有一个细节,注意,首先是用BufferedInputStream装饰了FileInputStream,然后再用LowerCaseInputStream装饰
while ((c = in.read()) >= 0)
{
System.out.print((char) c);
}
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
装饰者模式缺点
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。