I have a rather large project structured in this format:
class One : FirstThree {
fileprivate var integers: [Int] {
return [1, 2, 3, 101, 102]
}
override func allIntegers() -> [Int] {
return integers
}
func doStuffForOne() {
//does stuff unrelated to the other classes
}
}
class Two : FirstThree {
fileprivate var integers: [Int] {
return [1, 2, 3, 201]
}
override func allIntegers() -> [Int] {
return integers
}
func doStuffForTwo() {
//does stuff unrelated to the other classes
}
}
class Three : Numbers {
fileprivate var integers: [Int] {
return [301, 302, 303]
}
override func allIntegers() -> [Int] {
return integers
}
func doStuffForThree() {
//does stuff unrelated to the other classes
}
}
class FirstThree : Numbers {
fileprivate var integers: [Int] {
return [1, 2, 3]
}
override func allIntegers() -> [Int] {
return integers
}
func doStuffForFirstThree() {
//does stuff unrelated to the other classes
}
}
class Numbers {
func allIntegers() -> [Int] {
fatalError("subclass this")
}
func printMe() {
allIntegers().forEach({ print($0) })
}
}
Numbers
has many methods like printMe()
which I want any instance of all my subclasses to be able to call.
Numbers
also has an allIntegers()
function which I want any instance of these subclasses to be able to call. Which would probably be better as a variable right? But I can't override variables in a subclass. So instead I use the same private variable integers
in each subclass, which is read and returned by allIntegers()
.
Also notice an instance of Numbers
itself should never call allIntegers()
, it should only be called on a subclass.
Last, notice some of the subclasses contain the same objects 1, 2, 3
and then each has some custom integers. But not all the subclasses. If I later decide that all those subclasses need a 4
integer, I have to manually go through each class and punch in a 4
into the array, which is obviously error prone.
I've read up on protocol oriented programming and feel like a solution might lie in there, or I'd appreciate any other suggestions and creative approaches to architecting a better project.
Thanks!
EDIT
All subclasses are different because they also have their own functions to perform. I've updated the code to reflect this.
Imagine, a given class like One
is initialized many time throughout the code base, and is always initialized with the exact same integers
. Putting:
let one = One(integers: [1, 2, 3, 101, 102])
All throughout the code base would be error prone.
Hopefully this resolves some of the concerns of the contrived example I've put forward.
SOLUTION
Thank you everyone for your help. Here is the solution I came up with (please assume that all classes have their own unique methods).
class One : FirstThree {
override init() {
super.init()
self.integers = super.integers + [101, 102]
}
}
class Two : FirstThree {
override init() {
super.init()
self.integers = super.integers + [201]
}
}
class Three : Numbers {
var integers = [301, 302, 303]
}
class FirstThree : Numbers {
let integers = [1, 2, 3]
}
protocol Numbers {
var integers: [Int] { get }
func printMe()
}
extension Numbers {
func printMe() {
integers.forEach({ print($0) })
}
}