ניתוח ועיצוב מערכות
תוכנה
תירגול 7
Combining
Modules
01
The layers we will work with
• Presentation layer modules: These modules are responsible for presenting the data to the user. They include the UI (User
Interface) components such as screens, dialogs, and controls.
• Business logic modules: These modules are responsible for implementing the business rules of the application. They define
how the application works, how it processes data, and how it interacts with other systems.
• Data Access Layer (DAL) modules: These modules are responsible for interacting with the data storage layer. They abstract
away the details of the underlying data storage technology (e.g., SQL databases, NoSQL databases, file systems, etc.) and
provide a unified interface for the application to access and manipulate the data.
Example App
Now, let's create an example app that uses these modules. Let's say we're building a simple e-
commerce platform that allows users to browse products, add them to a shopping cart, and place
orders. We'll need the following classes:
• Product: Represents a product in the e-commerce platform. It has properties such as name,
description, price, and image.
• ShoppingCartItem: Represents an item in the user's shopping cart. It has properties such as
product, quantity, and total price.
• ShoppingCart: Represents the user's shopping cart. It has a list of ShoppingCartItem objects.
• Order: Represents an order in the e-commerce platform. It has properties such as order number,
date, total price, and a list of OrderItem objects.
• OrderItem: Represents an item in an order. It has properties such as product, quantity, and total
price.
The modules we will create
Presentation layer modules:
• ProductScreen: Displays a list of products and allows the user to select a product to view its details.
• ProductDetailsScreen: Displays the details of a selected product and allows the user to add it to their shopping cart.
• ShoppingCartScreen: Displays the contents of the user's shopping cart and allows the user to place an order.
Business logic modules:
• ProductController: Provides methods to retrieve and manipulate Product objects.
• ShoppingCartController: Provides methods to add and remove items from the user's shopping cart, and to calculate the total
price of the cart.
• OrderController: Provides methods to create and retrieve Order objects.
Data Access Layer (DAL) modules:
• ProductRepository: Provides methods to retrieve and save Product objects from/to the data storage layer.
• ShoppingCartRepository: Provides methods to retrieve and save ShoppingCart objects from/to the data storage layer.
• OrderRepository: Provides methods to retrieve and save Order objects from/to the data storage layer.
Presentation Layer Code
public class ProductDetailsScreen {
public class ProductScreen { private ProductController productController;
private ProductController productController; private ShoppingCartController shoppingCartController;
public ProductScreen(ProductController public ProductDetailsScreen(ProductController
productController) { productController, ShoppingCartController
this.productController = productController; shoppingCartController) {
} this.productController = productController;
this.shoppingCartController = shoppingCartController;
public void display() { }
List<Product> products =
productController.getAll(); public void display(int productId) {
// display the products in a list Product product = productController.getById(productId);
} // display the product details
} }
public void addToCart(int productId, int quantity) {
Product product = productController.getById(productId);
shoppingCartController.addItem(product, quantity);
}
}
Presentation Layer Code
public class ShoppingCartScreen {
private ShoppingCartController shoppingCartController;
private OrderController orderController;
public ShoppingCartScreen(ShoppingCartController shoppingCartController,
OrderController orderController) {
this. shoppingCartController = shoppingCartController;
this. orderController = orderController;
}
public void display() {
List<ShoppingCartItem> items = shoppingCartController.getItems();
// display the shopping cart items in a list
}
public void placeOrder() {
Order order = orderController.createOrder(shoppingCartController.getItems());
shoppingCartController.clearCart();
// display a message indicating that the order was placed
}
}
Business Layer public class Order {
private int id;
private List<ProductOrder> productOrders;
public class Product { private double total;
private int id;
private String name; public Order(double total) {
private double price; this.productOrders = new ArrayList<>();
this.total = total;
public Product(String name, double price) { }
this.name = name;
this.price = price; setters / getters
}
public void addProductOrder(ProductOrder productOrder) {
setters / getters productOrders.add(productOrder);
}
public double getPrice() {
return price; public double getTotal() {
} return total;
}
public void setPrice(double price) {
this.price = price; public void setTotal(double total) {
} this.total = total;
} }
}
Business Layer
public class ProductOrder {
private Product product;
private int quantity;
public ProductOrder(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
}
public class ShoppingCartController {
private ShoppingCartMapper shoppingCartMapper;
Business Layer private OrderMapper orderMapper;
public ShoppingCartController(ShoppingCartMapper
shoppingCartMapper, OrderMapper orderMapper) {
public class ProductController { this.shoppingCartMapper = shoppingCartMapper;
private ProductMapper productMapper; this.orderMapper = orderMapper;
}
public ProductController(ProductMapper public List<ShoppingCartItem> getItems() {
productMapper) { ShoppingCart cart = shoppingCartMapper.find();
this.productMapper = productMapper; return cart.getItems();
} }
public List<Product> getAll() { public void addItem(Product product, int quantity) {
return productMapper.findAll(); ShoppingCart cart = shoppingCartMapper.find();
} cart.addItem(product, quantity);
shoppingCartMapper.update(cart);
}
public Product getById(int id) {
return productMapper.findById(id); public void removeItem(ShoppingCartItem item) {
} ShoppingCart cart = shoppingCartMapper.find();
} cart.removeItem(item);
shoppingCartMapper.update(cart);
}
public void clearCart() {
ShoppingCart cart = shoppingCartMapper.find();
cart.clear();
shoppingCartMapper.update(cart);
}
}
public class OrderController {
private OrderMapper orderMapper;
Business Layer
public OrderController(OrderMapper orderMapper) {
this.orderMapper = orderMapper;
}
public Order createOrder(List<ShoppingCartItem> items) {
double total = calculateTotalPrice(items);
Order order = new Order(items, total);
orderMapper.insert(order);
return order;
}
public List<Order> getAll() {
return orderMapper.findAll();
}
public Order getById(int id) {
return orderMapper.findById(id);
}
private double calculateTotalPrice(List<ShoppingCartItem> items) {
double total = 0.0;
for (ShoppingCartItem item : items) {
total += item.getTotalPrice();
}
return total;
}
}
Data Access Layer
public class OrderMapper { public List<Order> findAll() {
private Connection connection; String sql = "SELECT * FROM orders";
private List<Order> identityMap; List<Order> orders = new ArrayList<>();
try (Statement statement = connection.createStatement()) {
public OrderMapper(Connection connection) { ResultSet rs = statement.executeQuery(sql);
this.connection = connection; while (rs.next()) {
this.identityMap = new ArrayList<>(); int id = rs.getInt("id");
} double total = rs.getDouble("total");
Order order = getById(id);
public void insert(Order order) { if (order == null) {
String sql = "INSERT INTO orders (total) VALUES (?)"; order = new Order(total);
try (PreparedStatement statement = order.setId(id);
connection.prepareStatement(sql)) { identityMap.add(order);
statement.setDouble(1, order.getTotal()); }
statement.executeUpdate(); orders.add(order);
ResultSet rs = statement.getGeneratedKeys(); }
if (rs.next()) { } catch (SQLException e) {
order.setId(rs.getInt(1)); e.printStackTrace();
identityMap.add(order); }
} return orders;
} catch (SQLException e) { }
e.printStackTrace();
}
}
Data Access Layer
public Order getById(int id) { public void update(Order order) {
for (Order order : identityMap) { String sql = "UPDATE orders SET total = ? WHERE id = ?";
if (order.getId() == id) { try (PreparedStatement statement =
return order; connection.prepareStatement(sql)) {
} statement.setDouble(1, order.getTotal());
} statement.setInt(2, order.getId());
String sql = "SELECT * FROM orders WHERE id = ?"; statement.executeUpdate();
try (PreparedStatement statement = } catch (SQLException e) {
connection.prepareStatement(sql)) { e.printStackTrace();
statement.setInt(1, id); }
ResultSet rs = statement.executeQuery(); }
if (rs.next()) { }
double total = rs.getDouble("total");
Order order = new Order(total);
order.setId(id);
identityMap.add(order);
return order;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
Class Diagrams to
Object Diagrams
Example 1
Example 1 - Solution
Example 2
Example 2 – Cont.
Example 2 – Cont.
Example 3
2..0
Example 3 – Cont.
From OD to CD