-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[dale] - Step5 리턴타입 class -> protocol 변경 #92
base: dale
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// PhotoRectangle.swift | ||
// DrawingApp | ||
// | ||
// Created by dale on 2022/03/16. | ||
// | ||
|
||
import Foundation | ||
|
||
class PhotoRectangle: Shape{ | ||
var backgroundImage: Data | ||
|
||
init(size: Size, position: Position, imageData: Data, alpha: Alpha) { | ||
self.backgroundImage = imageData | ||
super.init(size: size, position: position, alpha: alpha) | ||
} | ||
} | ||
|
||
extension PhotoRectangle : CustomStringConvertible{ | ||
var description: String { | ||
return "(\(self.id)), \(position), \(self.size), \(self.backgroundImage) , \(self.alpha)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Data 타입은 이렇게 description을 하는게 큰 의미는 없습니다. 존재하는지 혹은 크기가 바뀌는지 정도만 봐도 다행이죠 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,60 +8,59 @@ | |
import Foundation | ||
|
||
class Plane { | ||
private var rectangles : [Rectangle] = [] | ||
private var selectedRectangle : Rectangle? | ||
private var shapes : [Shape] = [] | ||
private var selectedShape : Shape? | ||
|
||
enum NotificationName { | ||
static let addRectangle : Notification.Name = Notification.Name("addRectangle") | ||
static let searchRectangle : Notification.Name = Notification.Name("searchRectangle") | ||
static let addShape : Notification.Name = Notification.Name("addShape") | ||
static let searchShape : Notification.Name = Notification.Name("searchShape") | ||
static let updateAlpha : Notification.Name = Notification.Name("updateAlpha") | ||
static let changeColor : Notification.Name = Notification.Name("changeColor") | ||
} | ||
|
||
enum NotificationKeyValue { | ||
static let rectangle = "rectangle" | ||
static let shape = "shape" | ||
static let index = "index" | ||
static let alpha = "alpha" | ||
static let color = "color" | ||
} | ||
|
||
subscript(index: Int) -> Rectangle? { | ||
if index < rectangleCount { | ||
let targetRectangle = rectangles[index] | ||
return targetRectangle | ||
subscript(index: Int) -> Shape? { | ||
if index < shapeCount { | ||
let targetShape = shapes[index] | ||
return targetShape | ||
} | ||
return nil | ||
} | ||
|
||
var rectangleCount : Int { | ||
return rectangles.count | ||
var shapeCount : Int { | ||
return shapes.count | ||
} | ||
|
||
func addRectangle(rectangle: Rectangle) { | ||
self.rectangles.append(rectangle) | ||
NotificationCenter.default.post(name: Plane.NotificationName.addRectangle, object: self, userInfo: [Plane.NotificationKeyValue.rectangle:rectangle]) | ||
func addShape(shape: Shape) { | ||
self.shapes.append(shape) | ||
NotificationCenter.default.post(name: Plane.NotificationName.addShape, object: self, userInfo: [Plane.NotificationKeyValue.shape:shape]) | ||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 Plane에서는 addShape()할 때 생성의 책임이 여기 없네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plane에서 생성하는게 맞는것 같습니다. 처음 설계시 사각형을 만드는 팩토리메소드가 인스턴스메소드라 팩토리에 대한 인스턴스는 VC가 가지고 있어야 한다고 생각을 했는데, 현재 팩토리메소드가 타입메소드 형태로 바뀌어 Plane에서 생성할 수 있습니다. |
||
} | ||
|
||
func searchRectangle(at position : Position) { | ||
self.selectedRectangle = nil | ||
for rectangle in rectangles.reversed() { | ||
if (rectangle.position.x...(rectangle.position.x + rectangle.size.width)).contains(position.x) && (rectangle.position.y...(rectangle.position.y + rectangle.size.height)).contains(position.y) { | ||
self.selectedRectangle = rectangle | ||
func searchShape(at position : Position) { | ||
self.selectedShape = nil | ||
for shape in shapes.reversed() { | ||
if (shape.position.x...(shape.position.x + shape.size.width)).contains(position.x) && (shape.position.y...(shape.position.y + shape.size.height)).contains(position.y) { | ||
self.selectedShape = shape | ||
Comment on lines
+48
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 부분도 shape의 속성을 가져와서 비교하는 게 아니라 shape.contains(x,y) 처럼 일을 시키면 좋을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 수정해보겠습니다! |
||
break | ||
} | ||
} | ||
guard let targetRectangle = selectedRectangle else {return} | ||
NotificationCenter.default.post(name: Plane.NotificationName.searchRectangle, object: self, userInfo: [Plane.NotificationKeyValue.rectangle:targetRectangle]) | ||
NotificationCenter.default.post(name: Plane.NotificationName.searchShape, object: self, userInfo: [Plane.NotificationKeyValue.shape:self.selectedShape]) | ||
} | ||
|
||
func updateAlphaValue(with alpha: Alpha) { | ||
self.selectedRectangle?.alpha = alpha | ||
self.selectedShape?.setAlpha(to: alpha) | ||
NotificationCenter.default.post(name: Plane.NotificationName.updateAlpha, object: self, userInfo: [Plane.NotificationKeyValue.alpha:alpha]) | ||
} | ||
|
||
func changeRandomColor(to randomColor : Color?) { | ||
guard let randomColor = randomColor else {return} | ||
self.selectedRectangle?.backgroundColor = randomColor | ||
func changeRandomColor(to randomColor : Color) { | ||
guard let rectangle = self.selectedShape as? Rectangle else {return} | ||
rectangle.setBackgroundColor(to: randomColor) | ||
NotificationCenter.default.post(name: Plane.NotificationName.changeColor, object: self, userInfo: [Plane.NotificationKeyValue.color:randomColor]) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// ViewObject.swift | ||
// DrawingApp | ||
// | ||
// Created by dale on 2022/03/16. | ||
// | ||
|
||
import Foundation | ||
|
||
class Shape{ | ||
private(set) var id : Id = Id() | ||
private(set) var size : Size | ||
private(set) var position : Position | ||
private(set) var alpha : Alpha | ||
|
||
init(size: Size, position : Position, alpha : Alpha) { | ||
self.size = size | ||
self.position = position | ||
self.alpha = alpha | ||
} | ||
|
||
func setAlpha(to alpha: Alpha) { | ||
self.alpha = alpha | ||
} | ||
} | ||
|
||
extension Shape: Equatable, Hashable { | ||
Comment on lines
+10
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어.. Shape가 프로토콜인 줄 알았는데 상위 클래스였네요. 상위 클래스가 있어도 되는데 프로토콜로 추상화할 부분도 시도해보세요. |
||
static func == (lhs: Shape, rhs: Shape) -> Bool { | ||
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shape 객체가 같다는 어떤 의미가 있을까요? https://lucas.codesquad.kr/masters-2022/course/모바일-iOS-클래스/포커게임-앱/카드덱-구현하고-테스트하기 에 참고자료로 올라온 내용 중에 Identity와 Equality를 다시 확인해보세요. 그래서 질문은 == 연산으로 무엇을 비교하려고 하는 것인가, 그 의도가 중요한 것 같습니다. class 경우는 === 연산도 있는데 어떤 역할을 하려는 것인가 궁금합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 처음 설계 당시에는 b와 같이 모든 속성이 동일하면 같다 라고 판단을 하였는데, 아주 적은 확률이라도 겹칠경우가 있어 두 도형의 참조값을 비교하는게 확실한 방법같아서 === 와 Objectidentifier사이에서 고민하게 되었습니다. |
||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,21 +7,21 @@ | |
|
||
import Foundation | ||
|
||
struct RectangleFactory { | ||
struct ShapeFactory { | ||
|
||
static func makeRandomRectangle(in screenSize : (Double,Double)) -> Rectangle? { | ||
static func makeRandomShape(in screenSize : (Double,Double)) -> Shape? { | ||
let randomPosition = RandomGenerator.generatePosition(screenSize: screenSize) | ||
let randomColor = RandomGenerator.generateColor() | ||
let randomAlpha = RandomGenerator.generateAlpha() | ||
|
||
return Rectangle(size: Size(width: 150, height: 120), position: randomPosition, color: randomColor, alpha: randomAlpha) | ||
} | ||
|
||
static func makePhotoRectangle(in screenSize : (Double, Double), imageData: Data) -> Rectangle? { | ||
static func makePhotoShape(in screenSize : (Double, Double), imageData: Data) -> Shape? { | ||
Comment on lines
+12
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 팩토리에서 생성하는 타입을 Shape 말고 프로토콜로도 추상화해보면 좋겠습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 추가로 추상화 진행해보겠습니다 |
||
let randomPosition = RandomGenerator.generatePosition(screenSize: screenSize) | ||
let randomAlpha = RandomGenerator.generateAlpha() | ||
|
||
return Rectangle(size: Size(width: 150, height: 120), position: randomPosition, imageData: imageData, alpha: randomAlpha) | ||
return PhotoRectangle(size: Size(width: 150, height: 120), position: randomPosition, imageData: imageData, alpha: randomAlpha) | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
항상 속성은 private var를 우선적으로 고려하세요