Is it legal,safe or purposeful to control if a subclass is conforming a protocol in base class [closed]
I wonder if it's a good practice to control if a protocol is conforming in base class. In my opinion, it's not a good practice because the base class should include all the properties and methods that should be related to sub classes. And the base class should not know the protocol that the sub class is conforming or not.
An example usage;
class BaseClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let self = self as? SomethingAble {
self.doIt()
}
}
}
protocol SomethingAble {
func doit()
}
Thanks,
swift
closed as primarily opinion-based by matt, FelixSFD, Roman Pokrovskij, Tomasz Mularczyk, grizzthedj Dec 31 '18 at 15:15
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 4 more comments
I wonder if it's a good practice to control if a protocol is conforming in base class. In my opinion, it's not a good practice because the base class should include all the properties and methods that should be related to sub classes. And the base class should not know the protocol that the sub class is conforming or not.
An example usage;
class BaseClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let self = self as? SomethingAble {
self.doIt()
}
}
}
protocol SomethingAble {
func doit()
}
Thanks,
swift
closed as primarily opinion-based by matt, FelixSFD, Roman Pokrovskij, Tomasz Mularczyk, grizzthedj Dec 31 '18 at 15:15
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
protocol SubClass: BaseClass, SomethingAble {
makes no sense. Did you meanclass
?
– matt
Dec 31 '18 at 11:24
1
You've answered your question already "the base class should not know the protocol that the sub class is conforming or not."
– Gereon
Dec 31 '18 at 11:26
1
@AlicanYilmaz Why is that a problem for you?
– Sulthan
Dec 31 '18 at 11:39
2
The protocol here is a red herring. Yourif let self = self as? SomethingAble
Is just the same asif let self = self as? SomeSubclass
— You would never say that because the code would all go in the SomeSubclass override. Otherwise what is subclassing for? You might as well throw away polymorphism altogether if you’re going to write code like that.
– matt
Dec 31 '18 at 11:41
1
“what's your advice to achieve a behaviour like implementing something in all subclasses except several of them?” Another subclass.
– matt
Dec 31 '18 at 12:02
|
show 4 more comments
I wonder if it's a good practice to control if a protocol is conforming in base class. In my opinion, it's not a good practice because the base class should include all the properties and methods that should be related to sub classes. And the base class should not know the protocol that the sub class is conforming or not.
An example usage;
class BaseClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let self = self as? SomethingAble {
self.doIt()
}
}
}
protocol SomethingAble {
func doit()
}
Thanks,
swift
I wonder if it's a good practice to control if a protocol is conforming in base class. In my opinion, it's not a good practice because the base class should include all the properties and methods that should be related to sub classes. And the base class should not know the protocol that the sub class is conforming or not.
An example usage;
class BaseClass: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let self = self as? SomethingAble {
self.doIt()
}
}
}
protocol SomethingAble {
func doit()
}
Thanks,
swift
swift
edited Dec 31 '18 at 11:27
Gereon
6,37741746
6,37741746
asked Dec 31 '18 at 11:17
Alican YilmazAlican Yilmaz
7210
7210
closed as primarily opinion-based by matt, FelixSFD, Roman Pokrovskij, Tomasz Mularczyk, grizzthedj Dec 31 '18 at 15:15
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by matt, FelixSFD, Roman Pokrovskij, Tomasz Mularczyk, grizzthedj Dec 31 '18 at 15:15
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
protocol SubClass: BaseClass, SomethingAble {
makes no sense. Did you meanclass
?
– matt
Dec 31 '18 at 11:24
1
You've answered your question already "the base class should not know the protocol that the sub class is conforming or not."
– Gereon
Dec 31 '18 at 11:26
1
@AlicanYilmaz Why is that a problem for you?
– Sulthan
Dec 31 '18 at 11:39
2
The protocol here is a red herring. Yourif let self = self as? SomethingAble
Is just the same asif let self = self as? SomeSubclass
— You would never say that because the code would all go in the SomeSubclass override. Otherwise what is subclassing for? You might as well throw away polymorphism altogether if you’re going to write code like that.
– matt
Dec 31 '18 at 11:41
1
“what's your advice to achieve a behaviour like implementing something in all subclasses except several of them?” Another subclass.
– matt
Dec 31 '18 at 12:02
|
show 4 more comments
protocol SubClass: BaseClass, SomethingAble {
makes no sense. Did you meanclass
?
– matt
Dec 31 '18 at 11:24
1
You've answered your question already "the base class should not know the protocol that the sub class is conforming or not."
– Gereon
Dec 31 '18 at 11:26
1
@AlicanYilmaz Why is that a problem for you?
– Sulthan
Dec 31 '18 at 11:39
2
The protocol here is a red herring. Yourif let self = self as? SomethingAble
Is just the same asif let self = self as? SomeSubclass
— You would never say that because the code would all go in the SomeSubclass override. Otherwise what is subclassing for? You might as well throw away polymorphism altogether if you’re going to write code like that.
– matt
Dec 31 '18 at 11:41
1
“what's your advice to achieve a behaviour like implementing something in all subclasses except several of them?” Another subclass.
– matt
Dec 31 '18 at 12:02
protocol SubClass: BaseClass, SomethingAble {
makes no sense. Did you mean class
?– matt
Dec 31 '18 at 11:24
protocol SubClass: BaseClass, SomethingAble {
makes no sense. Did you mean class
?– matt
Dec 31 '18 at 11:24
1
1
You've answered your question already "the base class should not know the protocol that the sub class is conforming or not."
– Gereon
Dec 31 '18 at 11:26
You've answered your question already "the base class should not know the protocol that the sub class is conforming or not."
– Gereon
Dec 31 '18 at 11:26
1
1
@AlicanYilmaz Why is that a problem for you?
– Sulthan
Dec 31 '18 at 11:39
@AlicanYilmaz Why is that a problem for you?
– Sulthan
Dec 31 '18 at 11:39
2
2
The protocol here is a red herring. Your
if let self = self as? SomethingAble
Is just the same as if let self = self as? SomeSubclass
— You would never say that because the code would all go in the SomeSubclass override. Otherwise what is subclassing for? You might as well throw away polymorphism altogether if you’re going to write code like that.– matt
Dec 31 '18 at 11:41
The protocol here is a red herring. Your
if let self = self as? SomethingAble
Is just the same as if let self = self as? SomeSubclass
— You would never say that because the code would all go in the SomeSubclass override. Otherwise what is subclassing for? You might as well throw away polymorphism altogether if you’re going to write code like that.– matt
Dec 31 '18 at 11:41
1
1
“what's your advice to achieve a behaviour like implementing something in all subclasses except several of them?” Another subclass.
– matt
Dec 31 '18 at 12:02
“what's your advice to achieve a behaviour like implementing something in all subclasses except several of them?” Another subclass.
– matt
Dec 31 '18 at 12:02
|
show 4 more comments
2 Answers
2
active
oldest
votes
You have many ways to implement optional protocols so one of them:
First:
Swift has a feature called extension
that allow us to provide a default implementation for those methods that we want to be optional.
protocol SomethingAble {
func optionalMethod()
func notOptionalMethod()
}
extension SomethingAble {
func optionalMethod() {
//this is a empty implementation to allow this method to be optional
}
}
Second:
@objc protocol SomethingAble {
@objc optional func optionalMethod()
}
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
2
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
add a comment |
I think, you can use custom delegate
variable
class myViewController: UIViewController {
var customDelegate : UIViewController?
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
if delegate is MainVC {
//do what you want.
} else {
// git la burdan ;)
}
}
.
.
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have many ways to implement optional protocols so one of them:
First:
Swift has a feature called extension
that allow us to provide a default implementation for those methods that we want to be optional.
protocol SomethingAble {
func optionalMethod()
func notOptionalMethod()
}
extension SomethingAble {
func optionalMethod() {
//this is a empty implementation to allow this method to be optional
}
}
Second:
@objc protocol SomethingAble {
@objc optional func optionalMethod()
}
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
2
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
add a comment |
You have many ways to implement optional protocols so one of them:
First:
Swift has a feature called extension
that allow us to provide a default implementation for those methods that we want to be optional.
protocol SomethingAble {
func optionalMethod()
func notOptionalMethod()
}
extension SomethingAble {
func optionalMethod() {
//this is a empty implementation to allow this method to be optional
}
}
Second:
@objc protocol SomethingAble {
@objc optional func optionalMethod()
}
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
2
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
add a comment |
You have many ways to implement optional protocols so one of them:
First:
Swift has a feature called extension
that allow us to provide a default implementation for those methods that we want to be optional.
protocol SomethingAble {
func optionalMethod()
func notOptionalMethod()
}
extension SomethingAble {
func optionalMethod() {
//this is a empty implementation to allow this method to be optional
}
}
Second:
@objc protocol SomethingAble {
@objc optional func optionalMethod()
}
You have many ways to implement optional protocols so one of them:
First:
Swift has a feature called extension
that allow us to provide a default implementation for those methods that we want to be optional.
protocol SomethingAble {
func optionalMethod()
func notOptionalMethod()
}
extension SomethingAble {
func optionalMethod() {
//this is a empty implementation to allow this method to be optional
}
}
Second:
@objc protocol SomethingAble {
@objc optional func optionalMethod()
}
answered Dec 31 '18 at 11:34
Jogendar ChoudharyJogendar Choudhary
2,5471619
2,5471619
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
2
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
add a comment |
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
2
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
Imagine you have 1500 + 5 classes, I want to do the same behaviour in 1500 classes except the 5. But all the 1505 classes inherits from BaseClass.
– Alican Yilmaz
Dec 31 '18 at 11:37
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
@AlicanYilmaz: No, Using first way You need to add extension only once then all 1500 classes not required optional methods
– Jogendar Choudhary
Dec 31 '18 at 11:40
2
2
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
1500 classes makes a design smell - you really have that many? with different implementations of methods on each one? Yu could ut the base as not incluin the rotocol an the the 5 and another abstract inherit from that and the protocol is added to that abstract
– Mark
Dec 31 '18 at 11:40
add a comment |
I think, you can use custom delegate
variable
class myViewController: UIViewController {
var customDelegate : UIViewController?
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
if delegate is MainVC {
//do what you want.
} else {
// git la burdan ;)
}
}
.
.
}
add a comment |
I think, you can use custom delegate
variable
class myViewController: UIViewController {
var customDelegate : UIViewController?
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
if delegate is MainVC {
//do what you want.
} else {
// git la burdan ;)
}
}
.
.
}
add a comment |
I think, you can use custom delegate
variable
class myViewController: UIViewController {
var customDelegate : UIViewController?
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
if delegate is MainVC {
//do what you want.
} else {
// git la burdan ;)
}
}
.
.
}
I think, you can use custom delegate
variable
class myViewController: UIViewController {
var customDelegate : UIViewController?
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
if delegate is MainVC {
//do what you want.
} else {
// git la burdan ;)
}
}
.
.
}
answered Dec 31 '18 at 11:57
delavega66delavega66
1,09111530
1,09111530
add a comment |
add a comment |
protocol SubClass: BaseClass, SomethingAble {
makes no sense. Did you meanclass
?– matt
Dec 31 '18 at 11:24
1
You've answered your question already "the base class should not know the protocol that the sub class is conforming or not."
– Gereon
Dec 31 '18 at 11:26
1
@AlicanYilmaz Why is that a problem for you?
– Sulthan
Dec 31 '18 at 11:39
2
The protocol here is a red herring. Your
if let self = self as? SomethingAble
Is just the same asif let self = self as? SomeSubclass
— You would never say that because the code would all go in the SomeSubclass override. Otherwise what is subclassing for? You might as well throw away polymorphism altogether if you’re going to write code like that.– matt
Dec 31 '18 at 11:41
1
“what's your advice to achieve a behaviour like implementing something in all subclasses except several of them?” Another subclass.
– matt
Dec 31 '18 at 12:02