ToB企服应用市场:ToB评测及商务社交产业平台

标题: 计划模式之布局型模式 [打印本页]

作者: 惊落一身雪    时间: 2025-1-21 07:31
标题: 计划模式之布局型模式


在软件开发的天下里,计划模式是先辈们智慧的结晶,它们为我们提供了通用的解决方案来应对各种常见的软件计划问题。本日,我们深入探究计划模式中的布局型模式,并用 Java 语言来实现它们。
什么是布局型模式

布局型模式主要关注怎样将类或对象组合成更大的布局,以实现更复杂的功能。它就像是搭建积木,通过不同的组合方式,让简单的组件发挥出强盛的作用。布局型模式可以分为类布局型模式和对象布局型模式,前者主要通过继承机制来组合类,后者则通过对象的组合来实现。
常见的布局型模式

署理模式(Proxy Pattern)

关键要点:署理模式为其他对象提供一种署理以控制对这个对象的访问。当无法或不想直接访问某个对象时,署理对象可以代替目标对象举行工作。
代码示例
  1. // 定义接口
  2. interface Subject {
  3. void request();
  4. }
  5. // 真实主题类
  6. class RealSubject implements Subject {
  7. @Override
  8. public void request() {
  9. System.out.println("真实主题执行请求");
  10. }
  11. }
  12. // 代理类
  13. class Proxy implements Subject {
  14. private RealSubject realSubject;
  15. public Proxy(RealSubject realSubject) {
  16. this.realSubject = realSubject;
  17. }
  18. @Override
  19. public void request() {
  20. // 可以在调用真实主题前后进行额外操作
  21. System.out.println("代理预处理");
  22. realSubject.request();
  23. System.out.println("代理后处理");
  24. }
  25. }
复制代码
利用示例
  1. public class ProxyPatternExample {
  2. public static void main(String[] args) {
  3. RealSubject realSubject = new RealSubject();
  4. Proxy proxy = new Proxy(realSubject);
  5. proxy.request();
  6. }
  7. }
复制代码
适配器模式(Adapter Pattern)

关键要点:将一个类的接口转换成客户盼望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
代码示例
  1. // 目标接口
  2. interface Target {
  3. void request();
  4. }
  5. // 适配者类
  6. class Adaptee {
  7. public void specificRequest() {
  8. System.out.println("适配者执行特定请求");
  9. }
  10. }
  11. // 适配器类
  12. class Adapter implements Target {
  13. private Adaptee adaptee;
  14. public Adapter(Adaptee adaptee) {
  15. this.adaptee = adaptee;
  16. }
  17. @Override
  18. public void request() {
  19. adaptee.specificRequest();
  20. }
  21. }
复制代码
利用示例
  1. public class AdapterPatternExample {
  2. public static void main(String[] args) {
  3. Adaptee adaptee = new Adaptee();
  4. Target target = new Adapter(adaptee);
  5. target.request();
  6. }
  7. }
复制代码
桥接模式(Bridge Pattern)

关键要点:将抽象部门与它的实现部门分离,使它们都可以独立地变化。它是一种对象布局型模式,通过将接口和实现分离,让它们可以独立扩展。
代码示例
  1. // 抽象化角色
  2. abstract class Abstraction {
  3. protected Implementor implementor;
  4. public Abstraction(Implementor implementor) {
  5. this.implementor = implementor;
  6. }
  7. public abstract void operation();
  8. }
  9. // 扩充抽象化角色
  10. class RefinedAbstraction extends Abstraction {
  11. public RefinedAbstraction(Implementor implementor) {
  12. super(implementor);
  13. }
  14. @Override
  15. public void operation() {
  16. implementor.operationImpl();
  17. }
  18. }
  19. // 实现化角色
  20. interface Implementor {
  21. void operationImpl();
  22. }
  23. // 具体实现化角色
  24. class ConcreteImplementorA implements Implementor {
  25. @Override
  26. public void operationImpl() {
  27. System.out.println("具体实现A的操作");
  28. }
  29. }
复制代码
利用示例
  1. public class BridgePatternExample {
  2. public static void main(String[] args) {
  3. Implementor implementor = new ConcreteImplementorA();
  4. Abstraction abstraction = new RefinedAbstraction(implementor);
  5. abstraction.operation();
  6. }
  7. }
复制代码
装饰器模式(Decorator Pattern)

关键要点:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为机动。
代码示例
  1. // 组件接口
  2. interface Component {
  3. void operation();
  4. }
  5. // 具体组件
  6. class ConcreteComponent implements Component {
  7. @Override
  8. public void operation() {
  9. System.out.println("具体组件执行操作");
  10. }
  11. }
  12. // 装饰器抽象类
  13. abstract class Decorator implements Component {
  14. protected Component component;
  15. public Decorator(Component component) {
  16. this.component = component;
  17. }
  18. @Override
  19. public void operation() {
  20. component.operation();
  21. }
  22. }
  23. // 具体装饰器
  24. class ConcreteDecoratorA extends Decorator {
  25. public ConcreteDecoratorA(Component component) {
  26. super(component);
  27. }
  28. @Override
  29. public void operation() {
  30. super.operation();
  31. System.out.println("具体装饰器A添加的额外操作");
  32. }
  33. }
复制代码
利用示例
  1. public class DecoratorPatternExample {
  2. public static void main(String[] args) {
  3. Component component = new ConcreteComponent();
  4. Component decoratedComponent = new ConcreteDecoratorA(component);
  5. decoratedComponent.operation();
  6. }
  7. }
复制代码
外观模式(Facade Pattern)

关键要点:为子体系中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子体系更加容易利用。
代码示例
  1. // 子系统类A
  2. class SubSystemA {
  3. public void operationA() {
  4. System.out.println("子系统A执行操作");
  5. }
  6. }
  7. // 子系统类B
  8. class SubSystemB {
  9. public void operationB() {
  10. System.out.println("子系统B执行操作");
  11. }
  12. }
  13. // 外观类
  14. class Facade {
  15. private SubSystemA subSystemA;
  16. private SubSystemB subSystemB;
  17. public Facade() {
  18. subSystemA = new SubSystemA();
  19. subSystemB = new SubSystemB();
  20. }
  21. public void operation() {
  22. subSystemA.operationA();
  23. subSystemB.operationB();
  24. }
  25. }
复制代码
利用示例
  1. public class FacadePatternExample {
  2. public static void main(String[] args) {
  3. Facade facade = new Facade();
  4. facade.operation();
  5. }
  6. }
复制代码
享元模式(Flyweight Pattern)

关键要点:运用共享技术有效地支持大量细粒度的对象。通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类的开销,从而进步体系资源的利用率。
代码示例
  1. // 享元工厂类
  2. class FlyweightFactory {
  3. private static final Map<String, Flyweight> flyweights = new HashMap<>();
  4. public static Flyweight getFlyweight(String key) {
  5. if (!flyweights.containsKey(key)) {
  6. flyweights.put(key, new ConcreteFlyweight(key));
  7. }
  8. return flyweights.get(key);
  9. }
  10. }
  11. // 享元接口
  12. interface Flyweight {
  13. void operation(String extrinsicState);
  14. }
  15. // 具体享元类
  16. class ConcreteFlyweight implements Flyweight {
  17. private final String intrinsicState;
  18. public ConcreteFlyweight(String intrinsicState) {
  19. this.intrinsicState = intrinsicState;
  20. }
  21. @Override
  22. public void operation(String extrinsicState) {
  23. System.out.println("内在状态:" + intrinsicState + ",外在状态:" + extrinsicState);
  24. }
  25. }
  26. 使用示例:
  27. public class FlyweightPatternExample {
  28. public static void main(String[] args) {
  29. Flyweight flyweight1 = FlyweightFactory.getFlyweight("key1");
  30. flyweight1.operation("extrinsic1");
  31. Flyweight flyweight2 = FlyweightFactory.getFlyweight("key1");
  32. flyweight2.operation("extrinsic2");
  33. }
  34. }
复制代码
组合模式(Composite Pattern)

关键要点:将对象组合成树形布局以表示 “部门 - 团体” 的条理布局。组合模式使得用户对单个对象和组合对象的利用具有一致性。
代码示例
  1. // 组件抽象类
  2. abstract class Component {
  3. protected String name;
  4. public Component(String name) {
  5. this.name = name;
  6. }
  7. public abstract void add(Component component);
  8. public abstract void remove(Component component);
  9. public abstract void display(int depth);
  10. }
  11. // 叶子节点类
  12. class Leaf extends Component {
  13. public Leaf(String name) {
  14. super(name);
  15. }
  16. @Override
  17. public void add(Component component) {
  18. System.out.println("叶子节点不能添加子节点");
  19. }
  20. @Override
  21. public void remove(Component component) {
  22. System.out.println("叶子节点不能移除子节点");
  23. }
  24. @Override
  25. public void display(int depth) {
  26. for (int i = 0; i < depth; i++) {
  27. System.out.print("-");
  28. }
  29. System.out.println("Leaf: " + name);
  30. }
  31. }
  32. // 复合节点类
  33. class Composite extends Component {
  34. private List<Component> children = new ArrayList<>();
  35. public Composite(String name) {
  36. super(name);
  37. }
  38. @Override
  39. public void add(Component component) {
  40. children.add(component);
  41. }
  42. @Override
  43. public void remove(Component component) {
  44. children.remove(component);
  45. }
  46. @Override
  47. public void display(int depth) {
  48. for (int i = 0; i < depth; i++) {
  49. System.out.print("-");
  50. }
  51. System.out.println("Composite: " + name);
  52. for (Component component : children) {
  53. component.display(depth + 2);
  54. }
  55. }
  56. }
复制代码
利用示例
  1. public class CompositePatternExample {
  2. public static void main(String[] args) {
  3. Composite root = new Composite("root");
  4. root.add(new Leaf("Leaf A"));
  5. root.add(new Leaf("Leaf B"));
  6. Composite comp = new Composite("Composite X");
  7. comp.add(new Leaf("Leaf XA"));
  8. comp.add(new Leaf("Leaf XB"));
  9. root.add(comp);
  10. root.display(1);
  11. }
  12. }
复制代码
总结

布局型模式为我们在软件计划中构建复杂布局提供了有力的工具。通过合理运用这些模式,可以进步代码的可维护性、可扩展性和可复用性。在实际项目中,根据具体的需求选择合适的布局型模式,能够让我们的代码更加优雅、高效。盼望这篇文章能资助你更好地明白和运用计划模式中的布局型模式。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4