Object - Oriented analysis and design with uml 2.0 - Bài 8: Thiết kế lớp

Tài liệu Object - Oriented analysis and design with uml 2.0 - Bài 8: Thiết kế lớp: *OBJECT-ORIENTED ANALYSIS AND DESIGN WITH UML 2.0Bài 8. Thiết kế lớpBộ môn Công nghệ phần mềm KHOA CễNG NGHỆ THễNG TIN TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI*Mụ hỡnh thiết kế*Nội dungXác định các thao tác (Operation)Xác định phương thức (Method)Xỏc định cỏc liờn kết (Association) Xác định các thuụ̣c tính (Attribute)Xác định phụ thuụ̣c (Dependency)Xỏc định tổng quỏt húa (Generalization)*Ánh xạ cỏc thụng điệp trong biểu đồ tương tỏc thành cỏc thao tỏc của cỏc lớp1. Xác định các thao tác : ClassA1 : performResponsibility (): result : ClassBClassB+ performResponsibility(): result*Tờn và mụ tả thao tácTạo ra cỏc tờn thao tỏc thớch hợpMụ tả kết quảSử dụng gúc nhỡn của đối tượng khỏch (gọi)Nhất quỏn giữa cỏc lớpXỏc định chữ ký của thao tỏcoperationName([direction]parameter : class,..) : returnTypeDirection: in (mặc định), out hoặc inout Đưa ra mụ tả ngắn gọn, bao gồm ý nghĩa của tất cả cỏc tham số*Hướng dõ̃n thiờ́t kờ́ chữ ký thao tácKhi thiết kế chữ ký của thao tỏc, cần xem...

ppt65 trang | Chia sẻ: Khủng Long | Lượt xem: 948 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Object - Oriented analysis and design with uml 2.0 - Bài 8: Thiết kế lớp, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
*OBJECT-ORIENTED ANALYSIS AND DESIGN WITH UML 2.0Bài 8. Thiết kế lớpBé m«n C«ng nghƯ phÇn mỊm KHOA CƠNG NGHỆ THƠNG TIN TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI*Mơ hình thiết kế*Nội dungXác định các thao tác (Operation)Xác định phương thức (Method)Xác định các liên kết (Association) Xác định các thuợc tính (Attribute)Xác định phụ thuợc (Dependency)Xác định tổng quát hĩa (Generalization)*Ánh xạ các thơng điệp trong biểu đồ tương tác thành các thao tác của các lớp1. Xác định các thao tác : ClassA1 : performResponsibility (): result : ClassBClassB+ performResponsibility(): result*Tên và mơ tả thao tácTạo ra các tên thao tác thích hợpMơ tả kết quảSử dụng gĩc nhìn của đối tượng khách (gọi)Nhất quán giữa các lớpXác định chữ ký của thao tácoperationName([direction]parameter : class,..) : returnTypeDirection: in (mặc định), out hoặc inout Đưa ra mơ tả ngắn gọn, bao gồm ý nghĩa của tất cả các tham số*Hướng dẫn thiết kế chữ ký thao tácKhi thiết kế chữ ký của thao tác, cần xem xét liệu tham số cĩ:Được truyền theo tham trị hay tham biếnCĩ bị thay đổi bởi thao tác hay khơngCĩ tùy chọn khơngThiết lập các giá trị mặc địnhCác khoảng tham số khơng hợp lệCàng ít tham số, càng tốtTruyền các đối tượng thay vì hàng loạt các dữ liệu.*Public operationsProtected operationsPrivateoperationsPhạm vi truy cập của thao tác (Operation Visibility)Phạm vi truy cập được sử dụng để thực hiện khả năng đĩng gĩiCĩ thể là public, protected, hoặc private*Phạm vi truy cập được biểu diễn như thế nào?Các ký hiệu sau được sử dụng: + Public access # Protected access - Private accessClass1- privateAttribute+ publicAttribute# protectedAttribute- privateOperation ()+ publicOPeration ()# protecteOperation ()*Phạm vi (Scope)Xác định số lượng thể hiện của thuộc tính/thao tác:Instance: Một thể hiện cho mỗi thể hiện của mỗi lớpClassifier: Một thể hiện cho tất cả các thể hiện của lớpPhạm vi Classifier được ký hiệu bằng cách gạch dưới tên thuộc tính/thao tác.Class1- classifierScopeAttr- instanceScopeAttr+ classifierScopeOp ()+ instanceScopeOp ()*Ví dụ: ScopeStudent- name- address- nextAvailID : int+ addSchedule ([in] theSchedule : Schedule, [in] forSemester : Semester)+ getSchedule ([in] forSemester : Semester) : Schedule+ hasPrerequisites ([in] forCourseOffering : CourseOffering) : boolean# passed ([in] theCourseOffering : CourseOffering) : boolean+ getNextAvailID () : int- studentID*Ví dụ: Xác định thao tácStudent+ getTuition() : double+ addSchedule ( [in] aSchedule : Schedule)+ getSchedule ( [in] forSemester : Semester) : Schedule+ deleteSchedule ( [in] forSemester : Semester)+ hasPrerequisites ( [in] forCourseOffering : CourseOffering) : boolean# hasPassed ( [in] aCourseOffering : CourseOffering) : boolean+ getNextAvailID() : int+ getStudentID() : int+ getName() : String+ getAddress() : String0..10..1+ registrant10..*RegistrationController+ submitSchedule()+ saveSchedule()+ getCourseOfferings() : CourseOfferingList+ getCurrentSchedule ( [in] forStudent : Student, [in] forSemester : Semester) : Schedule+ deleteCurrentSchedule()+ new ( [in] forStudentID : String)+ getStudent ( [in] anID : int) : StudentICourseCatalogSystem+ getCourseOfferings()+ initialize()>**Nội dungXác định các thao tác (Operation)Xác định phương thức (Method)Xác định các liên kết (Association) Xác định các thuợc tính (Attribute)Xác định phụ thuợc (Dependency)Xác định tổng quát hĩa (Generalization)*2. Xác định phương thứcPhương thức (method) là gì?Mơ tả sự thực thi của thao tác (operation)Mục đíchXác định các vấn đề riêng cho việc thực thi thao tácCác vấn đề cần xem xét:Các thuật toán đặc biệtCác đới tượng hoặc các thao tác khác cần sử dụngCác thuợc tính và các tham sớ sẽ được thực thi và sử dụngCác mới quan hệ sẽ được thực thi và sử dụng*Nội dungXác định các thao tác (Operation)Xác định phương thức (Method)Xác định các liên kết (Association) Xác định các thuợc tính (Attribute)Xác định phụ thuợc (Dependency)Xác định tổng quát hĩa (Generalization)*3. Xác định các liên kếtMục đíchTinh chỉnh các kết hợp cịn lạiCác vấn đề cần xem xét:Association vs. AggregationAggregation vs. CompositionAttribute vs. AssociationNavigabilityMultiplicity design*Composition là gì?Một dạng của aggregation với quyền sở hữu mạnh và các vịng đời trùng khớp.Whole sở hữu Part, tạo và hủy Part.Part bị bỏ đi khi Whole bị bỏ, Part khơng thể tồn tại nếu Whole khơng tồn tại.WholeCompositionPartPartWhole*Shared AggregationNon-shared AggregationAggregation: Shared vs. Non-sharedBy definition, composition is non-shared aggregation.WholePart1..*0..*Multiplicity > 1Multiplicity = 1Multiplicity = 11WholePart10..*WholePart0..*Composition*Aggregation hay Composition?Xem xét vịng đời của Class1 và Class2AggregationCompositionClass1Class2Class1Class2*Ví dụ: Composition10..*ScheduleCourseInfoCourseRegistrationForm11CourseRegistrationController*Attributes và CompositionSử dụng composition khi:Các đặc tính (property) cần định danh độc lậpNhiều lớp cĩ cùng các đặc tínhCác đặc tính cĩ một cấu trúc phức tạp và các đặc tính của riêng chúngCác đặc tính phải cĩ hành vi phức tạp của riêng chúngCác đặc tính cĩ mối quan hệ của riêng chúngCịn lại sử dụng thuộc tính (attribute)*Điều hướng (Navigability) là gì?Điều hướng từ một lớp kết hợp đến lớp đích sử dụng association.CourseRegistrationController>CourseInfo*Navigability: Hướng nào thực sự cần?Xem xét các biểu đồ tương tácThậm chí khi cả hai hướng đều cĩ vẻ cần nhưng một hướng lại vẫn hoạt động tốtNavigability theo 1 hướng ít xảy raSố lượng thể hiện của một lớp là nhỏ?0..40..*+ primaryCoursesCourseInfoSchedule0..40..*+ primaryCoursesCourseInfoSchedule0..40..*+ primaryCoursesCourseInfoSchedule*Ví dụ: Tinh chỉnh điều hướngTotal number of Schedule is small, orNever need a list of the Schedule on which the CourseInfo appearsTotal number of CourseInfo is small, orNever need a list of CourseInfo on a ScheduleTotal number of CourseInfo and Schedule are not smallMust be able to navigate in both directions0..40..*+ primaryCoursesCourseInfoSchedule0..40..*+ primaryCoursesCourseInfoSchedule0..40..*+ primaryCoursesCourseInfoSchedule*Example: Association Class DesignDesign Decisions0..*0..4+ primaryCourses+ alternateCourses0..*0..2PrimaryScheduleOfferingInfo- grade+ // is enrolled in? ()+ // mark as enrolled in ()+ // mark as committed ()ScheduleCourseInfoCourseInfoSchedule10..*- theCourseOffering+ alternateCourses0..*0..2PrimaryScheduleOfferingInfo- grade+ // is enrolled in? ()+ // mark as enrolled in ()+ // mark as committed ()- primaryCourseOfferingInfo0..41*Multiplicity = 1, hoặc Multiplicity = 0..1Cĩ thể được thực hiện trực tiếp bằng một giá trị đơn hoặc con trỏKhơng cần thiết kế thêmMultiplicity > 1Khơng thể sử dụng giá trị đơn hoặc con trỏCần thiết kế thêm nữaThiết kế bội số quan hệCần một cho CourseInfo0..10..1LecturerInfoCourseInfo+ Instructor0..10..*LecturerInfoCourseInfo+ Instructor*Multiplicity Design: OptionalityIf a link is optional, make sure to include an operation to test for the existence of the linkLecturerInfoCourseInfo0..10..***Chương 5. Thiết kế lớpXác định các thao tác (Operation)Xác định phương thức (Method)Xác định các liên kết (Association) Xác định các thuợc tính (Attribute)Xác định phụ thuợc (Dependency)Xác định tổng quát hĩa (Generalization)*4. Xác định các thuợc tínhXem xét các mơ tả phương thứcXem xét các trạng tháiXem xét bất kỳ thơng tin nào mà lớp đĩ cần lưu giữ, duy trì.*Biểu diễn các thuợc tínhChỉ ra tên, kiểu và giá trị mặc định nếu cĩattributeName : Type = DefaultTuân theo quy ước đặt tên của ngơn ngữ cài đặt và của dự án.Kiểu (type) nên là kiểu dữ liệu cơ bản trong ngơn ngữ thực thiKiểu dữ liệu cĩ sẵn, kiểu dữ liệu người dùng định nghĩa, hoặc lớp tự định nghĩa.Xác định phạm vi truy cậpPublic: +Private: -Protected: # *Các thuộc tính dẫn xuất (derived)Thuộc tính dẫn xuất là gì?Là thuộc tính cĩ giá trị cĩ thể được tính tốn dựa trên các thuộc tính khác.Khi nào thì sử dụng?Khi khơng đủ thời gian để tính tốn lại giá trị mỗi khi cầnKhi bạn phải cân đối giữa hiệu năng thời gian chạy với bộ nhớ yêu cầu.* name address nextAvailID : int studentID : int dateOfBirth : DateStudentVí dụ: Define AttributesRegistrationController0..1ICourseCatalogSystem>ScheduleCourseOffering number : String = “100” startTime : Time endTime : Time day : String /numStudents : int = ()- semester : Semester0..10..10..1+ registrant+ currentSchedule0..*0..40..20..*+ alternateCourses10..*+ primaryCourses**Nội dungXác định các thao tác (Operation)Xác định phương thức (Method)Xác định các liên kết (Association) Xác định các thuợc tính (Attribute)Xác định phụ thuợc (Dependency)Xác định tổng quát hĩa (Generalization)*Một phụ thuộc là gì?Là mối quan hệ ngữ nghĩa giữa hai đối tượng, trong đĩ một sự thay đổi trong supplier cĩ thể gây ra thay đổi cho client.Mục đíchXác định khi các mối quan hệ cấu trúc (association hoặc aggregation) khơng cần đến.Cần xem xét:Cái gì làm cho supplier cĩ thể được nhìn thấy client?5. Xác định phụ thuợc (Dependency)SupplierClient*Liên kết và Phụ thuộcKết hợp là mối quan hệ cấu trúcPhụ thuộc là mối quan hệ phi-cấu trúcĐể các đối tượng cĩ thể “biết lẫn nhau”, chúng phải được nhìn thấyLocal variable referenceParameter referenceGlobal referenceField referenceAssociationDependencySupplier2ClientSupplier1*Kết hợp và Phụ thuộcMột thể hiện của một kết hợp là một liên kếtTất cả các liên kết đều trở thành kết hợp trừ khi chúng cĩ phạm vi truy cập là tồn cục, cục bộ hoặc tham số.Các mối quan hệ phụ thuộc vào ngữ cảnhPhụ thuộc là các liên kết tạm thời với:Một khoảng thời gian giới hạnMột mối quan hệ độc lập với ngữ cảnhMột mối quan hệ tổng hợpA dependency is a secondary type of relationship in that it doesn't tellyou much about the relationship. For details you need to consult thecollaborations.*Phạm vi biến địa phươngThao tác op1() chứa một biến địa phương của ClassBClassA+ op1 ( )ClassB*Phạm vi tham sốThể hiện của ClassB được truyền tham số đến thể hiện của ClassA.ClassA+ op1 ( [in] aParam : ClassB )ClassB*Phạm vi tồn cụcThể hiện ClassUtility cĩ thể nhìn thấy vì nĩ là tồn cụcClassA+ op1 ( )ClassB+ utilityOp ( )*Các mối quan hệ lâu dài – Association (field visibility)Các mối quan hệ tạm thời — DependencyNhiều đối tượng chia sẻ cùng một thể hiệnTruyền thể hiện như một tham số (parameter visibility)Làm cho thể hiện cĩ phạm vi tồn cục cĩ kiểm sốt (global visibility)Nhiều đối tượng khơng chia sẻ cùng một thể hiện (local visibility)Xác định các phụ thuộc*Ví dụ: Define Dependencies (before)>ICourseCatalogSystem+ getCourseOfferings ( [in] forSemester : Semester) : CourseOfferingListStudent- name- address- StudentID : int+ addSchedule ( [in] aSchedule : Schedule )+ getSchedule ( [in] forSemester : Semester ) : Schedule+ hasPrerequisites ( [in] forCourseOffering : CourseOffering ) : boolean# passed ( [in] aCourseOffering : CourseOffering ) : booleanRegistrationController+ // submit schedule ()+ // save schedule ()+ // create schedule with offerings ()+ // get course offerings ()0..10..1+ registrant 0..*1+ courseCatalogSchedule- semester : Semester+ submit ()+ //save ()# any conflicts? ()+ //create with offerings()0..*10..10..1+ currentScheduleCourseOffering- number : String = "100"- startTime : Time- endTime : Time- day : String+ addStudent ( [in] aStudentSchedule : Schedule)+ removeStudent ( [in] aStudentSchedule : Schedule)+ new ()+ setData ()0..*0..4+ primaryCourses0..*0..2alternateCourses*Ví dụ: Define Dependencies (after)>ICourseCatalogSystem+ getCourseOfferings ( [in] forSemester : Semester) : CourseOfferingListRegistrationController+ // submit schedule ()+ // save schedule ()+ // create schedule with offerings ()+ // get course offerings ()0..1+ registrant Schedule- semester : Semester+ submit ()+ //save ()# any conflicts? ()+ //create with offerings()0..*0..10..1+ currentScheduleCourseOffering- number : String = "100"- startTime : Time- endTime : Time- day : String+ addStudent ( [in] aStudentSchedule : Schedule)+ removeStudent ( [in] aStudentSchedule : Schedule)+ new ()+ setData ()0..40..*0..2alternateCoursesGlobal visibilityParameter visibilityField visibilityField visibilityStudent- name- address- StudentID : int+ addSchedule ( [in] aSchedule : Schedule )+ getSchedule ( [in] forSemester : Semester ) : Schedule+ hasPrerequisites ( [in] forCourseOffering : CourseOffering ) : boolean# passed ( [in] aCourseOffering : CourseOffering ) : boolean0..110..*+ primaryCourses**Nội dungXác định các thao tác (Operation)Xác định phương thức (Method)Xác định các liên kết (Association) Xác định các thuợc tính (Attribute)Xác định phụ thuợc (Dependency)Xác định tổng quát hĩa (Generalization)*6. GeneralizationOne class shares the structure and/or behavior of one or more classes“Is a kind of” relationshipIn Analysis, use sparinglySuperclass (Parent) (Ancestor)Generalization RelationshipSubclasses (Child) (Descendants)Account+ balance+ name+ number+ withdraw ()+ createStatement ()CheckingSavings+ getInterest ()*There are no direct instances of AnimalLionTigerAnimal+ communicate ()+ communicate ()+ communicate ()Abstract and Concrete ClassesAbstract classes cannot have any objectsConcrete classes can have objectsAll objects are either lions or tigersAbstract classAbstract operationCommunicationDiscriminatorName clashes on attributes or operationsRepeated inheritanceMultiple Inheritance: ProblemsResolution of these problems is implementation-dependent.SomeClassBirdAnimal+ color+ getColor ()FlyingThing+ color+ getColor ()BirdAnimal+ color+ getColor ()FlyingThing+ color+ getColor ()**Generalization ConstraintsComplete End of the inheritance tree Incomplete Inheritance tree may be extendedDisjointSubclasses mutually exclusiveDoesn’t support multiple inheritanceOverlapping Subclasses are not mutually exclusiveSupports multiple inheritance*Example: Generalization ConstraintsAssetReal EstateBank AccountSecuritySavingsCheckingStockBond{disjoint}{disjoint,complete}{disjoint}End of inheritance hierarchyMultiple Inheritance not supported*Example: Generalization Constraints (cont.)VehicleAmphibiousVehicleWaterVehicleLandVehicle{overlapping}MultipleinheritancesupportedIs this correct?Generalization vs. AggregationGeneralization and aggregation are often confusedGeneralization represents an “is a” or “kind-of” relationshipAggregation represents a “part-of” relationshipWindowWindowWithScrollbarScrollbar**Do these classes follow the “is a” style of programming?Generalization: Share Common Properties and BehaviorFollows the “is a” style of programmingClass substitutabilityList+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)StackLionTigerAnimal+ communicate ()+ communicate ()+ communicate ()*Generalization: Share Common Properties and Behavior (cont.)List+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)StackLionTigerAnimal+ communicate ()+ communicate ()+ communicate ()Generalization: Share Implementation: FactoringSupports the reuse of the implementation of another classCannot be used if the class you want to “reuse” cannot be changedList+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)StackSequentialContainerList+ insertTop ([in] item)+ removeTop ()Stack+ insertBottom ([in] item)+ removeBottom ()+ insert ([in] item, [in] position)*Generalization Alternative: Share Implementation: DelegationSupports the reuse of the implementation of another classCan be used if the class you want to “reuse” cannot be changedList+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)StackListStack+ insertBottom ([in] item)+ removeBottom ()+ insert ([in] item, [in] position)11*push() and pop() can access methods of List but instances of Stack cannotImplementation InheritanceAncestor public operations, attributes, and relationships are NOT visible to clients of descendent class instancesDescendent class must define all access to ancestor operations, attributes, and relationshipsList+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()Stack>+ push (item)+ pop ()**Manufacturer AManufacturer BManufacturer COO Principle: EncapsulationReview: What Is Polymorphism?The ability to hide many different implementations behind a single interfaceRemote Control*Generalization: Implement PolymorphismWithout Polymorphismif animal = “Lion” thenLion communicateelse if animal = “Tiger” thenTiger communicateendWith PolymorphismAnimal communicateLionTigerAnimal+ communicate ()+ communicate ()+ communicate ()*Polymorphism: Use of Interfaces vs. GeneralizationInterfaces support implementation-independent representation of polymorphismRealization relationships can cross generalization hierarchiesInterfaces are pure specifications, no behaviorAbstract base class may define attributes and associationsInterfaces are totally independent of inheritanceGeneralization is used to re-use implementationsInterfaces are used to re-use behavioral specificationsGeneralization provides a way to implement polymorphismPolymorphism via Generalization Design DecisionsProvide interface only to descendant classes?Design ancestor as an abstract classAll methods are provided by descendent classesProvide interface and default behavior to descendent classes?Design ancestor as a concrete class with a default methodAllow polymorphic operationsProvide interface and mandatory behavior to descendent classes?Design ancestor as a concrete classDo not allow polymorphic operations**Ví dụ Generalization*Biểu đồ lớp đầy đủ*Tổng kếtCác mơ hình thiết kế được xây dựng trực tiếp từ các mơ hình phân tíchLà hình thức chi tiết hĩa từ các lớp trừu tượng hĩa trong mơ hình phân tíchÁnh xạ 1-1 cho những lớp phân tích đơn giảnÁnhxạ thành nhiều lớp thiết kế nếu lớp phân tích đĩ quá phức tạpLớp phân tích cĩ mức độ phức tạp cao cĩ thể được phát triển thành hệ thống con (subsystem)Sử dụng các giao diệnĐảm bảo hệ thống con cĩ tính độc lập tối đa với các thành phần cịn lạiTìm cách sử dụng lại các hệ thống con, gĩi hoặc các thư viện cĩ sẵn

Các file đính kèm theo tài liệu này:

  • ppttailieu.ppt