我是swift的新手。一些dessign模式的实现让我很困惑。
例如我在java代码中有presudo模式观察者(回调)(下面有例子)。即UI将自己的监听器传递给Manager类,并监听回调isConnected和isDisconnected。如果执行回调,则UI类显示某些消息" isConnected"或" isDisconnected"
public class UI{
private Manager mManager;
void createManager(){
mManager = new Manager(mManagerLister);
}
public void showMessage(String aMsg){
print(aMsg)
}
private final IManagerListener mManagerLister = new IManagerListener{
void isConnected(){
this.showMessage("isConnected")
}
void isDisconnected(){
this.showMessage("isConnected")
}
}
}
public class Manager{
interface IManagerListener{
void isConnected();
void isDisconnected();
}
private final mListener;
public Manager(IManagerListener aListener){
mListener = aListener;
}
}
如何正确地将此java代码移植到swift代码?我尝试移植但错误消息 类型' UI'没有会员' showMessage' 会显示
public class UI{
var manager: Manager?
var managerListener: IManagerListener?
func createManager(){
managerListener = ManagerListenerImp(self)
manager = Manager(managerListener)
}
public func showMessage(msg: String){
print(msg)
}
class ManagerListenerImp: IManagerListener{
weak var parent: UI
init(parent : UI ){
self.parent = parent
}
func isConnected(){
parent.showMessage("isConnected")
// Value of type 'UI' has no member 'showMessage'
}
..........
}
}
也许更优雅地存在一种使用回调的方式,我的方式不正确?
答案 0 :(得分:1)
有多种方法可以实现它。
因为你已经在下面详细说明了委托模式。
修改您的代码,如下所示
声明协议
@objc protocol ManagerListenerImp {
func isConnected()
}
在Manager类中声明变量
class Manager {
weak var delegate : ManagerListenerImp? = nil
}
确认您的UI类中的ManagerListenerImp
extension UI : ManagerListenerImp {
func isConnected () {
//your isConnected implementation here
}
}
传递UI实例(swift中的self和JAVA中的这个经理类)
func createManager(){
manager = Manager()
manager?.delegate = self
}
最后,每当你想从isConnected
课程中触发Manager
时,只需说出
self.delegate?.isConnected()
在您的经理类中
希望有所帮助
答案 1 :(得分:0)
我对哪个类引用了哪个类有些困惑,但在下面的示例中应该不难改变。
您可能正在寻找观察者模式。这可以让多个对象监听相同的更改:
任何应对管理器状态变化做出反应的类要实现的协议
protocol ManagerStateListener: AnyObject {
func stateChanged(to state: Manager.State)
}
Manager 类包含:
class Manager {
/// The possible states of the Manager
enum State {
case one
case two
case three
}
/// The variable that stores the current state of the manager
private var _currentState: State = .one
var currentState: State {
get {
return _currentState
}
set {
_currentState = newValue
/// Calls the function that will alert all listeners
/// that the state has changed
invoke()
}
}
/// The list with all listeners
var listeners: [ManagerStateListener] = []
/// A specific listener that gets initialised here
let someListener = SomeListener()
init() {
addListener(someListener) /// Add the listener to the list
}
/// Method that invokes the stateChanged method on all listeners
func invoke() {
for listener in listeners {
listener.stateChanged(to: currentState)
}
}
/// Method for adding a listener to the list of listeners
func addListener(_ listener: ManagerStateListener) {
listeners.append(listener)
}
/// Method for removing a specific listener from the list of listeners
func removeListener(_ listener: ManagerStateListener) {
if let index = listeners.firstIndex(where: { $0 === listener }) {
listeners.remove(at: index)
}
}
}
实现 ManagerStateListener 协议的示例监听器,由 Manager 类持有
class SomeListener : ManagerStateListener {
func stateChanged(to state: Manager.State) {
/// Do something based on the newly received state
switch state {
case .one:
print("State changed to one")
case .two:
print("State changed to two")
case .three:
print("State changed to three")
}
}
}
希望对大家有帮助。