Ios 最佳编码实践-在同一函数中画线和圆-结构、开关、类函数
我学习和编写swift已经有几个月了,当然我仍然在问自己某些解决方案的最佳实践是什么: 问题是: 我需要在200x200px的图像上打印一条随机高度的细线。 同样的功能还应该在该图像上打印一个固定大小的圆 构建这个功能并不是什么大不了的事,我倒是在问自己,我的解决方案是不是最好的方法。基本上我有一个带有类函数的util类 类函数中有一个基于枚举的switch语句,用于打印直线还是圆。 我正在考虑按照facade模式将交换机分成两个不同的功能。你觉得怎么样 这是代码,这是代码Ios 最佳编码实践-在同一函数中画线和圆-结构、开关、类函数,ios,swift,Ios,Swift,我学习和编写swift已经有几个月了,当然我仍然在问自己某些解决方案的最佳实践是什么: 问题是: 我需要在200x200px的图像上打印一条随机高度的细线。 同样的功能还应该在该图像上打印一个固定大小的圆 构建这个功能并不是什么大不了的事,我倒是在问自己,我的解决方案是不是最好的方法。基本上我有一个带有类函数的util类 类函数中有一个基于枚举的switch语句,用于打印直线还是圆。 我正在考虑按照facade模式将交换机分成两个不同的功能。你觉得怎么样 这是代码,这是代码 该函数违反了实体原则
该函数违反了实体原则,因为它很少绘制图形。你的想法是正确的。最好的解决方案是只有很少的功能,并且在上面有一个门面,这个功能违反了坚实的原则,因为它很少绘制图形。你的想法是正确的。最好的解决方案是使用很少的功能,并且在上面有一个门面。这个例子看起来不错。只是在每种情况下写太多的行似乎不是一个好的练习。相反,您可以创建函数并使用它们。所以,若您愿意,可以在项目的其他地方重用相同的代码。还有 Facade模式表示,您的类本身所做的逻辑不应该被项目中的其他类知道。因此,根据我的建议,您的代码应如下所示:
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/*
draw a line or a circle on an image canvas
*/
/* different printing function types */
enum FuncTypes: String {
case Line
case Circle
case PrintBaseHeight
case Default
}
final class ImageUtils{
class func drawLineOnImage(funcType: FuncTypes) -> UIImage{
//def vars
let drawHeight = 200
let drawWidth = 200
// var rectangle = CGRect(x: 0, y: 0, width: 0, height: 0)
UIGraphicsBeginImageContextWithOptions(CGSize(width: drawWidth, height: drawHeight), false, 0)
var context = UIGraphicsGetCurrentContext()
//flipp-coords
CGContextTranslateCTM(context, 0, CGFloat(drawHeight));
CGContextScaleCTM(context, 1.0, -1.0);
//get height and color for line
// let LineHeight = Int(arc4random_uniform(200) )
let LineColor = UIColor.cyanColor()
// var heightPercent = 0
//context stuff
CGContextSetLineWidth(context, 1)
//switch some func cases for image height
switch funcType {
case .Line:
print("Recording")
drawLine(&context!, height: drawHeight, LineColor: LineColor)
case .Circle:
print("image printingmarker")
drawCircle(&context!, height: drawHeight, LineColor: LineColor)
default:
break
}
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
static func drawLine (inout context:CGContext, height:Int, LineColor : UIColor) {
CGContextAddArc(context, 2.5, CGFloat(height/2), 2.5, 0, CGFloat(M_PI * 2), 0)
CGContextSetFillColorWithColor(context,LineColor.CGColor)
CGContextSetStrokeColorWithColor(context,LineColor.CGColor)
CGContextDrawPath(context, .FillStroke)
}
static func drawCircle (inout context:CGContext, height:Int, LineColor : UIColor) {
let LineHeight = Int(arc4random_uniform(200) )
var heightPercent = 0
CGContextMoveToPoint(context,0, 0)
heightPercent = LineHeight*55/100
CGContextSetAlpha(context,0.4);
CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
let rectangle = CGRect(x: 0, y: height/2, width: 4, height: Int(heightPercent))
CGContextAddRect(context, rectangle)
CGContextStrokePath(context)
CGContextSetFillColorWithColor(context,LineColor.CGColor)
CGContextFillRect(context, rectangle)
}
}
let lineOnImage = ImageUtils.drawLineOnImage(FuncTypes.Line)
let circleOnImahe = ImageUtils.drawLineOnImage(FuncTypes.Circle)
榜样似乎很好。只是在每种情况下写太多的行似乎不是一个好的练习。相反,您可以创建函数并使用它们。所以,若您愿意,可以在项目的其他地方重用相同的代码。还有 Facade模式表示,您的类本身所做的逻辑不应该被项目中的其他类知道。因此,根据我的建议,您的代码应如下所示:
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/*
draw a line or a circle on an image canvas
*/
/* different printing function types */
enum FuncTypes: String {
case Line
case Circle
case PrintBaseHeight
case Default
}
final class ImageUtils{
class func drawLineOnImage(funcType: FuncTypes) -> UIImage{
//def vars
let drawHeight = 200
let drawWidth = 200
// var rectangle = CGRect(x: 0, y: 0, width: 0, height: 0)
UIGraphicsBeginImageContextWithOptions(CGSize(width: drawWidth, height: drawHeight), false, 0)
var context = UIGraphicsGetCurrentContext()
//flipp-coords
CGContextTranslateCTM(context, 0, CGFloat(drawHeight));
CGContextScaleCTM(context, 1.0, -1.0);
//get height and color for line
// let LineHeight = Int(arc4random_uniform(200) )
let LineColor = UIColor.cyanColor()
// var heightPercent = 0
//context stuff
CGContextSetLineWidth(context, 1)
//switch some func cases for image height
switch funcType {
case .Line:
print("Recording")
drawLine(&context!, height: drawHeight, LineColor: LineColor)
case .Circle:
print("image printingmarker")
drawCircle(&context!, height: drawHeight, LineColor: LineColor)
default:
break
}
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
static func drawLine (inout context:CGContext, height:Int, LineColor : UIColor) {
CGContextAddArc(context, 2.5, CGFloat(height/2), 2.5, 0, CGFloat(M_PI * 2), 0)
CGContextSetFillColorWithColor(context,LineColor.CGColor)
CGContextSetStrokeColorWithColor(context,LineColor.CGColor)
CGContextDrawPath(context, .FillStroke)
}
static func drawCircle (inout context:CGContext, height:Int, LineColor : UIColor) {
let LineHeight = Int(arc4random_uniform(200) )
var heightPercent = 0
CGContextMoveToPoint(context,0, 0)
heightPercent = LineHeight*55/100
CGContextSetAlpha(context,0.4);
CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
let rectangle = CGRect(x: 0, y: height/2, width: 4, height: Int(heightPercent))
CGContextAddRect(context, rectangle)
CGContextStrokePath(context)
CGContextSetFillColorWithColor(context,LineColor.CGColor)
CGContextFillRect(context, rectangle)
}
}
let lineOnImage = ImageUtils.drawLineOnImage(FuncTypes.Line)
let circleOnImahe = ImageUtils.drawLineOnImage(FuncTypes.Circle)