装饰器模式和代理模式都是在不改变原有对象的基础上,为对象添加新功能的设计模式。在这篇博客中,我们将讨论这两种模式的基本概念、常见问题及如何避免它们,并提供代码示例。
装饰器模式动态地将责任附加到对象上。若要扩展功能,装饰器提供了比继承更有弹性的替代方案。
interface Coffee {
double getCost();
String getDescription();
}
class SimpleCoffee implements Coffee {
@Override
public double getCost() {
return 5.0;
}
@Override
public String getDescription() {
return "Simple Coffee";
}
}
abstract class CoffeeDecorator implements Coffee {
protected Coffee coffee;
public CoffeeDecorator(Coffee coffee) {
this.coffee = coffee;
}
@Override
public double getCost() {
return coffee.getCost();
}
@Override
public String getDescription() {
return coffee.getDescription();
}
}
class MilkCoffee extends CoffeeDecorator {
public MilkCoffee(Coffee coffee) {
super(coffee);
}
@Override
public double getCost() {
return super.getCost() + 0.5;
}
@Override
public String getDescription() {
return super.getDescription() + ", with milk";
}
}
public class Main {
public static void main(String[] args) {
Coffee simpleCoffee = new SimpleCoffee();
System.out.println(simpleCoffee.getCost());
System.out.println(simpleCoffee.getDescription());
Coffee milkCoffee = new MilkCoffee(simpleCoffee);
System.out.println(milkCoffee.getCost());
System.out.println(milkCoffee.getDescription());
}
}
代理模式为一个对象提供一个代理以控制对该对象的访问。代理对象在客户端和目标对象之间起到中介作用。
interface Image {
void display();
}
class RealImage implements Image {
@Override
public void display() {
System.out.println("Displaying real image from disk.");
}
}
class ProxyImage implements Image {
private RealImage realImage;
private boolean isLoaded = false;
public ProxyImage() {
// Lazy loading
}
@Override
public void display() {
if (!isLoaded) {
realImage = new RealImage();
isLoaded = true;
}
realImage.display();
}
}
public class Main {
public static void main(String[] args) {
Image proxyImage = new ProxyImage();
proxyImage.display();
}
}
在实际应用中,装饰器模式用于扩展功能,而代理模式用于控制访问或提供额外功能。理解这两种模式的适用场景,避免上述问题,可以提高代码的可扩展性和可维护性。