在软件开发的天下里,计划模式是先辈们智慧的结晶,它们为我们提供了通用的解决方案来应对各种常见的软件计划问题。本日,我们深入探究计划模式中的布局型模式,并用 Java 语言来实现它们。
什么是布局型模式
布局型模式主要关注怎样将类或对象组合成更大的布局,以实现更复杂的功能。它就像是搭建积木,通过不同的组合方式,让简单的组件发挥出强盛的作用。布局型模式可以分为类布局型模式和对象布局型模式,前者主要通过继承机制来组合类,后者则通过对象的组合来实现。
常见的布局型模式
署理模式(Proxy Pattern)
关键要点:署理模式为其他对象提供一种署理以控制对这个对象的访问。当无法或不想直接访问某个对象时,署理对象可以代替目标对象举行工作。
代码示例:
- // 定义接口
- interface Subject {
- void request();
- }
- // 真实主题类
- class RealSubject implements Subject {
- @Override
- public void request() {
- System.out.println("真实主题执行请求");
- }
- }
- // 代理类
- class Proxy implements Subject {
- private RealSubject realSubject;
- public Proxy(RealSubject realSubject) {
- this.realSubject = realSubject;
- }
- @Override
- public void request() {
- // 可以在调用真实主题前后进行额外操作
- System.out.println("代理预处理");
- realSubject.request();
- System.out.println("代理后处理");
- }
- }
复制代码 利用示例:
- public class ProxyPatternExample {
- public static void main(String[] args) {
- RealSubject realSubject = new RealSubject();
- Proxy proxy = new Proxy(realSubject);
- proxy.request();
- }
- }
复制代码 适配器模式(Adapter Pattern)
关键要点:将一个类的接口转换成客户盼望的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
代码示例:
- // 目标接口
- interface Target {
- void request();
- }
- // 适配者类
- class Adaptee {
- public void specificRequest() {
- System.out.println("适配者执行特定请求");
- }
- }
- // 适配器类
- class Adapter implements Target {
- private Adaptee adaptee;
- public Adapter(Adaptee adaptee) {
- this.adaptee = adaptee;
- }
- @Override
- public void request() {
- adaptee.specificRequest();
- }
- }
复制代码 利用示例:
- public class AdapterPatternExample {
- public static void main(String[] args) {
- Adaptee adaptee = new Adaptee();
- Target target = new Adapter(adaptee);
- target.request();
- }
- }
复制代码 桥接模式(Bridge Pattern)
关键要点:将抽象部门与它的实现部门分离,使它们都可以独立地变化。它是一种对象布局型模式,通过将接口和实现分离,让它们可以独立扩展。
代码示例:
- // 抽象化角色
- abstract class Abstraction {
- protected Implementor implementor;
- public Abstraction(Implementor implementor) {
- this.implementor = implementor;
- }
- public abstract void operation();
- }
- // 扩充抽象化角色
- class RefinedAbstraction extends Abstraction {
- public RefinedAbstraction(Implementor implementor) {
- super(implementor);
- }
- @Override
- public void operation() {
- implementor.operationImpl();
- }
- }
- // 实现化角色
- interface Implementor {
- void operationImpl();
- }
- // 具体实现化角色
- class ConcreteImplementorA implements Implementor {
- @Override
- public void operationImpl() {
- System.out.println("具体实现A的操作");
- }
- }
复制代码 利用示例:
- public class BridgePatternExample {
- public static void main(String[] args) {
- Implementor implementor = new ConcreteImplementorA();
- Abstraction abstraction = new RefinedAbstraction(implementor);
- abstraction.operation();
- }
- }
复制代码 装饰器模式(Decorator Pattern)
关键要点:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为机动。
代码示例:
- // 组件接口
- interface Component {
- void operation();
- }
- // 具体组件
- class ConcreteComponent implements Component {
- @Override
- public void operation() {
- System.out.println("具体组件执行操作");
- }
- }
- // 装饰器抽象类
- abstract class Decorator implements Component {
- protected Component component;
- public Decorator(Component component) {
- this.component = component;
- }
- @Override
- public void operation() {
- component.operation();
- }
- }
- // 具体装饰器
- class ConcreteDecoratorA extends Decorator {
- public ConcreteDecoratorA(Component component) {
- super(component);
- }
- @Override
- public void operation() {
- super.operation();
- System.out.println("具体装饰器A添加的额外操作");
- }
- }
复制代码 利用示例:
- public class DecoratorPatternExample {
- public static void main(String[] args) {
- Component component = new ConcreteComponent();
- Component decoratedComponent = new ConcreteDecoratorA(component);
- decoratedComponent.operation();
- }
- }
复制代码 外观模式(Facade Pattern)
关键要点:为子体系中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子体系更加容易利用。
代码示例:
- // 子系统类A
- class SubSystemA {
- public void operationA() {
- System.out.println("子系统A执行操作");
- }
- }
- // 子系统类B
- class SubSystemB {
- public void operationB() {
- System.out.println("子系统B执行操作");
- }
- }
- // 外观类
- class Facade {
- private SubSystemA subSystemA;
- private SubSystemB subSystemB;
- public Facade() {
- subSystemA = new SubSystemA();
- subSystemB = new SubSystemB();
- }
- public void operation() {
- subSystemA.operationA();
- subSystemB.operationB();
- }
- }
复制代码 利用示例:
- public class FacadePatternExample {
- public static void main(String[] args) {
- Facade facade = new Facade();
- facade.operation();
- }
- }
复制代码 享元模式(Flyweight Pattern)
关键要点:运用共享技术有效地支持大量细粒度的对象。通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类的开销,从而进步体系资源的利用率。
代码示例:
- // 享元工厂类
- class FlyweightFactory {
- private static final Map<String, Flyweight> flyweights = new HashMap<>();
- public static Flyweight getFlyweight(String key) {
- if (!flyweights.containsKey(key)) {
- flyweights.put(key, new ConcreteFlyweight(key));
- }
- return flyweights.get(key);
- }
- }
- // 享元接口
- interface Flyweight {
- void operation(String extrinsicState);
- }
- // 具体享元类
- class ConcreteFlyweight implements Flyweight {
- private final String intrinsicState;
- public ConcreteFlyweight(String intrinsicState) {
- this.intrinsicState = intrinsicState;
- }
- @Override
- public void operation(String extrinsicState) {
- System.out.println("内在状态:" + intrinsicState + ",外在状态:" + extrinsicState);
- }
- }
- 使用示例:
- public class FlyweightPatternExample {
- public static void main(String[] args) {
- Flyweight flyweight1 = FlyweightFactory.getFlyweight("key1");
- flyweight1.operation("extrinsic1");
- Flyweight flyweight2 = FlyweightFactory.getFlyweight("key1");
- flyweight2.operation("extrinsic2");
- }
- }
复制代码 组合模式(Composite Pattern)
关键要点:将对象组合成树形布局以表示 “部门 - 团体” 的条理布局。组合模式使得用户对单个对象和组合对象的利用具有一致性。
代码示例:
利用示例:
- public class CompositePatternExample {
- public static void main(String[] args) {
- Composite root = new Composite("root");
- root.add(new Leaf("Leaf A"));
- root.add(new Leaf("Leaf B"));
- Composite comp = new Composite("Composite X");
- comp.add(new Leaf("Leaf XA"));
- comp.add(new Leaf("Leaf XB"));
- root.add(comp);
- root.display(1);
- }
- }
复制代码 总结
布局型模式为我们在软件计划中构建复杂布局提供了有力的工具。通过合理运用这些模式,可以进步代码的可维护性、可扩展性和可复用性。在实际项目中,根据具体的需求选择合适的布局型模式,能够让我们的代码更加优雅、高效。盼望这篇文章能资助你更好地明白和运用计划模式中的布局型模式。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |