UML is a graphical notation specifically for drawing diagrams of an object-oriented system. A UML describes over a dozen different diagrams, but we’re only interested in a few of the most common, such as the classic class diagram.

ConcepObjectModel

ConcepObjectModel

Class Diagram

Class Diagram

When drawing diagrams, it’s common to see signs before the attributes or methods, most

– Minus signs in front of the attributes here. This is referred to as controlling visibility, and Minus means these should be private to the class, not directly accessible from other objects.

+ For example getName operation which will be public and marked with a Plus sign.

 

 

Identifying inheritance

 

Inheritates Abstract Class

Inheritates AbstractClass

easiest way of identifying an inheritance situation is with two words, “Is A”. Inheritance describes an “Is A” relationship.

 

Using aggregation and composition

Aggregation Composition

Aggregation Composition

it’s the unfilled diamond. So, for example, we might have a classroom object that will contain an array of student objects that might be important to diagram that relationship.

Document has a Page or has many pages. But if I were to delete the Document object all the associated Page objects should be deleted too. I would not expect then those page objects to be shared with any other part of the application. On the other hand in a plain aggregation situation as with the Classroom and Student relationship, if I deleted the Classroom object, perhaps the class got canceled, I would not expect all the Student objects to be destroyed, they may be used in different classrooms or just be able to live on their own.

And that’s the difference, Composition implies ownership, Aggregation does not. Now Aggregation is not usually worth showing on a diagram, but Composition often can be. If the lifetime of an object is dependent on another object existing that can be worth showing, even if we are just prompting the idea that when you’re defining the owning class, say here the Document class, you may need to write aconstructor and a destructor that would take care of creating and/or deleting the internal objects.

 

Using interfaces

Interfaces

Interfaces

Sequence Diagrams

Sequence Diagrams

Class Diagram

Class Diagram

Dependency:

Khi một class có sử dụng một instance của class B như một tham số đầu vào của method trong class A.
Ex:

public class A {

    public void doSomething(B b) {}

Nhưng giả sử class A có một member với kiểu dữ liệu là class B thì lúc đó ta có mối quan hệ

Aggregation:

Ex:

public class A {

    private B _b;

    public void setB(B b) { _b = b; }

Với ví dụ trên ta có thể thấy mối quan hệ giữa A và B chỉ là Aggreation có nghĩa là nếu như A bị hủy thì chưa chắc B đã được hủy. Do vậy nếu như instance của B được khởi tạo trong A như ví dụ dưới đây ta có mối quan hệ là

Composition:

Ex:

public class A {

    private B _b = new B();

Hoặc

public class A {

    private B _b;

    public A() {
        _b = new B();
    } // default constructor

Còn mối quan hệ như Inheritance hay Realization thì sao? Đúng như tên gọi của nó, nếu class B kế thừa từ class A ta có mối quan hệ

Inheritance:

Ex:

public class A {

    ...

} // class A

public class B extends A {

    ....

} // class B

Mặt khác nếu class B implement từ A thì ta có mối quan hệ (Ví dụ ta hay thấy đó là implement một interface)

Realization:

Ex:

public interface A {

    ...

} // interface A

public class B implements A {

    ...

} // class B
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *