Danh mục

Kiến trúc và thiết kế phần mềm

Số trang: 55      Loại file: pdf      Dung lượng: 3.94 MB      Lượt xem: 12      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: 29,000 VND Tải xuống file đầy đủ (55 trang) 0
Xem trước 6 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tài liệu trình bày chi tiết các bước hướng dẫn nhằm thiết kế hệ thống phần mềm, một số ví dụ cụ thể, đánh giá thực nghiệm hệ thống phần mềm.
Nội dung trích xuất từ tài liệu:
Kiến trúc và thiết kế phần mềmYoutube.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 và thiết kế phần mềm Hà Nội, tháng 05, năm 20211.Factory Pattern:Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTITUMLMột Factory Pattern bao gồm các thành phần cơ bản sau:  Super Class: môt supper class trong Factory Pattern có thể là 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 là Singleton hoặc cung cấp một public static method cho việc truy xuất và 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 1Tạo giao diện.Shape.javapublic interface Shape { void draw();}Bước 2Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTITTạo các lớp cụ thể triển khai cùng một giao diện.Rectangle.javapublic class Rectangle implements Shape { @Override public void draw() { System.out.println(Inside Rectangle::draw() method.); }}Square.javapublic class Square implements Shape { @Override public void draw() { System.out.println(Inside Square::draw() method.); }}Circle.javapublic class Circle implements Shape { @Override public void draw() { System.out.println(Inside Circle::draw() method.); }}Bước 3Tạ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.javapublic 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 4Sử 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.javapublic 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 5Xá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ẻ PTIT2. ABSTRACT FACTORY PATTERNUMLMột Abstract Factory Pattern bao gồm các thành phần cơ 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: là đối tượng sử dụng AbstractFactory và các AbstractProduct.Code:Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTITBước 1Tạo giao diện cho Shapes.Shape.javapublic interface Shape { void draw();}Bước 2Tạo các lớp cụ thể triển khai cùng một giao diện.RoundedRectangle.javapublic class RoundedRectangle implements Shape { @Override public void draw() { System.out.println(Inside RoundedRectangle::draw() method.); }}RoundedSquare.javapublic class RoundedSquare implements Shape { @Override public void draw() { System.out.println(Inside RoundedSquare::draw() method.); }}Rectangle.javapublic class Rectangle implements Shape { @Override public void draw() { System.out.println(Inside Rectangle::draw() method.); }}Bước 3Tạo một lớp Trừu tượng để lấy các nhà máy cho các Đối tượng Hình dạng Thường vàHình tròn.AbstractFactory.javapublic abstract class AbstractFactory {Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT abstract Shape getShape(String shapeType) ;}Bước 4Tạo các lớp Factory mở rộng AbstractFactory để tạo đối tượng của lớp cụ thể dựa trênthông ...

Tài liệu được xem nhiều: