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

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

Đ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

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.

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 Hà 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 thành phần sau:    Super Class: môt supper class Factory Pattern interface, abstract class hay class thông thường Sub Classes: sub class implement phương thức supper class theo nghiệp vụ riêng Factory Class: class chịu tránh nhiệm khởi tạo đối tượng sub class dựa theo tham số đầu vào Lưu ý: lớp Singleton cung cấp public static method cho việc truy xuất khởi tạo đối tượng Factory class sử dụng if-else switch-case để xác định class đầu Code: Bước Tạo giao diện Shape.java public interface Shape { void draw(); } Bước Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT Tạo lớp cụ thể triển khai 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 Tạo Nhà máy để tạo đối tượng lớp cụ thể dựa 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 Sử dụng Factory để lấy đối tượng lớp cụ thể cách chuyển thông tin 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 Xác minh kết đầu Inside Circle::draw() method Inside Rectangle::draw() method Inside Square::draw() method Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT ABSTRACT FACTORY PATTERN UML Một Abstract Factory Pattern bao gồm thành phần sau:      AbstractFactory: Khai báo dạng interface abstract class chứa phương thức để tạo đối tượng abstract ConcreteFactory: Xây dựng, cài đặt phương thức tạo đối tượng cụ thể AbstractProduct: Khai báo dạng interface abstract class để định nghĩa đối tượng abstract Product: Cài đặt đối tượng cụ thể, cài đặt phương thức quy định AbstractProduct Client: đối tượng sử dụng AbstractFactory AbstractProduct Code: Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT Bước Tạo giao diện cho Shapes Shape.java public interface Shape { void draw(); } Bước Tạo lớp cụ thể triển khai giao diện RoundedRectangle.java public class RoundedRectangle implements Shape { @Override public void draw() { System.out.println("Inside RoundedRectangle::draw() method."); } } RoundedSquare.java public class RoundedSquare implements Shape { @Override public void draw() { System.out.println("Inside RoundedSquare::draw() method."); } } Rectangle.java public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } Bước Tạo lớp Trừu tượng để lấy nhà máy cho Đối tượng Hình dạng Thường Hình trịn AbstractFactory.java public abstract class AbstractFactory { Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT } abstract Shape getShape(String shapeType) ; Bước Tạo lớp Factory mở rộng AbstractFactory để tạo đối tượng lớp cụ thể dựa thông tin cho ShapeFactory.java public class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } } RoundedShapeFactory.java public class RoundedShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new RoundedRectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new RoundedSquare(); } return null; } } Bước Tạo lớp Nhà máy sản xuất / nhà sản xuất để nhận nhà máy cách chuyển thông tin chẳng hạn Hình dạng FactoryProductioner.java public class FactoryProducer { public static AbstractFactory getFactory(boolean rounded){ if(rounded){ return new RoundedShapeFactory(); }else{ return new ShapeFactory(); Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT } } } Bước Sử dụng FactoryProductioner để lấy AbstractFactory để lấy nhà máy lớp cụ thể cách chuyển thông tin chẳng hạn loại AbstractFactoryPatternDemo.java public class AbstractFactoryPatternDemo { public static void main(String[] args) { //get shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory(false); //get an object of Shape Rectangle Shape shape1 = shapeFactory.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape1.draw(); //get an object of Shape Square Shape shape2 = shapeFactory.getShape("SQUARE"); //call draw method of Shape Square shape2.draw(); //get shape factory AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true); //get an object of Shape Rectangle Shape shape3 = shapeFactory1.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape3.draw(); //get an object of Shape Square Shape shape4 = shapeFactory1.getShape("SQUARE"); //call draw method of Shape Square shape4.draw(); } } Bước Xác minh kết đầu Inside Inside Inside Inside Rectangle::draw() method Square::draw() method RoundedRectangle::draw() method RoundedSquare::draw() method Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT 3.Builder Pattern UML Một builder gồm thành phần sau:     Product : đại diện cho đối tượng cần tạo, đối tượng phức tạp, có nhiều thuộc tính Builder : abstract class interface khai báo phương thức tạo đối tượng ConcreteBuilder : kế thừa Builder cài đặt chi tiết cách tạo đối tượng Nó xác định nắm giữ thể mà tạo ra, đồng thời cung cấp phương thức để trả các thể mà tạo trước Director/ Client: nơi gọi tới Builder để tạo đối tượng Code: Bước Tạo giao diện Mục đại diện cho mặt hàng thực phẩm đóng gói Item.java Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT public interface Item { public String name(); public Packing packing(); public float price(); } Packing.java public interface Packing { public String pack(); } Bước Tạo lớp cụ thể thực giao diện Đóng gói Wrapper.java public class Wrapper implements Packing { } @Override public String pack() { return "Wrapper"; } Bottle.java public class Bottle implements Packing { } @Override public String pack() { return "Bottle"; } Bước Tạo lớp trừu tượng triển khai giao diện mục cung cấp chức mặc định Burger.java public abstract class Burger implements Item { @Override public Packing packing() { return new Wrapper(); } Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT public class Rectangle implements Shape { } @Override public void draw() { System.out.println("Rectangle::draw()"); } Square.java public class Square implements Shape { } @Override public void draw() { System.out.println("Square::draw()"); } Circle.java public class Circle implements Shape { } @Override public void draw() { System.out.println("Circle::draw()"); } Bước Tạo lớp mặt tiền ShapeMaker.java public class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; public ShapeMaker() { circle = new Circle(); rectangle = new Rectangle(); square = new Square(); } public void drawCircle(){ circle.draw(); } public void drawRectangle(){ rectangle.draw(); Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT } } public void drawSquare(){ square.draw(); } Bước Sử dụng mặt tiền để vẽ loại hình dạng khác FacadePatternDemo.java public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); } } shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); Bước Xác minh kết đầu Circle::draw() Rectangle::draw() Square::draw() 12 Chain of Responsibility Pattern: UML: Youtube.com/PoppinKhiem - Sân chơi giới trẻ PTIT Code: Bước Tạo lớp trình ghi tóm tắt AbstractLogger.java public abstract class AbstractLogger { public static int INFO = 1; public static int DEBUG = 2; public static int ERROR = 3; protected int level; //next element in chain or responsibility protected AbstractLogger nextLogger; public void setNextLogger(AbstractLogger nextLogger){ this.nextLogger = nextLogger; } public void logMessage(int level, String message){ if(this.level

Ngày đăng: 22/06/2021, 11:14

Mục lục

  • Một Factory Pattern bao gồm các thành phần cơ bản

  • 12. Chain of Responsibility Pattern:

Tài liệu cùng người dùng

Tài liệu liên quan