为什么在CustomerService Service = serviceFactory.getInstance()。getServiceType(serviceType.customer);

问题描述 投票:0回答:1
superservice:

package service; public interface SuperService { }
customerService:

package service.custom; import dto.Customer; import java.util.List; public interface CustomerService{ boolean add(Customer customer); Customer search(String id); boolean update(String id, Customer customer); boolean delete(String id); List<Customer> getAll(); }
customerServiceImpl:

package service.custom.impl; import dto.Customer; public class CustomerServiceImpl{ public boolean add(Customer customer) { return false; } }
serviceFactory:

package service; import service.custom.impl.CustomerServiceImpl; import service.custom.impl.ItemServiceImpl; import service.custom.impl.OrderServiceImpl; import util.ServiceType; public class ServiceFactory { private static ServiceFactory instance; private ServiceFactory(){ } public static ServiceFactory getInstance(){ if(instance==null){ instance= new ServiceFactory(); } return instance; } public <T extends SuperService> T getServiceType(ServiceType type){ switch (type){ case CUSTOMER:return (T) new CustomerServiceImpl(); case ITEM:return (T) new ItemServiceImpl(); case ORDER:return (T) new OrderServiceImpl(); } return null; } }

customerFormController:

package controller; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXTextField; import dto.Customer; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import service.ServiceFactory; import service.custom.CustomerService; import util.ServiceType; public class CustomerFormController { @FXML private JFXButton btnAdd; @FXML private JFXButton btnDelete; @FXML private JFXButton btnReload; @FXML private JFXButton btnSearch; @FXML private JFXButton btnUpdate; @FXML private TableColumn<?, ?> colAddress; @FXML private TableColumn<?, ?> colId; @FXML private TableColumn<?, ?> colName; @FXML private TableColumn<?, ?> colSalary; @FXML private TableView<?> tblCustomers; @FXML private JFXTextField txtAddress; @FXML private JFXTextField txtId; @FXML private JFXTextField txtName; @FXML private JFXTextField txtSalary; @FXML void btnAddOnAction(ActionEvent event) { String idText = txtId.getText(); String nameText = txtName.getText(); String addressText = txtAddress.getText(); double salary = Double.parseDouble(txtSalary.getText()); Customer customer = new Customer(idText, nameText, addressText, salary); CustomerService service = ServiceFactory.getInstance().getServiceType(ServiceType.CUSTOMER); service.add(customer); } @FXML void btnDeleteOnAction(ActionEvent event) { } @FXML void btnReloadOnAction(ActionEvent event) { } @FXML void btnSearchOnAction(ActionEvent event) { } @FXML void btnUpdateOnAction(ActionEvent event) { } }

这是一种分层的架构设计,在这里,我试图使用工厂设计模式将
CustomerServiceImpl

对象从服务层传递到控制器/演示层。我试图了解有限的类型通用物,并决定进行实验以了解概念,然后才遇到问题。您能帮我理解为什么

CustomerService service = ServiceFactory.getInstance().getServiceType(ServiceType.CUSTOMER);
没有编译错误,因为
CustomerService
没有扩展
SuperService
CustomerServiceImpl
CustomerService
没有实现
CustomerService
引用不能用于
CustomerServiceImpl
对象?
    

java generics javafx interface bounded-types
1个回答
0
投票

public <T extends SuperService> T getServiceType(ServiceType type){ switch (type){ case CUSTOMER:return (T) new CustomerServiceImpl(); case ITEM:return (T) new ItemServiceImpl(); case ORDER:return (T) new OrderServiceImpl(); } return null; }

在适当的位置上,您基本上将所有sumpers施放到超级服务中。
现在,由于SuperService是一个接口,您不会收到编译错误,但是您会遇到运行时错误!

基本上归结为:

casting到无关接口不会给您编译错误。

更多信息可以在此处找到:cast铸造到无关接口时,它为什么会编译?

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.