Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT
HỌC VIỆN CÔNG NGHỆ BƯU CHÍNH VIỄN THÔNG
-----�����----
Kiến trúc thiết kế phần mềm
Nội, tháng 05, năm 2021
1.Factory Pattern:
Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT
UML
Một Factory Pattern bao gồm các thành phần bản sau:
Super Class: môt supper class trong Factory Pattern thể một interface,abstract
class hay một class thông thường.
Sub Classes: các sub class s implement các phương thức của supper class theo nghiệp
vụ riêng của nó.
Factory Class: một class chịu tránh nhiệm khởi tạo các đối tượng sub class dựa theo tham
số đầu vào. Lưu ý: lớp này Singleton hoặc cung cấp một public static method cho việc
truy xuất khởi tạo đối tượng. Factory class sử dụng if-else hoặc switch-case để xác định
class con đầu ra.
Code:
Bước 1
Tạo giao diện.
Shape.java
public interface Shape {
void draw();
}
Bước 2
Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT
Tạo các lớp cụ thể triển khai cùng một giao diện.
Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Square.java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Bước 3
Tạo một Nhà máy để tạo đối tượng của lớp cụ th dựa trên thông tin đã cho.
ShapeFactory.java
public class ShapeFactory {
//use getShape method to get object of type shape
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
}else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
Bước 4
Sử dụng Factory để lấy đối tượng của lớp cụ thể bằng cách chuyển một thông tin như
kiểu.
FactoryPatternDemo.java
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory =new ShapeFactory();
//get an object of Circle and call its draw method.
Shape shape1 =shapeFactory.getShape("CIRCLE");
//call draw method of Circle
shape1.draw();
//get an object of Rectangle and call its draw method.
Shape shape2 =shapeFactory.getShape("RECTANGLE");
//call draw method of Rectangle
shape2.draw();
//get an object of Square and call its draw method.
Shape shape3 =shapeFactory.getShape("SQUARE");
//call draw method of square
shape3.draw();
}
}
Bước 5
Xác minh kết quả đầu ra.
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT
2. ABSTRACT FACTORY PATTERN
UML
Một Abstract Factory Pattern bao gồm các thành phần bản sau:
AbstractFactory: Khai báo dạng interface hoặc abstract class chứa các phương thức để
tạo ra các đối tượng abstract.
ConcreteFactory: Xây dựng, cài đặt các phương thức tạo các đối tượng cụ thể.
AbstractProduct: Khai báo dạng interface hoặc abstract class để định nghĩa đối tượng
abstract.
Product: Cài đặt của các đối tượng cụ thể, cài đặt các phương thức được quy định tại
AbstractProduct.
Client: đối tượng sử dụng AbstractFactory các AbstractProduct.
Code: