设计模式-之结构模式

一:结构型模式总结全图:

                       

 

二:详细解析:

  1. (Adapter)适配器模式图

 

 

Adapter适配器模式:创建一个接口去使用现成的不兼容的接口的类,主要应用是对系统维护添加新功能时。

使用过程:给要使用的对象提供一个访问接口,使得目标对象功能整合到系统中。

生活实例:简单典型的就是电脑笔记本电源

2. Proxy代理模式

 

Proxy代理模式:用代理对象来控制对原有对象的引用,其接口不可以改变。

3. Decorator装饰模式图

 

 

 

public class Test

{

   public static void main(String[] args)

   {

      ErrorLog errorLog = new ErrorLog();

      DecorateLog errorLogAbove = new DecorateLog(errorLog);

      System.out.println("errorLogAbove:");

      errorLogAbove.printMessage();

 

      DubugLog dubugLog = new DubugLog();

      DecorateLog dubugLogAbove = new DecorateLog(errorLogAbove, dubugLog);

      System.out.println("dubugLogAbove:");

      dubugLogAbove.printMessage();

 

      InforLog inforLog = new InforLog();

      DecorateLog inforLogAbove = new DecorateLog(dubugLogAbove, inforLog);

      System.out.println("inforLogAbove:");

      inforLogAbove.printMessage();

   }

}

 

abstract class Log

{

   public abstract void printMessage();

}

 

class DecorateLog extends Log

{

 

   Log log1;

   Log log2;

 

   public DecorateLog()

   {

   }

 

   public DecorateLog(Log log1)

   {

      this.log1 = log1;

   }

 

   public DecorateLog(Log log1, Log log2)

   {

      this.log1 = log1;

      this.log2 = log2;

   }

 

   public void printMessage()

   {

      if (null != log1)

         log1.printMessage();

      if (null != log2)

         log2.printMessage();

   }

}

 

class ErrorLog extends Log

{

   public void printMessage()

   {

      System.out.println("Error Information");

   }

}

 

class DubugLog extends Log

{

   public void printMessage()

   {

      System.out.println("DubugLog Information");

   }

}

 

class InforLog extends Log

{

   public void printMessage()

   {

      System.out.println("InforLog Information");

   }

}

Decorator装饰模式:为目标对象动态添加一些新的操作和功能。

4.外观模式图

 

Facade外观模式:对系统屏蔽子系统的组件,降低客户端和系统内部的耦合。

生活实例:发短信查话费。项目中应用为数据库方面。(DataUtil类的实现就是Facade)

5.桥接模式图

 

Bridge桥接模式:将系统的抽象与实现分离,有利于分层架构的实现。减少因变化带来的代码修改。

package com.test.Bridge;

 

public class Test

{

   public static void main(String[] args)

   {

      AbstractRoad Road1 = new SpeedWay();

      Road1.car = new Car();

      Road1.Run();

   }

}

 

abstract class AbstractCar

{

   public abstract void Run();

}

 

class Car extends AbstractCar

{

   public void Run()

   {

 

      System.out.println("小汽车在");

   }

}

 

class Bus extends AbstractCar

{

   public void Run()

   {

 

      System.out.println("公共汽车在");

   }

}

 

abstract class AbstractRoad

{

     AbstractCar car;

 

   public abstract void Run();

}

 

class SpeedWay extends AbstractRoad

{

   public void Run()

   {

 

      car.Run();

      System.out.println("高速公路上行驶");

   }

}

 

class Street extends AbstractRoad

{

   public void Run()

   {

 

      car.Run();

      System.out.println("市区街道上行驶");

   }

}

生活实例:设备开关

6.组合模式图

 

Composite组合模式:使得用户对对象使用具有一致性

在结构上和decorate很想,通常情况两种模式是一起实现的

package com.test.Composite;

 

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

 

public class Test

{

   public static void main(String[] args)

   {

      // 构造根节点

      Composite rootComponent = new Composite("root");

 

      // 添加两个叶子几点,也就是子部件

      Leaf leaf1 = new Leaf("leaf1");

      Leaf leaf2 = new Leaf("leaf2");

 

      rootComponent.add(leaf1);

      rootComponent.add(leaf2);

 

      // 遍历组合部件

      rootComponent.eachChild();

   }

}

 

abstract class Component

{

   String name;

 

   public String getName()

   {

      return name;

   }

 

   public void setName(String name)

   {

      this.name = name;

   }

 

   // 添加部件

   public abstract void add(Component component);

 

   // 删除部件

   public abstract void remove(Component component);

 

   // 遍历所有子部件

   public abstract void eachChild();

    

}

 

class Leaf extends Component

{

   public Leaf()

   {

   }

 

   public Leaf(String name)

   {

      this.name = name;

   }

 

   // 叶子节点不具备添加的能力,所以不实现

   public void add(Component component)

   {

      throw new NotImplementedException();

   }

 

   // 叶子节点不具备添加的能力必然也不能删除

   public void remove(Component component)

   {

      throw new NotImplementedException();

   }

 

   // 叶子节点没有子节点所以显示自己的执行结果

   public void eachChild()

   {

      String str = String.format(name+"执行了..");

      System.out.println(str);

   }

}

 

class Composite extends Component

{

   public Composite()

   {

   }

 

   public Composite(String name)

   {

      this.name = name;

   }

 

   // 用来保存组合的部件

   List<Component> myList = new ArrayList<Component>();

 

   // 添加节点 添加部件

   public void add(Component component)

   {

      myList.add(component);

   }

 

   // 删除节点 删除部件

   public void remove(Component component)

   {

      myList.remove(component);

   }

 

   // 遍历子节点

   public void eachChild()

   {

      String str = String.format(name+"执行了..");

      System.out.println(str);

      for (Component c : myList)

      {

         c.eachChild();

      }

 

   }

}

 

 

7.享元模式

 

享元模式:运用共享技术有效的支持大量细粒度的对象

 

 

转载于:https://www.cnblogs.com/liule1225/p/3998414.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值