zoukankan      html  css  js  c++  java
  • [设计模式]结构型设计模式

    简介

    结构型设计模式:关注类或者类与对象之间的组合关系,以构建更加复杂的系统

    代码

    1. 适配器模式
      * 类适配器
       package me.maxiaolong.designpattern;
      
       /**
        * @author maxiaolong
        * created at 2020/7/4
        * 结构型设计模式:关注类或类与对象之间的组合以构成更加复杂的系统
        * 1. 类结构型设计模式:关注类之间的组合,类之间的组合一般只存在继承关系和实现关系
        * 2. 对象结构型设计模式:关注类与对象之间的组合,类与对象之间的关联可以使用组合和聚合,满足合成复用原则(大部分结构型设计模式为对象结构型设计模式)
        *
        * 适配器设计模式(Adapter/Wrapper)主要是将一个类的接口转换为用户希望的另一接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作
        */
       
       public class ClassAdapterClient {
           public static void main(String[] args) {
               Target target = new Adapter();
               target.request();
           }
       }
       
       class Adaptee{
           public void specialRequest(){
               System.out.println("special request");
           }
       }
       
       interface Target{
           /**
            * 客户端目标类所期望的请求方法
            */
           void request();
       }
       
       class Adapter extends Adaptee implements Target{
       
           @Override
           public void request() {
               specialRequest();
           }
       }
      
    * 对象适配器
      ```java
      package me.maxiaolong.designpattern;
    
      /**
       * @author maxiaolong
       * created at 2020/7/4
       */
      public class ObjectAdapterClient {
          public static void main(String[] args) {
              Target1 target1 = new Adapter1();
              target1.request();
          }
      }
      
      abstract class Target1{
          public abstract void request();
      }
      
      class Adaptee1{
          public void specialRequest(){
              System.out.println("special request");
          }
      }
      
      class Adapter1 extends Target1{
          private Adaptee1 adaptee1 = new Adaptee1();
      
          @Override
          public void request() {
              adaptee1.specialRequest();
          }
      }
      ```
    
    1. 装饰模式
    ```java
    package me.maxiaolong.designpattern;
    
    /**
     * @author maxiaolong
     * created at 2020/7/4
     * 装饰模式是一种替换继承的技术,他通过一种无序定义子类的方式给对象动态增加职责,使用对象之间的关联关系取代类之间的继承关系(聚合)
     */
    public class DecoratorClient {
        public static void main(String[] args) {
            //创建对象
            Component concreteComponent = new ConcreteComponent();
            Decorator concreteDecorator = new ConcreteDecorator2(concreteComponent);
            //same method, add additional method
            concreteDecorator.operation();
        }
    }
    
    abstract class Component{
        public abstract void operation();
    }
    
    class ConcreteComponent extends Component{
    
        @Override
        public void operation() {
            System.out.println("concrete component operation");
        }
    }
    
    class Decorator extends Component{
    
        private Component component;
    
        public  Decorator(Component component){
            this.component = component;
        }
    
        public Component getComponent() {
            return component;
        }
    
        public void setComponent(Component component) {
            this.component = component;
        }
    
        @Override
        public void operation() {
            component.operation();
        }
    }
    
    class ConcreteDecorator1 extends Decorator{
        private int addedState;
    
        public ConcreteDecorator1(Component component) {
            super(component);
        }
    }
    
    class ConcreteDecorator2 extends Decorator{
    
        public ConcreteDecorator2(Component component) {
            super(component);
        }
    
        @Override
        public void operation() {
            getComponent().operation();
            addedBehavior();
        }
    
        public void addedBehavior(){
            System.out.println("add behavior");
        }
    }
    ```
    
    1. 代理模式
    ```java
    package me.maxiaolong.designpattern;
    
    /**
     * @author maxiaolong
     * created at 2020/7/4
     * 代理模式:当直接访问某些对象存在问题是可以通过一个代理对象间接访问,为了保证客户端使用的透明性,所访问的真实对象与代理对象都要实现相同的接口(组合)
     * 使用组合的方式(且使用的是真实对象作为类的声明,设置为私有),代理类与真实类需要实现同样的接口,客户端应该完全无法接触真实客体
     */
    public class ProxyClient {
        public static void main(String[] args) {
            Proxy proxy = new Proxy();
            proxy.request();
        }
    }
    
    abstract class Subject{
        public abstract void request();
    }
    
    class RealSubject extends Subject{
    
        @Override
        public void request() {
            System.out.println("real subject");
        }
    }
    
    class Proxy extends Subject{
    
        /**
         * 一定要使用真实对象
         */
        private RealSubject realSubject = new RealSubject();
    
        @Override
        public void request() {
            beforeRequest();
            realSubject.request();
            afterRequest();
        }
    
        public void beforeRequest(){
            System.out.println("before proxy");
        }
    
        public void afterRequest(){
            System.out.println("after proxy");
        }
    }
    ```
  • 相关阅读:
    ssh.sh_for_ubuntu1604
    ssh.sh_for_ubuntu1404
    ssh.sh_for_ubuntu1204
    ssh.sh_for_centos
    raw,cow,qcow,qcow2镜像的比较
    Oz 创建Windows2008R2镜像
    Oz 创建Ubuntu镜像
    Oz 创建Debian8镜像
    Oz 创建CentOS7镜像
    Oz 创建CentOS6镜像
  • 原文地址:https://www.cnblogs.com/mrdragonma/p/13253914.html
Copyright © 2011-2022 走看看