`

接口型模式

 
阅读更多
1.Adapter(适配器)模式

对象适 配器(Object Adapter)

public interface IPeg {
	void insertIntoHole();
}
public class SquarePeg implements IPeg {
	@Override
	public void insertIntoHole() {
		System.out.println("I'm inserting into square hole...");
		// other logic...
	}
}
public interface IRoundPeg {
	void insertIntoRoundHole();
}
public class RoundPeg implements IRoundPeg {
	@Override
	public void insertIntoRoundHole() {
		System.out.println("I'm inserting into round hole...");
		// other logic...
	}
}
public class RoundPegAdapter implements IPeg {
	private IRoundPeg roundPeg;

	public RoundPegAdapter(IRoundPeg roundPeg) {
		this.roundPeg = roundPeg;
	}

	@Override
	public void insertIntoHole() {
		roundPeg.insertIntoRoundHole();
	}
}

类适配器


public class RoundPegAdapter2 extends RoundPeg implements IPeg {
	@Override
	public void insertIntoHole() {
		super.insertIntoRoundHole();
	}
}


双向适配器


public class TwoWayPegAdapter implements IRoundPeg, IPeg {
	private IPeg squarePeg;
	private IRoundPeg roundPeg;

	public TwoWayPegAdapter(IPeg squarePeg) {
		this.squarePeg = squarePeg;
	}

	public TwoWayPegAdapter(IRoundPeg roundPeg) {
		this.roundPeg = roundPeg;
	}

	@Override
	public void insertIntoRoundHole() {
		squarePeg.insertIntoHole();
	}

	@Override
	public void insertIntoHole() {
		roundPeg.insertIntoRoundHole();
	}
}

测试
public class TestDrive {
	public static void main(String[] args) {
		TestDrive test = new TestDrive();
		IPeg squarePeg = new SquarePeg();
		IRoundPeg roundPeg = new RoundPeg();
		RoundPegAdapter adpater = new RoundPegAdapter(roundPeg);
		System.out.println("Testing square peg...");
		test.testPeg(squarePeg);
		System.out.println("\nTesting square adapter peg...");
		test.testPeg(adpater);
		TwoWayPegAdapter roundPeg2 = new TwoWayPegAdapter(roundPeg);
		TwoWayPegAdapter squarePeg2 = new TwoWayPegAdapter(squarePeg);
		System.out.println("\nTesting a 2-way square adapter peg...");
		test.testPeg(roundPeg2);
		System.out.println("\nTesting 2-way round adapter peg...");
		test.testRoundPeg(squarePeg2);
	}

	private void testPeg(IPeg peg) {
		peg.insertIntoHole();
	}

	private void testRoundPeg(IRoundPeg peg) {
		peg.insertIntoRoundHole();
	}
}

2.Facade(外观)模式

目的:提供一个接口,使子系统更加容易使用。

通常,我们应该把子系统中的类重构为一个个目的明确的类。这样做可以使代码更加容易维护,但是这样也会让子系统用户不知从何处开始。为了便于子系统用户的使用,我们可以在子系统中顺带提供一些示例类或者外观类。示例类通常是指能够独立运行但不可复用的应用程序,仅用来示范子系统的用法。外观类通常是一个可配置、可复用的类,它为方便用户使用子系统提供了一个更高层次的接口。

总结:外观封装了子系统之间复杂的交互和依赖关系,为客户对象提供了单一简单的界面,降低了系统的复杂性。在讲解外观模式同时,介绍了 最少知识原则,我们知道,类之间耦合度越低,越易扩展和实现重用。
public class HotelReceptionist {
    public void subscribe() {
        System.out.println("Subscribe a table...");
    }
}
public class Cook {
    public void cookDish() {
        System.out.println("Cooking dishes...");
    }
}
public class Waitress {
    public void serveDishes() {
        System.out.println("Serving dishes...");
    }

    public void waitForAnOrder() {
        System.out.println("Waiting for the order...");
    }
}
public class Cashier {
    public void check() {
        System.out.println("Check the bill...");
    }
}
public class Assistant {
    private HotelReceptionist hotelReceptionist;
    private Cook cook;
    private Waitress waitress;
    private Cashier cashier;

    public Assistant(HotelReceptionist hotelReceptionist, Cook cook, Waitress waitress, Cashier cashier) {
        this.hotelReceptionist = hotelReceptionist;
        this.cook = cook;
        this.waitress = waitress;
        this.cashier = cashier;
    }

    public void prepareDinner() {
        hotelReceptionist.subscribe();
        waitress.waitForAnOrder();
        cook.cookDish();
    }

    public void endDinner() {
        waitress.serveDishes();
        cashier.check();
    }
}
public class Boss {
    private Assistant assistant;

    public Boss(Assistant assistant) {
        this.assistant = assistant;
    }

    public void treat() {
        assistant.prepareDinner();
        address();
        assistant.endDinner();
    }

    private void address() {
        System.out.println("Boss is bitching : \"Tomorrow is going to be better, we will make blah blah...\"");
    }
}
public class FacadeTestDrive {
    public static void main(String[] args) {
       Cashier cashier = new Cashier();
       Cook cook = new Cook();
       HotelReceptionist hotelReceptionist = new HotelReceptionist();
       Waitress waitress = new Waitress();
       Assistant assistant = new Assistant(hotelReceptionist, cook, waitress, cashier);

       Boss boss = new Boss(assistant);

       boss.treat();
   }
}


3.Composite(组合)模式

目的:让用户能够用统一的接口处理单个对象以及对象组合。


常见的组合

   Composite模式包含两个相关的重要概念。其中一个概念是群组可以包含个体对象,也可以包含其他的群组。与此相关的另一个概念是群组和个体对象可以共享同一个接口。将这些概念应用于对象建模,就可以创建一个抽象类或Java接口来定义群组对象和个体对象的公有特性。

     组合对象模式中的方法通常采用递归定义。由于递归的存在,在编写代码的时候需要谨防死循环。不过,只要通过采取措施保证组合对象模型为树状结构,就可一避免这类问题。另外,组合对象模型中也可以出现环,但是我们必须修改算法来监控可能出现的无穷尽递归。

安全的组合模式

public abstract class BranchComponent {
    public String getName() {
        throw new UnsupportedOperationException();
    }

    public String getDiscription() {
        throw new UnsupportedOperationException();
    }

    public void display() {
        throw new UnsupportedOperationException();
    }
}
import java.util.ArrayList;
import java.util.List;
public class BranchComposite extends BranchComponent {
    private String name;
    private String discription;
    private List<BranchComponent> childrenBranch;

    public BranchComposite(String name, String discription) {
        this.name = name;
        this.discription = discription;
        childrenBranch = new ArrayList<BranchComponent>();
    }

    public void display() {
        System.out.printf("%s: %s\n", name, discription);
        for (BranchComponent child : childrenBranch) {
            child.display();
        }
    }

    public String getName() {
        return name;
    }

    public String getDiscription() {
        return discription;
    }

    public void add(BranchComponent child) {
        childrenBranch.add(child);
    }

    public void remove(BranchComponent child) {
        childrenBranch.remove(child);
    }

    public BranchComponent getChild(int index) {
        return childrenBranch.get(index);
    }
}
public class BranchLeaf extends BranchComponent {
    private String name;
    private String discription;

    public BranchLeaf(String name, String discription) {
        this.name = name;
        this.discription = discription;
    }

    public void display() {
        System.out.printf("\t%s: %s\n", name, discription);
    }

    public String getName() {
        return name;
    }

    public String getDiscription() {
        return discription;
    }
}
public class TestDrive {
    public static void main(String[] args) {
        BranchComposite china = new BranchComposite("CN", "China Branch");

        BranchComposite shanghai = new BranchComposite("Sh", "Shanghai Branch");
        BranchLeaf huangpu = new BranchLeaf("Hp", "Huangpu Branch");
        BranchLeaf yangpu = new BranchLeaf("Yp", "Yangpu Branch");
        BranchLeaf pudong = new BranchLeaf("Pd", "Pudong Branch");

        BranchComposite beijing = new BranchComposite("Bj", "Beijing Branch");
        BranchLeaf dongcheng = new BranchLeaf("Dc", "Dongcheng Branch");
        BranchLeaf xicheng = new BranchLeaf("Xc", "Xicheng Branch");
        BranchLeaf haidian = new BranchLeaf("Hd", "Haidian Branch");

        shanghai.add(huangpu);
        shanghai.add(yangpu);
        shanghai.add(pudong);

        beijing.add(dongcheng);
        beijing.add(xicheng);
        beijing.add(haidian);

        china.add(shanghai);
        china.add(beijing);

        System.out.println("Displaying the head bank information");
        display(china);

        System.out.println("\nDisplaying Shanghai bank branch information");
        display(shanghai);

        System.out.println("\nDisplaying Pudong bank branch information in Shanghai");
        display(pudong);
    }

    private static void display(BranchComponent branch) {
        branch.display();
    }
}


透明的组合模式


public abstract class BranchComponent {
    public String getName() {
        throw new UnsupportedOperationException();
    }

    public String getDiscription() {
        throw new UnsupportedOperationException();
    }

    public void display() {
        throw new UnsupportedOperationException();
    }

    public void add(BranchComponent child) {
        throw new UnsupportedOperationException();
    }

    public void remove(BranchComponent child) {
        throw new UnsupportedOperationException();
    }

    public BranchComponent getChild(int index) {
        throw new UnsupportedOperationException();
    }
}
import java.util.ArrayList;
import java.util.List;
public class BranchComposite extends BranchComponent {
    private String name;
    private String discription;
    private List<BranchComponent> childrenBranch;

    public BranchComposite(String name, String discription) {
        this.name = name;
        this.discription = discription;
        childrenBranch = new ArrayList<BranchComponent>();
    }

    public void display() {
        System.out.printf("%s: %s\n", name, discription);
        for (BranchComponent child : childrenBranch) {
            child.display();
        }
    }

    public String getName() {
        return name;
    }

    public String getDiscription() {
        return discription;
    }

    public void add(BranchComponent child) {
        childrenBranch.add(child);
    }

    public void remove(BranchComponent child) {
        childrenBranch.remove(child);
    }

    public BranchComponent getChild(int index) {
        return childrenBranch.get(index);
    }
}
public class BranchLeaf extends BranchComponent {
    private String name;
    private String discription;

    public BranchLeaf(String name, String discription) {
        this.name = name;
        this.discription = discription;
    }

    public void display() {
        System.out.printf("\t%s: %s\n", name, discription);
    }

    public String getName() {
        return name;
    }

    public String getDiscription() {
        return discription;
    }
}
public class TestDrive {
    public static void main(String[] args) {
        BranchComposite china = new BranchComposite("CN", "China Branch");

        BranchComposite shanghai = new BranchComposite("Sh", "Shanghai Branch");
        BranchLeaf huangpu = new BranchLeaf("Hp", "Huangpu Branch");
        BranchLeaf yangpu = new BranchLeaf("Yp", "Yangpu Branch");
        BranchLeaf pudong = new BranchLeaf("Pd", "Pudong Branch");

        BranchComposite beijing = new BranchComposite("Bj", "Beijing Branch");
        BranchLeaf dongcheng = new BranchLeaf("Dc", "Dongcheng Branch");
        BranchLeaf xicheng = new BranchLeaf("Xc", "Xicheng Branch");
        BranchLeaf haidian = new BranchLeaf("Hd", "Haidian Branch");

        shanghai.add(huangpu);
        shanghai.add(yangpu);
        shanghai.add(pudong);

        beijing.add(dongcheng);
        beijing.add(xicheng);
        beijing.add(haidian);

        china.add(shanghai);
        china.add(beijing);

        System.out.println("Displaying the head bank information");
        display(china);

        System.out.println("\nDisplaying Shanghai bank branch information");
        display(shanghai);

        System.out.println("\nDisplaying Pudong bank branch information in Shanghai");
        display(pudong);
    }

    private static void display(BranchComponent branch) {
        branch.display();
    }
}


4.Bridge(桥接)模式

意图:将抽象与抽象方法的实现相分离,这样他们就可以独立变化。

    抽象就是指依赖于一系列抽象方法的类。最简单的抽象实例是抽象的类层次结构,其中超类中的具体类依赖于其他抽象类。如果希望用另一种机器的排列方式来构造最初的类层次结构,就必须把那些抽象的方法移到另一种类层次结构中。在这种情况下,我们可以使用Bridge模式,将抽象与其抽象方法的实现相分离。
    Bridge模式是常见的例子就是驱动程序,比如数据库驱动程序。数据库驱动程序提供了Bridge模式结构中固有的权衡的良好实例。一个驱动程序可能会请求某个实现程序不支持的方法。另一方面,驱动程序可能会忽略应用到某个特定数据库的有用方法。这将迫使我们重新编写针对实现而不是抽象的代码。我们是否应该更重视抽象而不是具体并非一直都很明朗,但是有意地做这些决定是非常重要的.

  • 大小: 32.3 KB
  • 大小: 30.6 KB
  • 大小: 43 KB
  • 大小: 60.2 KB
  • 大小: 86.9 KB
分享到:
评论

相关推荐

    DHCP配置全局模式与接口模式

    DHCP配置全局模式与接口模式 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一种应用层协议,用于将IP地址和其他相关参数分配给网络设备。DHCP配置模式有两种:全局模式和接口模式。本文将详细...

    设计模式PPT

    - **接口型模式**:如外观模式、适配器模式、组合模式和桥接模式,它们关注对象间的接口集成和通信。 - **责任型模式**:如单例模式、观察者模式、中介者模式、代理模式和职责链模式,这些模式主要处理对象间的职责...

    设计模式1,FACADE模式,Adapter

    首先,FACADE(外观)模式是一种接口型模式,它的主要作用是为复杂的子系统提供一个简单的接口,使得客户端无需了解子系统内部的复杂性。通过创建一个外观类,对外暴露统一的操作,隐藏子系统组件间的交互细节。这样...

    23种设计模式,创建型模式共5种,结构型模式7种,行为型模式11种

    设计模式分为三大类:创建型模式、结构型模式和行为型模式。 **创建型模式**关注的是对象的创建。共有五种创建型模式: 1. **工厂方法模式**:它定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法...

    设计模式,软件开发

    #### 接口型模式——适配器模式解析 设计模式是在软件工程领域内,针对特定问题的一种通用解决方案。它们提供了一种标准化的方式来处理常见的编程难题,使代码更加灵活、可重用和易于理解。在众多设计模式中,...

    java 设计模式

    ### Java设计模式之接口型模式总结 #### 一、引言 在软件开发过程中,设计模式作为一套被广泛接受的解决方案,为解决常见问题提供了模板。本文将深入探讨Java设计模式中的四大接口型模式——适配器模式(Adapter)、...

    Python 程序语言设计模式思路-结构型模式:适配器模式-将不兼容的接口转换为可兼容的接口

    适配器模式(Adapter Pattern)是一种结构型设计模式,旨在将一个类的接口转换为客户端期望的另一个接口,从而使原本由于接口不兼容而无法一起工作的类能够协同工作。适配器模式通过引入一个适配器类,解决了接口不...

    设计模式之结构型模式

    在本文中,我们将深入探讨结构型设计模式,特别是桥接模式、适配器模式、装饰者模式和组合模式,以及它们在实际场景中的应用。 1. **桥接模式**: 桥接模式将抽象部分与实现部分分离,使得它们可以独立进行变化。...

    S7-200 SMART如何将485接口由自由口模式切换成PPI模式?.docx

    老款的S7-200系列PLC可以通过拨码开关切换到STOP模式来恢复PPI模式,但S7-200 SMART的紧凑型CRs CPU(如CR20s、CR30s、CR40s和CR60s)没有这样的硬件开关,且没有以太网接口,因此切换方式有所不同。 将485接口从...

    行为型模式+结构型模式+创建型模式:三大设计模式实例剖析与深入解读

    在给定的标题和描述中,我们关注的是三种主要的设计模式类别:行为型模式、结构型模式和创建型模式。下面将分别对这三大类模式进行详细阐述,并通过实例剖析它们的核心概念和应用场景。 **行为型模式**主要关注对象...

    (结构型模式)Bridge模式

    Bridge模式是一种设计模式,属于结构型模式之一,其主要目的是将抽象部分与实现部分分离,使得两者可以独立地进行变化。这种模式的核心思想是“抽象不应该依赖于具体,而应该依赖于抽象”。Bridge模式通过引入一个...

    设计模式之结构型模式uml类图EA文件.rar

    在给定的压缩包文件中,我们关注的是结构型设计模式,这些模式主要用于处理类和对象的组合与结构,以实现更灵活、可扩展的设计。下面我们将详细探讨其中涉及到的几个模式:桥接模式、适配器模式、装饰者模式和组合...

    结构型模式的几个模式

    根据它们的功能,这些模式通常被分为三类:创建型模式、结构型模式和行为型模式。本篇文章将重点讨论结构型模式中的几种关键类型:外观模式、代理模式、适配器模式以及桥接模式。 #### 二、结构型模式简介 结构型...

    Java设计模式之结构型模式源码以及文档

    今天我们要探讨的是“Java设计模式之结构型模式”。结构型模式主要关注如何组织类和对象,以达到良好的架构,提升代码的可读性和可维护性。在这个主题中,我们将深入理解并讨论这些模式的原理、应用场景及其实现。 ...

    JAVA设计模式,常用创建型、结构型、行为型模式介绍和代码示例

    ### JAVA设计模式详解:创建型、结构型、行为型模式介绍及代码示例 #### 一、概述 在软件开发过程中,设计模式提供了一系列解决常见问题的模板,帮助开发者更高效地编写高质量代码。设计模式通常被划分为三种类型...

    C#面向对象设计模式纵横谈-创建型模式

    虽然不是严格意义上的创建型模式,但组合模式与创建型模式密切相关,因为它允许我们构建树形结构的对象。它将简单对象和组合对象以一致的方式处理,使客户端代码可以透明地操作单个元素或整个对象结构。 以上创建...

    C#面向对象设计模式纵横谈(9):Composite 组合模式(结构型模式)

    标题中的“C#面向对象设计模式纵横谈(9):Composite组合模式(结构型模式)”明确了文章的主题聚焦于C#语言环境下的设计模式探讨,具体到第9篇讨论的是“Composite组合模式”。这一模式属于结构型模式的一种,旨在解决...

    Bridge 桥接模式(结构型模式)

    视频资源“7.Bridge 桥接模式(结构型模式).wmv”可能涵盖了以下内容: 1. 桥接模式的概念解释和基本结构。 2. 桥接模式的UML类图展示,解释各个角色之间的关系。 3. 实例演示,如图形界面库的设计,其中颜色和形状是...

Global site tag (gtag.js) - Google Analytics