Bridge Design Pattern

You might also like

You are on page 1of 33

TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP.

HỒ CHÍ MINH
KHOA CÔNG NGHỆ THÔNG TIN

SEMINAR
BRIDGE DESIGN PATTERN
GVHD: ThS.NGUYỄN TRẦN THI VĂN
SVTH: Phan Văn Phước Thịnh 16110474
Nguyễn Thanh Tân 16110541
Phạm Ngọc Diêu 16110291
Nhóm 8: Brigde Design Pattern Huỳnh Lê Hữu Hưng 16110347
Ngày báo cáo: 22/04/2019 Nguyễn Thanh Liêm 16110375
1
Nội dung thuyết trình
Tổng quan về Bridge Pattern

Bridge Design Pattern

Demo

Các mẫu liên quan

Tổng kết
I- TỔNG QUAN
3
1 . Khái niệm
 Bridge Pattern là một trong những Pattern
thuộc nhóm cấu trúc (Structural Pattern).
 Ý tưởng của Brigde là tách tính trừu tượng
(abstraction) ra khỏi tính hiện thực
(implementation) của nó.
 Mục đích dễ dàng chỉnh sửa hoặc thay thế
mà không làm ảnh hưởng đến những nơi
có sử dụng lớp ban đầu

GROUP 8 BRIDGE DESIGN PATTERN


I- TỔNG QUAN
4
3
2 . Các thành phần của Bridge
class Logical View

«interface»
Abstraction Implementor
- implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

 Client: đại diện cho khách hàng sử dụng các chức năng thông
qua Abstraction.
GROUP 8 BRIDGE DESIGN PATTERN
I- TỔNG QUAN
5
3
2 . Các thành phần của Bridge
class Logical View

«interface»
Abstraction Implementor
- implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

 Abstraction : định ra một abstract interface quản lý việc tham


chiếu đến đối tượng hiện thực cụ thể (Implementor).
GROUP 8 BRIDGE DESIGN PATTERN
I- TỔNG QUAN
6
3
2 . Các thành phần của Bridge
class Logical View

«interface»
Abstraction Implementor
- implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

 Refined Abstraction (AbstractionImpl) : hiện thực các phương


thức đã được định ra trong Abstraction bằng cách sử dụng
một tham chiếu đến một đối tượng của Implementor.
GROUP 8 BRIDGE DESIGN PATTERN
I- TỔNG QUAN
7
3
2 . Các thành phần của Bridge
class Logical View

«interface»
Abstraction Implementor
- implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

 Implementor : định ra các interface cho các lớp hiện thực.


Thường là interface định ra các tác vụ nào đó của Abstraction.
GROUP 8 BRIDGE DESIGN PATTERN
I- TỔNG QUAN
8
3
2 . Các thành phần của Bridge
class Logical View

«interface»
Abstraction Implementor
- implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

 ConcreteImplementor : hiện thực Implementor interface.


GROUP 8 BRIDGE DESIGN PATTERN
9
I- TỔNG QUAN
3. Tại sao lại nên dùng Bridge Pattern ?
 Thông thường các ứng dụng lớn thường yêu
cầu chúng ta thêm module cho ứng dụng có
sẵn nhưng không được sửa đổi framework/ứng
dụng có sẵn vì các framework/ứng dụng đó có
thể được công ty nâng cấp lên version mới.
Bridge Pattern sẽ giúp chúng ta trong trường
hợp này.

SlidePATTERN
GROUP 8 BRIDGE DESIGN 4
II- BRIDGE DESIGN PATTERN 10
1. ĐẶT VẤN ĐỀ

SlidePATTERN
GROUP 8 BRIDGE DESIGN 5
II- BRIDGE DESIGN PATTERN 11
1. ĐẶT VẤN ĐỀ

SlidePATTERN
GROUP 8 BRIDGE DESIGN 5
II- BRIDE DESIGN PATTERN 12
2. Đề xuất cách xây dựng Bridge
BƯỚC 1: XÁC ĐỊNH NHỮNG THỰC THI SẼ BỊ THAY ĐỔI

RA LỆNH

THỰC THI

SlidePATTERN
GROUP 8 BRIDGE DESIGN 6
II- BRIDE DESIGN PATTERN 13
2. Đề xuất cách xây dựng Bridge
BƯỚC 1:class
XÁC ĐỊNH
Logical NHỮNG THỰC THI SẼ BỊ THAY ĐỔI
View

Remote «interface»
Device
+ ChannelDown(): void
+ ChannelUp(): void + Disable(): void
+ Remote(Device) + Enable(): void
+ TogglePower(): void + GetChannel(): int
+ VolumeDown(): void + GetVolume(): int
+ VolumeUp(): void + isEnable(): bool
+ SetChannel(int): void
+ SetVolume(int): void

SlidePATTERN
GROUP 8 BRIDGE DESIGN 7
II- BRIDE DESIGN PATTERN 14
2. Đề xuất cách xây dựng Bridge
BƯỚC 2: DIỄN TẢ SỰ THAY ĐỔI CỦA CHÚNG TRÊN CLASS

class Logical View

«interface»
Remote
Device
+ ChannelDown(): void
+ Disable(): void
+ ChannelUp(): void
+ Enable(): void
+ Remote(Device)
+ GetChannel(): int
+ TogglePower(): void
+ GetVolume(): int
+ VolumeDown(): void
+ isEnable(): bool
+ VolumeUp(): void
+ SetChannel(int): void
+ SetVolume(int): void

Remote1 Remote2 Remote3 TV Radio DVD Player

SlidePATTERN
GROUP 8 BRIDGE DESIGN 8
II- BRIDE DESIGN PATTERN 15
2. Đề xuất cách xây dựng Bridge
BƯỚC
class 3:
Logical View KẾT CÁC MỐI LIÊN HỆ GỮA CÁC CLASS

Remote «interface»
Device
- TheDevice: Device
+ Disable(): void
+ ChannelDown(): void + Enable(): void
+ ChannelUp(): void + GetChannel(): int
+ Remote(Device) + GetVolume(): int
+ TogglePower(): void + isEnable(): bool
+ VolumeDown(): void + SetChannel(int): void
+ VolumeUp(): void + SetVolume(int): void

Remote1 Remote2 Remote3


TV Radio DVD Player

GROUP 8 BRIDGE DESIGN PATTERN


II- BRIDE DESIGN PATTERN 16
2. Đề xuất cách xây dựng Bridge
BƯỚC 3: MỞ RỘNG THIẾT KẾ
class Logical View

Remote «interface»
Device
# TheDevice: Device
+ Disable(): void
+ ChannelDown(): void + Enable(): void
+ ChannelUp(): void + GetChannel(): int
+ Remote(Device) + GetVolume(): int
+ TogglePower(): void + isEnable(): bool
+ VolumeDown(): void + SetChannel(int): void
+ VolumeUp(): void + SetVolume(int): void

TV Radio
- channel: int = 0 - FM: int = 0
RemoteBasic RemoteAdvanceMute - power: bool = false - power: bool = false
+ RemoteBasic(Device) - volume: int = 0 - volume: int = 0
+ Mute(): void
+ RemoteAdvanceMute(Device) + Disable(): void + Disable(): void
+ Enable(): void + Enable(): void
+ GetChannel(): int + GetChannel(): int
+ GetVolume(): int + GetVolume(): int
+ isEnable(): bool + isEnable(): bool
+ SetChannel(int): void + Radio()
+ SetVolume(int): void + SetChannel(int): void
+ TV() + SetVolume(int): void
16
SlidePATTERN
GROUP 8 BRIDGE DESIGN 10
II- BRIDE DESIGN PATTERN 17

17
SlidePATTERN
GROUP 8 BRIDGE DESIGN 11
II- BRIDE DESIGN PATTERN 18

18
SlidePATTERN
GROUP 8 BRIDGE DESIGN 12
II- BRIDE DESIGN PATTERN 19

19
SlidePATTERN
GROUP 8 BRIDGE DESIGN 13
II- BRIDE DESIGN PATTERN 20
class CẤU TRÚC
Logical View CỦA MẨU 1:

«interface»
Abstraction Implementor
- implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

20
SlidePATTERN
GROUP 8 BRIDGE DESIGN 14
II- BRIDE DESIGN PATTERN 21
CẤU
class Logical ViewTRÚC CỦA MẨU 2:

«interface»
Abstraction Implementor
# implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

21
SlidePATTERN
GROUP 8 BRIDGE DESIGN 15
II- BRIDE DESIGN PATTERN 22
•Abstraction
• Định ra một Abstractioin
• Quản lý việc tham chiếu đến đối tượng hiện thực cụ thể
•Refined Abstraction
• Mở rộng INTERFACE mà đã được định ra trong Abstraction
•Implementor
• Định ra các Interfacecho các lớp hiện thực.
•ConcreteImplementor
• hiện thực Implementor Interface
22
SlidePATTERN
GROUP 8 BRIDGE DESIGN 16
III - DEMO 23
lass DemoBridge_Paint
Abstraction
PenShapeBase

- drawObject: DrawObject Implementation


# height: int
# width: int DrawObject
# x: int
-drawObject
# y: int # drawPen: Pen
+ draw(Graphics): void + draw(Graphics, int, int, int, int): void
+ PenShapeBase() + DrawObject()
+ setLoacation(int, int): void
+ setSize(int, int): void
«property»
+ Draw(): DrawObject

PenCircle PenLine PenRect Draw Circle Draw Line Draw Rect

+ draw(Graphics): void + draw(Graphics): void + draw(Graphics): void + draw(Graphics, int, int, int, int): void + draw(Graphics, int, int, int, int): void + draw(Graphics, int, int, int, int): void
+ PenCircle() + PenLine() + drawWithDash(Graphics): void + DrawCircle() + DrawLine() + DrawRect()
+ PenRect() + drawWidthDash(Graphics, int, int, int, int): void

23
SlidePATTERN
GROUP 8 BRIDGE DESIGN 17
IV- CÁC DESIGN PATTERN LIÊN QUAN
24
BRIDGE

24
SlidePATTERN
GROUP 8 BRIDGE DESIGN 17
IV- CÁC DESIGN PATTERN LIÊN QUAN 25

Abstract Factory

25
SlidePATTERN
GROUP 8 BRIDGE DESIGN 19
IV- CÁC DESIGN PATTERN LIÊN QUAN
26
BUILDER

26
SlidePATTERN
GROUP 8 BRIDGE DESIGN 17
IV- CÁC DESIGN PATTERN LIÊN QUAN
27

Adapter, Strategy và State:

GROUP 8 BRIDGE DESIGN PATTERN


V- TỔNG KẾT
28
 Bridge Pattern là một trong những
Pattern thuộc nhóm cấu trúc
(Structural Pattern).
 Ý tưởng của Brigde là tách tính trừu
tượng (abstraction) ra khỏi tính hiện
thực (implementation) của nó.

28
SlidePATTERN
GROUP 8 BRIDGE DESIGN 22
V- TỔNG KẾT
29
class Logical View

«interface»
Abstraction Implementor
# implementor: Implementor + Operation(): void
+ Operation(): void

RefinedAbstraction RefinedAbstraction2 ConcreteImplementorB ConcreteImplementorA


+ Operation(): void + Operation(): void + Operation(): void + Operation(): void
+ RefinedAbstraction() + RefinedAbstraction2()

29
SlidePATTERN
GROUP 8 BRIDGE DESIGN 22
V- TỔNG KẾT
30
Ưu điểm:
 Giảm sự phụ thuộc giữa abstraction và
implemention.
 Giảm số lượng những lớp con không cần
thiết.
 Code sẽ gọn gàng hơn và kích thước ứng
dụng sẽ nhỏ hơn.
 Dễ bảo trì và mở rộng về sau hơn.
 Cho phép ẩn các chi tiết implement từ client.

30
SlidePATTERN
GROUP 8 BRIDGE DESIGN 22
V- TỔNG KẾT
31
Nhược điểm:

 Phức tạp hóa vì lạm dụng quy tắc HAS-A.

 Nhiều interfaces với vài hoặc chỉ duy nhất


một implement.

 Đa gián tiếp.

31
SlidePATTERN
GROUP 8 BRIDGE DESIGN 22
VI- TÀI LIỆU THAM KHẢO
https://refactoring.guru/design-PATTERN/bridge
https://www.c-sharpcorner.com/article/bridge-
PATTERN-in-C-Sharp/
https://www.slideshare.net/velvetflair/bridge-
design-pattern?next_slideshow=1
https://www.dofactory.com/net/bridge-design-pattern
32
Q&A

33
Slide

You might also like