1. Trang chủ
  2. » Luận Văn - Báo Cáo

tiểu luận mẫu thiết kê

17 0 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề DEALING WITH GENERALIZATION
Tác giả HÔ NHẬT DUY, NGUYÊN NGỌC TRUNG HIÉU, NGUYÊN THANH HÀ
Người hướng dẫn NGUYÊN THANH PHƯỚC
Trường học ĐẠI HỌC TÔN ĐỨC THẮNG
Chuyên ngành Software Engineering
Thể loại Tieu Luan
Năm xuất bản 2023
Thành phố THANH PHO HO CHi MINH
Định dạng
Số trang 17
Dung lượng 1,33 MB

Nội dung

Mechanics: ¢ Define a superclass constructor e Using the Put up fiedls to move the common code from to subclass to the superclass ¢ Call the sperclass’s constructor when define a subclas

Trang 1

NHOM 14

ĐẠI HỌC TÔN ĐỨC THẮNG

TON DUC THANG UNIVERSITY

TIEU LUAN

MAU THIET KE

Giảng viên hướng dẫn: NGUYÊN THANH PHƯỚC

Sinh viên thực hiện:

4 HÔ NHẬT DUY - 519H0285

2 NGUYÊN NGỌC TRUNG HIÉU- 519H0290

3 NGUYÊN THANH HÀ - 518H0491

Trang 2

NHOM 14

THANH PHO HO CHi MINH, NAM 2023

Table of Content

S

THÀNH PHÓ HỒ CHÍ MINH, NĂM 202 Q-GẠ TH LH HH HH1 TH g1 ngu nen 2 CHAP 11 DEALING WITH GENERALIZATION - CS HH SH HH kg sey 2

REPLACE INHERITANCE WITH DELEGATION 13 REPLACE DELEGATION WITH INHERITANCE 15

xa (zìi- 0n n 15

Trang 3

NHOM 14

CHAP 11 DEALING WITH GENERALIZATION

PULL UP FIELD

Problems: When you create different subcategories, sometimes they have the same features These features may have different names but they serve a similar purpose To avoid repetition, you need to check how these features are used and see if they can be combined By doing this, you can remove the duplicated features and simplify the code This approach helps you to generalize the behavior and reduce repetition in the subclasses

Two subclasses have the same field

Move the field to the superclass

Employee

[ l [ ]

Salesman Engineer

Employee

==]

Solution: move it into superclass

Mechanics: (steps)

e Inspect/search all uses of the candidate fields to find out which of them are used in the same way

e Rename them to make sure it correct with the superclass

¢ Compile and test

Trang 4

NHOM 14

Design Pattern with Application Method:

¢ Template Method Pattern

e Factory Method Pattern

e Decorator Pattern

¢ Strategy Pattern

PULL UP METHODS

Problems: Having duplicate methods in your code can create problems in the future as it increases the chances of bugs and inconsistencies It's not always easy to spot duplicates, but if you find two methods with the same body, it's a good idea to use the "Pull Up Method” technique This means combining the duplicate code into one method that can be used by both classes Before doing this, it's important to make sure that the methods behave the same way and that you have tested them thoroughly If there are differences, you may need to adjust the code or tests In some cases, you may need to parameterize the methods first before generalizing them

Solution: rename and move it to superclass

Mechanics: (steps)

® Inspect/search all methods as same as work and result

¢ Create a new method in the superclass with the copy of the body of duplicate methods, change the signatures of it in needs

e Delete methods on subclass

Trang 5

NHOM 14

¢ Complie and test

¢ Continue delete and complie and test until the method in superclass

is only

e Review code for change in needs

Design Pattern with Application Method:

¢ Template Method Pattern

¢ Strategy Pattern

¢ Command Pattern

PULL UP CONTRUCTOR BODY

Problems: Constructors have limitations compared to normal methods, making it challenging to manipulate them When dealing with subclasses, the common behavior is often the construction itself, and this cannot be pulled up into the superclass using inheritance A solution is to create a superclass constructor that can be called by the subclasses, and in many cases, this constructor is the entire body of the method If refactoring the code becomes too complex, another alternative is to consider using the

"Replace Constructor with Factory Method" pattern instead

Trang 6

NHOM 14

constructor

|

fF \

Solution: cretae a superclass constructor then call it (by super() method in

java)

Mechanics:

¢ Define a superclass constructor

e Using the Put up fiedls to move the common code from to subclass

to the superclass

¢ Call the sperclass’s constructor when define a subclass constructor

¢ Complie and test Design Pattern with Application Method:

¢ Template Method Pattern

e Factory Method Pattern

e Abstract Method Pattern

Trang 7

NHOM 14

PULL DOWN METHOD

Problems: Pull Down Method is the opposite of Pull Up Method | use it

when | need to move behavior from a superclass to a specific subclass,

usually because it makes sense only there You often do this when you

use Extract Subclass

Employee

Employee

= =>

Sj) Se

Solution: put these method down into the subclass

Mechanics:

Declare a method in all subcalss that need the method(s), then copy the body to each subclass

Remove method from superclass and orther class that doesn’t need Complie and test

If too many subclass need, using Pull up method to create a sub- superclass for those subclass for inheritance

Design Pattern with Application Method:

Template Method Pattern Strategy Pattern Decorator Pattern

Trang 8

NHOM 14

PULL DOWN FIELD

Problem: Push Down Field is the opposite of Pull Up Field Use it when you don't need a field in the superclass but only in a subclass

Salesman Engineer | Engineer

i |

Solution: move the fileds to those subclass

Mechanics:

¢ Declare the fields in all subcalss that need the method(s), then copy the body to each subclass

¢ Remove fields from superclass and orther class that doesn’t need

¢ Complie and test

e If too many subclass need, using Pull up fields to create a sub- superclass for those subclass for inheritance

Design Pattern with Application Method:

¢ Template Method Pattern

¢ Strategy Pattern

e Decorator Pattern

Trang 9

NHOM 14

EXTRACT SUBCLASS

Problem: many classes/objects have the similar features or fields => hard

to change in furture and maybe conflit together

Job Item

getTotalPrice

getTotalPrice =":

getEmployee

Labor Item

getUnitPrice getEmployee

Solution: create and move the duplicate or common features into a new superclass

Mechanics:

se Create an abstract superclass

e Make old classes become subclasses of that superclass

e Using Pull up fields, Pull up method and Pull up constructor body to move common need to superclass

e Find and replace the code to new class in need

e Check each subclass to make sure it run

Design Pattern with Application Method:

Trang 10

NHOM 14

e Abstract Factory Method Pattern

e Factory Method Pattern

¢ Strategy Method Pattern

¢ Command Pattern

EXTRACT INTERFACE

Problem: a group of client have the similar subset and you want to make sure the class create in furture must have the features similar to the others

of group

«interface» Billable

getRate Employee hasSpecialSkill

hasSpecialSkill => |

getName !

getDepartment l

Employee

getRate

hasSpec:alSkill getName getDepartment

Solution: create an interface superclass contain the subset

Mechanics:

10

Trang 11

NHOM 14

¢ Create an interface superclass

e Declare the common operations in that interface

e Make other class that contain the common operations implement the interface superclass to make them become subclasses

¢ Change type of declare in the subclass

Design Pattern with Application Method:

¢ Command Pattern

¢ Proxy Pattern

¢ Stratergy Pattern

COLLAPSE HIERARCHI

Problem: subclass is as same as with the superclass

Job Item

getTotalPrice Job Item getUnitPrice

getTotalPrice =:

getUnitPrice 7

getEmployee

Labor ltem

qetUnitPrice getEmployee

11

Trang 12

NHOM 14

Solution: merge them together

Mechanics:

¢ Decide which class you want to remove

e lf you choose superclas, then use the Pull down method and Pull down fields to get rid of the superclass The oposite, use Pull up method and Pull up fields to merge them

e Replace all the other class which using one of them

¢ Delete the non-use class

Design Pattern with Application Method:

¢ Template Method Pattern

e Adapter Pattern

e Decorator Pattern

¢ Bridge Pattern

FROM TEMPLATE METHOD

Problem: some subclass have the same steps in method

12

Trang 13

NHOM 14

ite _ doubie base = _unÑs ' _rate " 0.5;

doubie tax = base " Site TAX_ RATE " 0.2;

return base + tax;

4

4

⁄ ite Residential Site Lifeline Site ⁄

getBillableAmount Oo

getBillableAmount ©

~ ~ ~

double base = _units " _ rate;

double tax = base * Ste TAX_RATE;

NV return base + tax;

Site

getBillableAmount O _ getBaseAmoun ee gelTaxAmount es

return getBaseAmount() + getTaxAmount();

Residential Site LifelineSite

getBaseAmount getBaseAmount

getTaxAmount getTaxAmount

Solution: put them into a template class

Mechanics:

se Slpit each step in the algorithms into function which return the result

or do the similar task with the old algorithm

13

Trang 14

NHOM 14

e Use Pull up method to move the similar method into the superclass and then remove them in subclas

e For the non-similar method, use the Rename method to make sure they have the same call but handle can be different

¢ Create an abstract method In superclass with the same signatures of the non-similar methoids

¢ Complie and test for each subclass

Design Pattern with Application Method:

e Factory Method Pattern

¢ Strategy Pattern

e Visitor Pattern

¢ Command Pattern

e Interpreter Pattern

REPLACE INHERITANCE WITH DELEGATION

Problem: subclass may not want or need to override all the method of superclass or the superclass hay too many method that subclass don’t need to use

14

Trang 15

NHOM 14

Stack 1 Vector

isEmpty Q isEmpty

=>

IN

Stack return _vector.isEmpty()

Solution: create a private field and put the superclass into it, superclass will

become an object class

Mechanics:

¢ Create a private class in the subclass which extends the superclass

¢ Create a private fields by calling the class have just created like an object

¢ Implement method of the superclass in the private class

e Remove the inheritance declaration in all the method in subclass and replace by calling the object method

¢ Complie and test

Design Pattern with Application Method:

¢ Strategy Pattern

¢ Decorator Pattern

e Adapter Pattern

¢ Proxy Pattern

15

Trang 16

NHOM 14

REPLACE DELEGATION WITH INHERITANCE

Problem: the subclass want to use all the method in superclass or the superclass just contain some simple methods

Solution: make the subclass become the delegate inheritance class and remove the delegatting method

Mechanics:

e Declare the subclass to inheritance the superclass

¢ Overide the abstract method or place the object in a field reference

in need

¢ Delee method which work as delegation method, use Rename method in need

e Replace reference delegation fields with the current object

¢ Remove all delegation fields, complie and test

Design Pattern with Application Method:

¢ Strategy Pattern

¢ Decorator Pattern

e Adapter Pattern

¢ Proxy Pattern

REFERENCE

[GITHUB chứa file example_ code]

[1] https:/github.com/Dinowannacode/Design_ Pattern_ group14.git

Trang 17

NHOM 14

[Rubric nhém ty danh gia]

[2]https://docs.google.com/spreadsheets/d/

1BFgzu3rwMRnSPdillPmxiPNzGUwCgzCrLiZwxS2Jgq20/edit? usp=sharing

[Sách tham khảo]

[3]2002 - Refactoring: Improving the Design of Existing Code

17

Ngày đăng: 30/09/2024, 18:03

w