Skip to content

Commit 16274ea

Browse files
author
Omar Abdelhafith
committed
Removing function argument from exectue function
1 parent c4750c6 commit 16274ea

File tree

4 files changed

+64
-28
lines changed

4 files changed

+64
-28
lines changed

Design-Patterns.playground.zip

71 Bytes
Binary file not shown.
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
protocol FileOperationCommand {
2-
func execute(file: String)
2+
init(file: String)
3+
func execute()
34
}
45

56
class FileMoveCommand : FileOperationCommand {
6-
func execute(file: String) {
7+
let file:String
8+
required init(file: String) {
9+
self.file = file
10+
}
11+
12+
func execute() {
713
print("\(file) moved")
814
}
915
}
1016

1117
class FileDeleteCommand : FileOperationCommand {
12-
func execute(file: String) {
18+
let file:String
19+
required init(file: String) {
20+
self.file = file
21+
}
22+
23+
func execute() {
1324
print("\(file) deleted")
1425
}
1526
}
1627

1728
class FileManager {
18-
let operation: FileOperationCommand
19-
20-
init(operation: FileOperationCommand) {
21-
self.operation = operation
29+
var deleteCommand: FileOperationCommand
30+
var moveCommand: FileOperationCommand
31+
32+
init(deleteCommand: FileDeleteCommand, moveCommand: FileMoveCommand) {
33+
self.deleteCommand = deleteCommand;
34+
self.moveCommand = moveCommand;
2235
}
23-
24-
func executeOn(# file:String) {
25-
self.operation.execute(file)
36+
37+
func delete () {
38+
deleteCommand.execute()
39+
}
40+
41+
func move () {
42+
moveCommand.execute()
2643
}
2744
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var fileManager = FileManager(operation: FileDeleteCommand())
2-
fileManager.executeOn(file: "/path/to/testfile")
1+
let deleteCommand = FileDeleteCommand(file: "/path/to/testfile");
2+
let moveCommand = FileMoveCommand(file: "/path/to/testfile");
3+
let fileManager = FileManager(deleteCommand:deleteCommand , moveCommand: moveCommand)
34

4-
fileManager = FileManager(operation: FileMoveCommand())
5-
fileManager.executeOn(file: "/path/to/testfile")
5+
fileManager.delete()
6+
fileManager.move()

README.markdown

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,41 +343,59 @@ tuple.z
343343

344344
```swift
345345
protocol FileOperationCommand {
346-
func execute(file: String)
346+
init(file: String)
347+
func execute()
347348
}
348349

349350
class FileMoveCommand : FileOperationCommand {
350-
func execute(file: String) {
351+
let file:String
352+
required init(file: String) {
353+
self.file = file
354+
}
355+
356+
func execute() {
351357
print("\(file) moved")
352358
}
353359
}
354360

355361
class FileDeleteCommand : FileOperationCommand {
356-
func execute(file: String) {
362+
let file:String
363+
required init(file: String) {
364+
self.file = file
365+
}
366+
367+
func execute() {
357368
print("\(file) deleted")
358369
}
359370
}
360371

361372
class FileManager {
362-
let operation: FileOperationCommand
363-
364-
init(operation: FileOperationCommand) {
365-
self.operation = operation
373+
var deleteCommand: FileOperationCommand
374+
var moveCommand: FileOperationCommand
375+
376+
init(deleteCommand: FileDeleteCommand, moveCommand: FileMoveCommand) {
377+
self.deleteCommand = deleteCommand;
378+
self.moveCommand = moveCommand;
366379
}
367-
368-
func executeOn(# file:String) {
369-
self.operation.execute(file)
380+
381+
func delete () {
382+
deleteCommand.execute()
383+
}
384+
385+
func move () {
386+
moveCommand.execute()
370387
}
371388
}
372389
```
373390

374391
**Usage:**
375392
```swift
376-
var fileManager = FileManager(operation: FileDeleteCommand())
377-
fileManager.executeOn(file: "/path/to/testfile")
393+
let deleteCommand = FileDeleteCommand(file: "/path/to/testfile");
394+
let moveCommand = FileMoveCommand(file: "/path/to/testfile");
395+
let fileManager = FileManager(deleteCommand:deleteCommand , moveCommand: moveCommand)
378396

379-
fileManager = FileManager(operation: FileMoveCommand())
380-
fileManager.executeOn(file: "/path/to/testfile")
397+
fileManager.delete()
398+
fileManager.move()
381399
```
382400

383401
##Iterator

0 commit comments

Comments
 (0)