Chào các em, chủ đề hôm nay của anh sẽ bàn về Design PatternAbstract Factory ? Khi nào chúng ta sẽ dùng nó trong lập trình.
1- Abstract Factory Là gì ?
2- Khi nào nên dùng Abstract Factory
Khi chúng ta muốn tạo một họ sản phẩm (FAMILY PRODUCT) liên quan đến . Ví dụ như mình muốn tạo một đối tượng là Xe hơi Toyota. Thì các thành phần cấu thành xe hơi Toyota phải từ chính Toyota mà ra . Ví dụ Xe hơi Toyota thì Tay laí Toyota , lốp xe Toyota. Khung xe được sản xuất tại Toyota. Nói tóm lại các thành phần cấu tạo nên chiếc xe phải từ Toyota mà ra cả.
3- Abstract Factory UML
4- Abstract Factory Button và Checkbox
5- Xây dựng ứng dụng
Trong ví dụ trên về xây dựng một ứng dụng Paint(vẽ) gồm có các phần như tạo button (nút) và tạo checkbox. Ứng với hệ điều hành Windows thì nó sẽ tạo ra bộ sản phẩm nút và check box của Windows. Nếu là hệ điều hành Mac thì nó sẽ tạo ra một bộ sản phẩm checkbox và nút bấm cho Mac. Như vậy phụ thuộc vào hệ điều hành mà mình đang dùng thì mình sẽ tạo các các thành phần tương ứng cho hệ điều hành đó. Không có trường hợp hệ điều hành Windows mà mình có thể tạo ra nút bấm Windows và Checkbox của Mac được.
GUI Factory trong thiết kế tương ứng với Abstract Facetory . Gui Factory gồm các method để tạo ra một sản phẩm . Mỗi method là tạo ra một phần của sản phẩm như button hay checkbox
MacOSFactory chính là ConcreatFactory1 : gồm các phương thức abstract để tạo ra button và checkbox tương ứng với Mac
WinOSFactory chính là ConcreatFactory 2 : gồm các phương thức abstract để tạo ra button và checkbox tương ứng với
Interface Button : Chính là AbstractProductA . Gồm phương thức vẽ để mô tả cho hành động vẽ button là vẽ như thế nào với mỗi hệ điều hành Windows hay Mac
Interface Checkbox : Chính là AbstractProductB . Gồm phương thức vẽ để mô tả cho hành động vẽ checkbox là vẽ như thế nào với mỗi hệ điều hành Windows hay Mac
Windows Button : là ProductA2 . Đây là lớp định nghĩa Windows button là gì
Mac Button : là ProductA1 . Đây là lớp định nghĩa Mac button là gì
Windows Checkbox : là ProductB2 . Đây là lớp định nghĩa Windows checkbox là gì
Mac Checkbox : là Product B1 . Đây là lớp định nghĩa Mac checkbox là gì
Application : Sử dụng Gui Factory và 2 lớp Interface Button và Interface Checkbox để tạo ra họ sản phẩm . Nếu windows thì button và checkbox Windows và ngược lại
publicclassDemo{/**
* Application picks the factory type and creates it in run time (usually at
* initialization stage), depending on the configuration or environment
* variables.
*/privatestaticApplicationconfigureApplication(){Applicationapp;GUIFactoryfactory;StringosName=System.getProperty("os.name").toLowerCase();if(osName.contains("mac")){factory=newMacOSFactory();app=newApplication(factory);}else{factory=newWindowsFactory();app=newApplication(factory);}returnapp;}publicstaticvoidmain(String[]args){Applicationapp=configureApplication();app.paint();}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* Factory users don't care which concrete factory they use since they work with
* factories and products through abstract interfaces.
*/publicclassApplication{privateButtonbutton;privateCheckboxcheckbox;publicApplication(GUIFactoryfactory){button=factory.createButton();checkbox=factory.createCheckbox();}publicvoidpaint(){button.paint();checkbox.paint();}}
1
2
3
4
5
6
7
/**
* Abstract factory knows about all (abstract) product types.
*/publicinterfaceGUIFactory{ButtoncreateButton();CheckboxcreateCheckbox();}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Each concrete factory extends basic factory and responsible for creating
* products of a single variety.
*/publicclassMacOSFactoryimplementsGUIFactory{@OverridepublicButtoncreateButton(){returnnewMacOSButton();}@OverridepublicCheckboxcreateCheckbox(){returnnewMacOSCheckbox();}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Each concrete factory extends basic factory and responsible for creating
* products of a single variety.
*/publicclassWindowsFactoryimplementsGUIFactory{@OverridepublicButtoncreateButton(){returnnewWindowsButton();}@OverridepublicCheckboxcreateCheckbox(){returnnewWindowsCheckbox();}}
1
2
3
4
5
6
/**
* Checkboxes is the second product family. It has the same variants as buttons.
*/publicinterfaceCheckbox{voidpaint();}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* All products families have the same varieties (MacOS/Windows).
*
* This is a variant of a checkbox.
*/publicclassMacOSCheckboximplementsCheckbox{@Overridepublicvoidpaint(){System.out.println("You have created MacOSCheckbox.");}}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* All products families have the same varieties (MacOS/Windows).
*
* This is another variant of a checkbox.
*/publicclassWindowsCheckboximplementsCheckbox{@Overridepublicvoidpaint(){System.out.println("You have created WindowsCheckbox.");}}
1
2
3
4
5
6
7
8
9
10
/**
* Abstract Factory assumes that you have several families of products,
* structured into separate class hierarchies (Button/Checkbox). All products of
* the same family have the common interface.
*
* This is the common interface for buttons family.
*/publicinterfaceButton{voidpaint();}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* All products families have the same varieties (MacOS/Windows).
*
* This is a MacOS variant of a button.
*/publicclassMacOSButtonimplementsButton{@Overridepublicvoidpaint(){System.out.println("You have created MacOSButton.");}}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* All products families have the same varieties (MacOS/Windows).
*
* This is another variant of a button.
*/publicclassWindowsButtonimplementsButton{@Overridepublicvoidpaint(){System.out.println("You have created WindowsButton.");}}
publicclassDemo{/**
* Application picks the factory type and creates it in run time (usually at
* initialization stage), depending on the configuration or environment
* variables.
*/privatestaticApplicationconfigureApplication(){Applicationapp;GUIFactoryfactory;StringosName=System.getProperty("os.name").toLowerCase();if(osName.contains("mac")){factory=newMacOSFactory();app=newApplication(factory);}else{factory=newWindowsFactory();app=newApplication(factory);}returnapp;}publicstaticvoidmain(String[]args){Applicationapp=configureApplication();app.paint();}}
Mọi người hãy Subscribe kênh youtube dưới đây nhé để cập nhật các video mới nhất về kỹ thuật và kỹ năng mềm