返回
结构型设计模式:构建更优异的Java应用程序
后端
2022-12-30 13:26:52
结构型设计模式专注于类与对象之间的组织方式,以增强其灵活性。这类模式通常用于处理系统中组件之间的复杂关系。常见的结构型设计模式包括代理、适配器、装饰器、桥接、组合、外观以及享元等。
代理模式(Proxy Pattern)
代理模式通过为某对象提供一个代理来控制对这个对象的访问。这种设计模式适用于远程调用,或是作为保护对象的屏障。通过代理,可以增强目标功能或改变目标角色的行为方式。
代码示例:
interface Image {
void display();
}
class RealImage implements Image {
private String fileName;
public RealImage(String fileName) {
this.fileName = fileName;
loadFromDisk(fileName);
}
@Override
public void display() {
System.out.println("Displaying " + fileName);
}
private void loadFromDisk(String fileName) {
System.out.println("Loading " + fileName);
}
}
class ProxyImage implements Image {
private RealImage realImage;
private String fileName;
public ProxyImage(String fileName) {
this.fileName = fileName;
}
@Override
public void display() {
if (realImage == null) {
realImage = new RealImage(fileName);
}
realImage.display();
}
}
适配器模式(Adapter Pattern)
当接口不匹配时,使用适配器模式。这允许类的接口与客户端代码需要的服务相适应。通过适配器可以将一个类的接口转换成客户希望的另一个接口。
代码示例:
interface Target {
void request();
}
class Adaptee {
public void specificRequest() {
System.out.println("Called specificRequest");
}
}
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
装饰器模式(Decorator Pattern)
装饰器模式用于动态地给一个对象添加一些额外的职责。这种模式提供了一种比继承更加灵活的扩展机制。
代码示例:
interface Component {
void operation();
}
class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("ConcreteComponent operation");
}
}
abstract class Decorator implements Component {
protected Component component;
public Decorator(Component c) {
this.component = c;
}
@Override
public void operation() {
if (component != null) {
component.operation();
}
}
}
class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component c) {
super(c);
}
@Override
public void operation() {
super.operation();
System.out.println("ConcreteDecorator added functionality");
}
}
桥接模式(Bridge Pattern)
桥接模式将抽象部分与实现部分分离,使它们可以独立变化。这使得系统更加灵活,扩展也变得容易。
代码示例:
interface Abstraction {
void operation(Implementor impl);
}
class RefinedAbstraction extends Abstraction {
@Override
public void operation(Implementor impl) {
System.out.print("RefinedAbstraction operation with ");
impl.operation();
}
}
interface Implementor {
void operation();
}
class ConcreteImplementorA implements Implementor {
@Override
public void operation() {
System.out.println("ConcreteImplementorA");
}
}
组合模式(Composite Pattern)
组合模式将对象组合成树形结构以表示“部分-整体”的层次关系。通过这个模式,用户可以一致地使用单个对象和组合对象。
代码示例:
interface Graphic {
void print();
}
class CompositeGraphic implements Graphic {
private List<Graphic> children = new ArrayList<>();
@Override
public void print() {
for (Graphic graphic : children) {
graphic.print();
}
}
public void add(Graphic g) {
children.add(g);
}
public void remove(Graphic g) {
children.remove(g);
}
}
class Ellipse implements Graphic {
@Override
public void print() {
System.out.println("Ellipse");
}
}
外观模式(Facade Pattern)
外观模式提供了一个统一的接口,用来访问子系统中的多个组件。它简化了客户端与复杂子系统的交互。
代码示例:
class Module1 {
public void doSomething() { System.out.println("Doing something in module 1"); }
}
class Module2 {
public void doSomeOtherThing() { System.out.println("Doing some other thing in module 2"); }
}
class Facade {
private Module1 module1;
private Module2 module2;
public Facade(Module1 m1, Module2 m2) {
this.module1 = m1;
this.module2 = m2;
}
public void doEverything() {
module1.doSomething();
module2.doSomeOtherThing();
}
}
享元模式(Flyweight Pattern)
享元模式用于减少对象的创建,从而提高性能。通过共享大量细粒度的对象,它可以显著地节省内存。
代码示例:
interface Flyweight {
void operation(String state);
}
class ConcreteFlyweight implements Flyweight {
@Override
public void operation(String state) {
System.out.println("ConcreteFlyweight with " + state);
}
}
class FlyweightFactory {
private Map<String, Flyweight> flyweights = new HashMap<>();
public Flyweight getFlyweight(String key) {
if (!flyweights.containsKey(key)) {
flyweights.put(key, new ConcreteFlyweight());
}
return flyweights.get(key);
}
}
这些模式的实现为Java应用程序提供了强大的结构基础,帮助开发者构建更优异且灵活的应用。理解并恰当地运用这些设计模式对于提高代码质量和维护性至关重要。
相关资源: