第四单元总结

第四单元总结

18373330 张洪康

第一次作业

  • UML图

    ![](https://img2020.cnblogs.com/blog/1976940/202006/1976940-20200619210708509-211588568.png)
    
  • 设计架构思想
    private final HashMap<String, UmlElement> elements = new HashMap<>();

    private final HashSet<UmlAssociation> umlAssociation = new HashSet<>();
    private final HashSet<UmlAssociationEnd> umlAssociationEnd = new HashSet<>();
    private final HashSet<UmlAttribute> umlAttribute = new HashSet<>();
    private final HashSet<UmlClass> umlClass = new HashSet<>();
    private final HashSet<UmlGeneralization> umlGeneralization = new HashSet<>();
    private final HashSet<UmlInterface> umlInterface = new HashSet<>();
    private final HashSet<UmlInterfaceRealization> umlInterfaceRealization = new HashSet<>();
    private final HashSet<UmlOperation> umlOperation = new HashSet<>();
    private final HashSet<UmlParameter> umlParameter = new HashSet<>();
    
    private final HashMap<UmlClass,UmlClass> ccGeneralizationMap = new HashMap<>();
    private final HashMap<UmlClass,HashSet<UmlInterface>> ciGeneralizationMap = new HashMap<>();
    private final HashMap<UmlInterface,HashSet<UmlInterface>> iiGeneralizationMap = new HashMap<>();
    
    • 首先采用一个ID对UmlElement的映射,可以通过ID访问对应元素(运用ID唯一特性)
    • 其次用HashSet分别存储不同类型的元素,代码如下
      for (UmlElement item:elements) {
      this.elements.put(item.getId(),item);
      switch (item.getElementType()) {
      case UML_CLASS:
      umlClass.add((UmlClass)item);
      break;
      case UML_ATTRIBUTE:
      umlAttribute.add((UmlAttribute)item);
      break;
      case UML_INTERFACE:
      umlInterface.add((UmlInterface)item);
      break;
      case UML_OPERATION:
      umlOperation.add((UmlOperation)item);
      break;
      case UML_PARAMETER:
      umlParameter.add((UmlParameter)item);
      break;
      case UML_ASSOCIATION:
      umlAssociation.add((UmlAssociation)item);
      break;
      case UML_GENERALIZATION:
      umlGeneralization.add((UmlGeneralization)item);
      break;
      case UML_ASSOCIATION_END:
      umlAssociationEnd.add((UmlAssociationEnd)item);
      break;
      case UML_INTERFACE_REALIZATION:
      umlInterfaceRealization.add((UmlInterfaceRealization)item);
      break;
      default:
      break;
      }
      }
    • 最后建立继承与实现接口的映射,代码如下
      for (UmlGeneralization generalization:umlGeneralization) {
      UmlElement source = this.elements.get(generalization.getSource());
      UmlElement target = this.elements.get(generalization.getTarget());
      if (source instanceof UmlClass && target instanceof UmlClass) {
      ccGeneralizationMap.put((UmlClass) source, (UmlClass) target);
      }
      else if (source instanceof UmlInterface && target instanceof UmlInterface) {
      iiGeneralizationMap.putIfAbsent((UmlInterface) source,new HashSet<>());
      HashSet interfaces = iiGeneralizationMap.get(source);
      interfaces.add((UmlInterface) target);
      }
      else {
      while (true) {
      System.out.print("!!!!!!");
      }
      }
      }
      for (UmlInterfaceRealization interfaceRealization:umlInterfaceRealization) {
      UmlElement source = this.elements.get(interfaceRealization.getSource());
      UmlElement target = this.elements.get(interfaceRealization.getTarget());
      ciGeneralizationMap.putIfAbsent((UmlClass) source,new HashSet<>());
      HashSet interfaces = ciGeneralizationMap.get(source);
      interfaces.add((UmlInterface) target);
      }
      由于接口可以多继承以及类可以实现多个接口,所以是从元素到HashSet的映射,HashSet中包含了所有继承(实现)的元素。
    • 大部分的方法可以采用遍历实现,部分方法可能需要使用多次遍历,如
      public List getImplementInterfaceList(String className) throws ClassNotFoundException, ClassDuplicatedException {
      HashSet answer = new HashSet<>();
      UmlClass umlClass = getClass(className);
      HashSet allClass = getAllClass(umlClass);
      for (String id:allClass) {
      umlClass = (UmlClass) elements.get(id);
      HashSet interfaces = ciGeneralizationMap.get(umlClass);
      if (interfaces != null) {answer.addAll(interfaces);}
      }
      HashSet temp = new HashSet<>(answer);
      while (true) {
      HashSet targets = new HashSet<>();
      for (UmlInterface umlInterface:temp) {
      if (iiGeneralizationMap.get(umlInterface) != null) {
      targets.addAll(iiGeneralizationMap.get(umlInterface));
      }
      }
      answer.addAll(temp);
      if (targets.isEmpty()) { break;}
      temp = new HashSet<>(targets);
      targets.clear();
      }
      ArrayList arrayList = new ArrayList<>();
      for (UmlInterface umlInterface:answer) {
      arrayList.add(umlInterface.getName());
      }
      return arrayList;
      }
      首先找到直接继承的集合,然后不断访问继承映射从该集合中的元素获取新的集合。把该元素放到答案中并且将获取到的新集合放到原集合内,重复直到集合为空(即找不到任何继承的元素)。后面大多用图的方法类似。

第二次作业

  • UML图

    直接新建类继承第一次作业的类,实现新增的接口。
  • 设计架构思想
    与上次基本相同
    private final HashSet umlStateMachines = new HashSet<>();
    private final HashSet umlRegions = new HashSet<>();
    private final HashSet umlStates = new HashSet<>();
    private final HashSet umlTransitions = new HashSet<>();
    private final HashMap<UmlElement,HashSet> ssTransitionMap = new HashMap<>();
    private final HashSet umlInteractions = new HashSet<>();
    private final HashSet umlLifelines = new HashSet<>();
    private final HashSet umlMessages = new HashSet<>();
    for (UmlElement item:elements) {
    switch (item.getElementType()) {
    case UML_STATE_MACHINE: umlStateMachines.add((UmlStateMachine) item);
    break;
    case UML_REGION: umlRegions.add((UmlRegion)item);
    break;
    case UML_STATE:
    case UML_FINAL_STATE:
    case UML_PSEUDOSTATE:
    umlStates.add(item);
    break;
    case UML_TRANSITION: umlTransitions.add((UmlTransition)item);
    break;
    case UML_INTERACTION: umlInteractions.add((UmlInteraction)item);
    break;
    case UML_LIFELINE: umlLifelines.add((UmlLifeline)item);
    break;
    case UML_MESSAGE: umlMessages.add((UmlMessage)item);
    break;
    default:
    break;
    }
    }
    HashMap<String, UmlElement> elements1 = super.getElements();
    for (UmlTransition umlTransition:umlTransitions) {
    UmlElement source = elements1.get(umlTransition.getSource());
    UmlElement target = elements1.get(umlTransition.getTarget());
    ssTransitionMap.putIfAbsent(source,new HashSet<>());
    HashSet states = ssTransitionMap.get(source);
    states.add(target);
    }

第三次作业

  • UML图

  • 同样没有修改作业1和作业2,直接新建类满足新增需求。

  • 作业架构基本相同

BUG分析

  • 第一次作业在返回的时候返回了HashSet,忘了可能有多个相同的情况,改为ArrayList后通过

学期总结

  • 第一单元以求导现实要求为基础,主题是多项式的求导。在本单元的设计中,最重要的是对之前C语言的面向过程进行区分,将不同类别的项抽象成各种各样的类。在三次作业中一步一步的理解面向对象的设计思路与架构,对不同对象进行良好的封装,破除了之前一个程序写一个文件写很长的问题,是走向一名合格的程序员的必经之路。(在一次又一次的重构中怀疑自我)
  • 第二单元以现实中的电梯调度为基础,主题是多线程电梯调度。这个单元我们接触到了一个全新的领域:多线程。多线程由于可复原性比较低,DEBUG较困难,大部分只能使用肉眼DEBUG法,因此深刻理解多线程,死锁,阻塞等概念对我们编写代码异常重要。多线程的领域十分广泛,其构造方法也多种多样。最普通的生产者消费者模型便是其一。而在以后的工作学习中,多线程也是十分重要的一部分。学好多线程对我们未来的学习生活具有重要意义。
  • 第三单元的主题是JML规格,模拟一个社交网络。这个单元实现较为简单,最难的部分是读懂JML规格。JML规格是为了方便多人协作,在多个人共同完成一份工程的时候起到至关重要的作用。通过统一的语言协调不同部分的工作。即使语言可能不同,但由于JML规格的存在使得完全满足规格的代码可以完全对接,这在工程设计上十分关键。
  • 第四单元主要是让我们熟悉UML图的表示,具体实现并不难。

心得体会

  • 作为6系大二三巨头(CO,OO,OS)之一,难度自然没得说。通过今年的学习,我头发掉了,身体虚了,就连鼠标也拿不动了
    这学期我又新掌握了一门语言,PYTHON NO 1!
    我熟练掌握了面向对象的思路,以及如何创建一个对象
    第一次写出并使用多线程,并在这学期的OO网课中熟练的运用了多线程思想
    了解了多种设计模式,如生产者消费者模式,工厂模式等等等等
    对JAVA多种多样的容器有了更深入的理解,根据不同的需求取长补短。
    紧接这PYTHON对CPU的自动化测试后,我又学会了对JAVA的自动化测试

一点小小小小小的建议

  • 我个人感觉第三单元第四单元没有第一二单元精良(虽然我不是抖M),但第一二单元的作业是让我们写代码边写边理解,而第三四单元的作业是理解之后硬套(可能表述的有点不清),尤其是第四单元,其实真正的工程并不能促使我更进一步的理解UML。
posted @ 2020-06-19 21:09  18373330_张洪康  阅读(160)  评论(0编辑  收藏  举报