Java_23种设计模式(三)----- 结构型模式

来源:互联网 发布:淘宝刷收藏的软件 编辑:程序博客网 时间:2024/06/06 16:28
/* * 结构型模式: 7种 * 1、 适配器模式: 类适配器;对象适配器;接口适配器 * 2、 装饰器模式 * 3、 代理模式 * 4、 外观模式 * 5、 桥接模式 * 6、 组合模式 * 7、 享元模式 */public class ConstructModel {    // 1. 适配器模式    Targetable t = new ClassAdapter(); // 类适配器    t.method1();    t.method2();    Targetable t1 = new ObjectAdapter(new Source()); // 对象适配器    t1.method1();    t1.method2();    Souceable t2 = new SourceSub1(); // 接口适配器    t2.method2();    // 2. 装饰器模式    DecoratorSourceable d1 = new Decorator(new DecoratorSource());    d1.method1;    // 3. 代理模式    ProxySourceable p1 = new Proxy();    p1.method1();    // 4. 外观模式    ComputerFacade c = new ComputerFacade();    c.startup();    // 5. 桥接模式     Bridge b = new SingleBridge();    b.setSource(new Source1());    b.method();    b.setSource(new Source2());    b.method();    // 6. 组合模式    Tree tree = new Tree ("A");    CompositeTree cTree1 = new CompositeTree("B");    CompositeTree cTree2 = new CompositeTree("C");    cTree1.add(cTree2);    tree.cTree.add(cTree1);    // 7. 享元模式    FlyWeightFactory pool = new FlyWeightFactory(10);    FlyWeight f = pool.getFlyWeight();}/* * 享元模式 ------ 目的是实现对象的共享,即共享池。当系统中的对象多的时候,可以减少内存的开销,通常与工厂模式一起使用 */ class FlyWeightFactory {    private Vector<FlyWeight> pool;    public FlyWeightFactory ( int poolSize) {        pool = new Vectory<FlyWeight>(poolSize);    }    public FlyWeight getFlyWeight (){        if ( pool.size() > 0 ) {            FlyWeight f = pool.get(0);            pool.remove();            return f;        }        return null;    }}class FlyWeight2 implements FlyWeight {    public void method () {System.out.println("FlyWeight2 -> method");}class FlyWeight1 implements FlyWeight {    public void method () {System.our.println("FlyWeight1 -> method");}}interface FlyWeight {    void method();}/* * 组合模式------又叫部分-整体模式, 在处理类似树形结构的问题时比较方便 * 使用场景: 用于表示树形结构,例如 二叉树、数等 */class Tree {    CompositeTree cTree = null;    public Tree(String name ) {        cTree = new CompositeTree(name);    }}class CompositeTree {    private String name;    public CompositeTree (String name) {this.name = name;}    private CompositeTree parent;    private Vector<CompositeTree> children = new Vector<CompositeTree>();    public void add(CompositeTree tree ) {        children.add(tree);    }}/* * 桥接模式 --- 将抽象化和实现化解耦,使得二者可以独立变化 * 抽象(桥)与实现分不同的实现方。  一方负责制定抽象化方法,另一方负责编写实现类 * 一般是通过set****(具体实现类) */class SingleBridge extends Bridge {    public void method (){        getSource().method();    }}abstract class Bridge {    private BridgeSourceable source;    public void setSource(BridgeSourceable source) {this.source = source;}    public BridgeSourceable getSource() {return this.source;}    public void method() {source.method();}} class Source2 implements BridgeSourceable {    public void method () {System.out.println("Source2 -> method");}}class Source1 implements BridgeSourceable {    public void method () {System.out.println("Source1 -> method");}} interface BridgeSourceable {    void method();}/* * 外观模式 --- 解决类与类之间的依赖关系,将关系放在一个Facade类中,降低类与类之间的耦合度 * ----类似于spring一样,可以将类与类之间的关系配置到配置文件中 *  该模式中没有涉及到接口 */class ComputerFacade {    private Memory m;    private CPU c;    public ComputerFacade() {        m = new Memory();        c = new CPU();    }    public void startup () {m.startup(); c.startup();}}class Memory {    public void startup (){System.out.println ("Memory -> startup");}}class CPU {    public void startup () {System.out.println("CPU -> startup");}}/* * 代理模式 * 场景: 需要对原有的方法进行改进 * 方法1: 修改原有的方法。这样违反了"开闭原则" * 方法2: 采用一个代理类 调用原有的方法,且对产生的结果进行控制 */class Proxy implements ProxySourceable {    private ProxySourceable source;    public Proxy() {        source = new ProxySource();    }    public void method1 () {         System.out.println("Proxy -> method1");        source.method1();    }}class ProxySource implements ProxySourceable {    public void method1() {System.out.println("ProxySource -> method1");}}interface ProxySourceable {    void method1();}/* * 装饰器模式 ------ 给一个对象增加一些新的功能,而且是动态的 * 条件1: 装饰对象和被装饰对象实现同一个接口 * 条件2: 装饰对象持有被装饰对象的实例 * 优点: 动态的为一个对象增加功能,而且还能动态撤销(继承不能达到这一点,继承的功能是静态的,不能动态增删) * 缺点: 产生过多相同的对象,不易排错 */class Decorator implements DecoratorSourceable {    private DecoratorSourceable source;    public Decorator (DecoratorSourceable source ) { this.source = source;}    public void method() {        System.out.println("Decorator -> method");        source.method();    }}class DecoratorSource implements DecoratorSourceable {    public void method() {System.out.println("DecoratorSource -> method");}} interface DecoratorSourceable {    void method();}/* * 接口适配器 ----- 待适配的对象为接口。对接口做初次实现,变成抽象类 * ------------- 之后实际类继承抽象类,实现方法 */clas SourceSub1 extends InterfAdapter {    public void method1 () {System.out.println("SourceSub1 -> method1");}}abstract class InterfAdapter implements Souceable {    public void method1(){}    public void method2() {} }interface Sourceable {    void method1();    void method2();}/* * 对象适配器 ------- 将对象引入到适配器中 将一个对象转换成满足另一个新接口的对象 * 条件1: 对象作为成员变量 * 条件2: 构造时,采用父类引用指向子类对象方式 */class ObjectAdapter implements Targetable {    private Source s;    public ObjectAdapter(Source s ){this.s = s;}    public void method1 () {s.method1();}    public void method2 () {}}/* * 类适配器 ----------继承类 * -----将一个类转换成满足另一个新接口类 */class ClassAdapter extends Source implements Targetable {    // method1 已经在source里面实现了    public void method2 (){};}interface Targetable {    void method1();    void method2();}class Source {    public void method1() {System.out.println("Source -> method1");}}
原创粉丝点击