Kate Lo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Week 9 ## Class Diagrams: Intermediate-Level - A class diagram can also show different types of relationships between classes: inheritance, compositions, aggregations, dependencies. ### Modeling inheritance - You can use a **triangle** and a **solid line** to indicate class inheritance. ![](https://i.imgur.com/GP2ayC0.png =20%x) ### Modeling composition - A composition is an association that **represents a strong whole-part relationship**. When the *whole* is destroyed, *parts* are destroyed too. - Composition also implies that there **cannot be cyclical links**. - UML uses a **solid diamond symbol** to denote composition. ![](https://i.imgur.com/ci6b9ER.png =30%x) #### Implementing composition - Composition is implemented using a **normal variable**. - If correctly implemented, the ‘part’ object will be deleted when the ‘whole’ object is deleted. Ideally, the ‘part’ object may not even be visible to clients of the ‘whole’ object. > In this code, the Email has a composition type relationship with the Subject class, in the sense that the subject is part of the email. ```java= class Email { private Subject subject; ... } ``` ### Modeling aggregation - Aggregation represents a **container-contained relationship**. It is a weaker relationship than composition. - UML uses a **hollow diamond** is used to indicate an aggregation. ![](https://i.imgur.com/gwH42qx.png =30%x) - Which one of these is **recommended not to use** in UML diagrams because it adds more confusion than clarity? - [ ] a. Composition symbol - [x] b. Aggregation symbol #### Implementing aggregation - Implementation is similar to that of composition except the containee object can exist even after the container object is deleted. > In the code below, there is an aggregation association between the Team class and the Person in that a Team contains Person a object who is the leader of the team. ```java= class Team { Person leader; ... void setLeader(Person p) { leader = p; } } ``` ### Modeling dependencies - A dependency is a need for one class to depend on another **without having a direct association** with it. One cause of dependencies is interactions between objects that do not have a long-term link between them. - UML uses a **dashed arrow** to show dependencies. ![](https://i.imgur.com/7xmmrJT.png =40%x) #### Implementing dependencies > In the code below, Foo has a dependency on Bar but it is not an association because it is only a transient interaction and there is no long term relationship between a Foo object and a Bar object. i.e. the Foo object does not keep the Bar object it receives as a parameter. ```java= class Foo{ int calculate(Bar bar){ return bar.getValue(); } } class Bar{ int value; int getValue(){ return value; } } ``` ### Modeling enumerations ![](https://i.imgur.com/0Y0wynI.png =20%x) ![](https://i.imgur.com/wYFlOQN.png =80%x) ### Modeling abstract classes - You can use italics or {abstract} (preferred) keyword to denote abstract classes/methods. ![](https://i.imgur.com/aiKp3ne.png =50%x) ![](https://i.imgur.com/xhgZpr7.png =50%x) ### Modeling interfaces - An interface is shown similar to a class with an additional keyword **<<interface>>**. When a class implements an interface, it is shown similar to class inheritance except a **dashed line** is used instead of a solid line. ![](https://i.imgur.com/VZiOZgT.png =40%x) ## Logging - Logging is the deliberate recording of certain information during a **program execution** for future reference. First, import the relevant Java package: ```java= import java.util.logging.*; ``` Next, create a Logger: ```java= private static Logger logger = Logger.getLogger("Foo"); ``` Now, you can use the Logger object to log information. Note the use of logging level for each message. When running the code, the logging level can be set to WARNING so that log messages specified as INFO level (which is a lower level than WARNING) will not be written to the log file at all. ```java= // log a message at INFO level logger.log(Level.INFO, "going to start processing"); //... processInput(); if(error){ //log a message at WARNING level logger.log(Level.WARNING, "processing error", ex); } //... logger.log(Level.INFO, "end of processing"); ``` ## Assertions - **Assertions are used to define assumptions about the program state so that the runtime can verify them.** An assertion failure indicates a possible bug in the code because the code has resulted in a program state that violates an assumption about how the code should behave. - **If the runtime detects an assertion failure, it typically take some drastic action** such as terminating the execution with an error message. - Use the **`assert` keyword to define assertions**. > This assertion will fail with the message x should be 0 if x is not 0 at this point. ```java= x = getX(); assert x == 0 : "x should be 0"; ... ``` - Assertions can be disabled without modifying the code. ex. `java -enableassertions HelloWorld` (or `java -ea HelloWorld`) will run `HelloWorld` with assertions enabled while `java -disableassertions HelloWorld` will run it without verifying assertions. - **Java disables assertions by default.** - Assertions are suitable for **verifying assumptions about Internal Invariants, Control-Flow Invariants, Preconditions, Postconditions, and Class Invariants**. - **Exceptions and assertions are two complementary ways of handling errors** in software but they serve different purposes. Therefore, **both assertions and exceptions should be used in code**. - The raising of an **exception** indicates an **unusual condition** created by the user (e.g. user inputs an unacceptable input) or the environment (e.g., a file needed for the program is missing). - An **assertion failure** indicates the **programmer made a mistake** in the code (e.g., a null value is returned from a method that is not supposed to return null under any circumstances). ## Design Principles ### Abstraction - The guiding principle of abstraction is that **only details that are relevant to the current perspective or the task at hand needs to be considered.** - ==Data abstraction==: abstracting away the lower level data items and thinking in terms of bigger entities - ==Control abstraction==: abstracting away details of the actual control flow to focus on tasks at a higher level > print(“Hello”) is an abstraction of the actual output mechanism within the computer. ### Coupling - **Coupling is a measure of the degree of dependence between components, classes, methods, etc**. ==**Low coupling**== indicates that a component is **less dependent** on other components. ==**High coupling**== (aka tight coupling or strong coupling) is **discouraged** due to the following disadvantages: - **Maintenance is harder** because a change in one module could cause changes in other modules coupled to it (i.e. a ripple effect). - **Integration is harder** because multiple components coupled with each other have to be integrated at the same time. - **Testing and reuse of the module is harder** due to its dependence on other modules. ![](https://i.imgur.com/QvWeeon.png =60%x) > Discuss the coupling levels of alternative designs x and y. ![](https://i.imgur.com/cXsUwwm.png =50%x) -> Overall coupling levels in x and y seem to be similar (neither has more dependencies than the other). (Note that the number of dependency links is not a definitive measure of the level of coupling. Some links may be stronger than the others.). However, in x, A is highly-coupled to the rest of the system while B, C, D, and E are standalone (do not depend on anything else). In y, no component is as highly-coupled as A of x. However, only D and E are standalone. > Explain the link (if any) between regressions and coupling. -> When the system is highly-coupled, the risk of regressions is higher too e.g. when component A is modified, all components ‘coupled’ to component A risk ‘unintended behavioral changes’. > Discuss the relationship between coupling and testability. -> Coupling decreases testability because if the SUT is coupled to many other components it becomes difficult to test the SUI in isolation of its dependencies. - **X is coupled to Y if a change to Y can ==potentially== require a change in X.** ### Cohesion - **Cohesion is a measure of how strongly-related and focused the various responsibilities of a component are.** A highly-cohesive component keeps related functionalities together while keeping out all other unrelated things. - **Higher cohesion is better**. Disadvantages of low cohesion (aka weak cohesion): - **Lowers the understandability of modules** as it is difficult to express module functionalities at a higher level. - **Lowers maintainability** because a module can be modified due to unrelated causes (reason: the module contains code unrelated to each other) or many many modules may need to be modified to achieve a small change in behavior (reason: because the code related to that change is not localized to a single module). - **Lowers reusability of modules** because they do not represent logical units of functionality. ### Single Responsibility Principle - Single Responsibility Principle (SRP): **A class should have one, and only one, reason to change.** ### Separation of Concerns Principle - Separation of Concerns Principle (SoC): **To achieve better modularity, separate the code into distinct sections, such that each section addresses a separate concern.** - This principle should lead to **higher cohesion and lower coupling.** ## Testing: Intermediate Techniques - **Testability is an indication of how easy it is to test an SUT.** As testability depends a lot on the design and implementation. You should try to increase the testability when you design and implement a software. **The higher the testability, the easier it is to achieve a better quality software.**

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully