Swift Protocol oriented mixed scale

I have a conceptual question with programmatic programming. Let's say I am creating a protocol Foo and I want to extend Foo with the action () function in the protocol extension. action () will always be basically the same no matter who implements it, so I don't want to repeat this code. The only part of action () that changes is prop, and prop should be an instance property. So my corresponding framework should just need a prop definition and then the action will work correctly. This code is fine and looks like this:

protocol Foo {
    var prop : String {get set}
}
extension Foo {
    func action() -> String {
        return prop + ": Applied Action."
    }
}

      

But now here's the catch. Let's say prop is information that I don't want other classes to have access to. I want them to only have access to it via action (). In the current swift, the prop should be at least internal because the protocol is internal, but I need to keep it private so that consumer classes can't accidentally change a property or read information, which I only want them to be accessed via an action (), I could add action () to the protocol, but then I need to rewrite the action () every time the structure matches.

This same example can be extended to function as well:

protocol Foo {
     //customization point by conforming class
    func customAction(str: String) -> String
}
extension Foo {
    func action() -> String {
        //Do some default work
        let str = "some string created from previous default work"
        return customAction(str: str)
    }
}

      

action () will always be the same implementation, except for cusotmAction (). So, I need the appropriate classes to implement customAction (), but I don't want the outer classes to call customAction () as it only provides certain behavior that should only be used inside the action (). So I need customAction () to be private, but again this is not possible.

So here's my question, is this just a breakdown in programmatic programming? How can I use protocols to write this type of code without giving prop / customAction () too many options, or rewriting the same code inside action () over and over? This example is pretty simple, but I have to run into more complex versions of the same problem. Is there another way to look at this problem from a POP perspective, or should I be looking at a more object oriented approach?

+3


source to share


1 answer


I do not believe that this is inevitably a rollback of protocol-oriented programming as a concept, but just the language in which you use it. Perhaps Swift was not designed with such cases. However, I think one way to solve this problem would be to use composition, in which you create an object for which you want to define functions, such as action

one that uses state that is not externally accessible. Take a look at this example:

struct Foo {
    init(prop: String) {
        self.prop = prop
    }

    func action() -> String {
        return prop + ": Applied Action."
    }

    private let prop: String
}


class Test {
    let foo = Foo(prop: "TEST")
}

      



In this case, it Test

can define its own prop

in the same way (but not quite so) if it used the protocol. That being said, you can now call all functions foo

inside the instance Test

without any access to the main variables. Doing it this way (with composition) also saves you the inheritance problems that make people use POP in the first place. Hope this helps!

0


source







All Articles