1 什么是工厂模式?
什么是工厂方法?在基类中定义创建对象的一个接口,让子类决定实例化哪个类。工厂方法让一个类的实例化延迟到子类中进行。工厂方法要解决的问题是对象的创建时机,它提供了一种扩展的。
2 工厂模式案例
1)定义方式
#pragma mark 创建button
+(UIButton*)createButtonWithFrame:(CGRect)frame target:(id)target SEL:(SEL)method title:(NSString*)title
{
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
button.frame=frame;
[button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
return button;
}
2)使用方式
UIButton * nextLastImageButton = [MyControl createButtonWithFrame:CGRectMake(CGRectGetMaxX(myBackImageView.frame)-100, CGRectGetMaxY(myBackImageView.frame)-100, 80, 80) target:self SEL:@selector(showNextLast:) title:@""];