zoukankan      html  css  js  c++  java
  • 装饰模式


    问题由来

       系统的复用机制的不合理。

       可以看出该结构图采用的是继承复用结构,这样的话如果增加新功能,只能通过复用父类的方法来增加新的功能来扩展功能。


    合成复用原则:在实现功能的复用时,要多用关联,少用继承。
    里氏代换原则:所有引用基类对象的地方都能够透明的引用其子类的对象。

    装饰模式

       动态地给一个对象增加一些额外的的职责,就增加对象功能来说,装饰模式比生产子类实现更灵活。

    结构类图:



       Component:抽象构件:声明了基本的方法,是具体构件和抽象装饰者的共同父类。
       ConcreateComponent:具体构件:具体的构件,装饰者类可以给它动态的增加功能。
       Decorator:抽象装饰类:维护一个指向抽象组件的属性,通过该引用调用具体构件的方法,通过子类可以实现扩展功能。
       ConcreateDecorator:具体装饰类,实现某个具体功能的装饰。

             在程序运行时,通过向具体的装饰类注入具体的Component子类就可以实现功能的扩展。在这个过程中可以使用完全针对抽象层编程,这样可以使客户端对装饰过程透明,可以一致的使用这些对象。

    代码

    构件类:

    1
    2
    3
    public abstract class {
    public abstract void display();
    }

    1
    2
    3
    4
    5
    6
    public class ListBox extends {
    public void display() {
    System.out.println("显示列表");
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    public class TextBox extends {
    public void display() {
    System.out.println("显示文本框");
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    public class Window extends {
    public void display() {
    System.out.println("显示窗口");
    }
    }



    装饰类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public abstract class ComponentDecorator extends {
    private Component 大专栏  装饰模式 component;
    public ComponentDecorator(Component component)
    {
    this.component=component;
    }
    public void display(){
    this.component.display();
    }
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class SrcollBarDecorator extends ComponentDecorator {
    public SrcollBarDecorator(Component component) {
    super(component);
    }
    public void display() {
    super.display();
    setSrcollBar();
    }
    public void setSrcollBar()
    {
    System.out.println("增加滚动条");
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class BlackBorderDecorator extends ComponentDecorator {
    public BlackBorderDecorator(Component component) {
    super(component);
    }
    public void display() {
    super.display();
    setBlackBorder();
    }
    public void setBlackBorder()
    {
    System.out.println("增加黑色边框");
    }
    }



    测试:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class test {
    public static void main(String[] args) {
    Component component,componentSB,componentBB;
    component=new Window();
    componentSB=new SrcollBarDecorator(component);
    componentBB=new BlackBorderDecorator(componentSB);
    componentBB.display();
    }
    }

  • 相关阅读:
    .NE 学习概要
    (转)工作之路---记录LZ如何在两年半的时间内升为PM
    XP下Winform背景透明问题
    CSE(Corrupted State Exceptions) 严重异常处理办法
    (转)C#模拟键盘鼠标事件
    (转).net项目技术选型总结
    (转)MSMQ续
    (转)MSMQ(消息队列)
    (转)TCP三次握手
    Socket编程初探
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12256091.html
Copyright © 2011-2022 走看看