工厂模式

来源:互联网 发布:win10调整软件字体大小 编辑:程序博客网 时间:2024/06/05 02:25

1 什么是工厂模式?

什么是工厂方法?在基类中定义创建对象的一个接口,让子类决定实例化哪个类。工厂方法让一个类的实例化延迟到子类中进行。工厂方法要解决的问题是对象的创建时机,它提供了一种扩展的。

2 工厂模式案例

1)定义方式

#pragma mark 创建button

+(UIButton*)createButtonWithFrame:(CGRect)frame target:(id)target SEL:(SEL)method title:(NSString*)title

{

    UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];

    [button setTitle:titleforState:UIControlStateNormal];

    [button setTitleColor:[UIColorblueColor] forState:UIControlStateNormal];

    button.frame=frame;

    

    [button addTarget:targetaction:method forControlEvents:UIControlEventTouchUpInside];

    return button;

}

2)使用方式

 UIButton * nextLastImageButton = [MyControlcreateButtonWithFrame:CGRectMake(CGRectGetMaxX(myBackImageView.frame)-100,CGRectGetMaxY(myBackImageView.frame)-100,80, 80)target:selfSEL:@selector(showNextLast:)title:@""];


1 0
原创粉丝点击