当前位置: 首页 > news >正文

做网站必须要认证吗百度推广平台登陆

做网站必须要认证吗,百度推广平台登陆,吴江城乡和住房建设局网站,网站设计的建议文章目录 前言设计模式的三大原则简单工厂模式工厂方法模式抽象工厂模式关于三兄弟的升级与降级注意 前言 上文讲完了iOS的架构模式,接下来聊一聊设计模式,设计模式有许多,主要介绍一下工厂模式 设计模式的三大原则 S 单一职责原则 告诉我…

文章目录

  • 前言
  • 设计模式的三大原则
  • 简单工厂模式
  • 工厂方法模式
  • 抽象工厂模式
  • 关于三兄弟的升级与降级
  • 注意


前言

上文讲完了iOS的架构模式,接下来聊一聊设计模式,设计模式有许多,主要介绍一下工厂模式

设计模式的三大原则

  • S 单一职责原则
    告诉我们实现类要职责单一;
    例如UIView负责处理事件传递响应,CALayer负责动画与视图的显示
  • O 开闭原则是总纲,它告诉我们要对扩展开放,对修改关闭;
// 基本的绘制类
@interface Shape : NSObject
- (void)draw;
@end// 扩展新的形状,无需修改现有代码
@interface Circle : Shape
@end@implementation Circle
- (void)draw {// 绘制圆形的逻辑
}
@end
  • L 里氏替换原则告诉我们不要破坏继承体系;
@interface Bird : NSObject
- (void)fly;
@end@interface Duck : Bird
@end@implementation Duck
- (void)fly {// 实现飞行
}
@end
  • D 迪米特法则。 一个对象应当对其他对象尽可能少的了解,实现高聚合、低耦合
// 班级类
@interface Class : NSObject
- (NSArray *)getAllStudents;
@end// 学校类
@interface School : NSObject
@property (strong, nonatomic) Class *class;
- (NSArray *)getAllStudentsInClass;
@end@implementation School
- (NSArray *)getAllStudentsInClass {return [self.class getAllStudents];
}
@end
  • I 接口隔离原则: 使用多个专门的协议、而不是一个庞大臃肿的协议
@protocol Printer
- (void)printDocument;
@end@protocol Scanner
- (void)scanDocument;
@end@interface MultiFunctionMachine : NSObject <Printer, Scanner>
@end@implementation MultiFunctionMachine
- (void)printDocument {// 打印文档
}- (void)scanDocument {// 扫描文档
}
@end
  • D 依赖倒置原则。抽象不应该依赖于具体实现、具体实现可以依赖于抽象。 调用接口感觉不到内部是如何操作的

定义协议(接口)

首先,我们定义一个文件读取的协议,这个协议负责声明读取文件内容的方法。

// FileReaderProtocol.h
#import <Foundation/Foundation.h>@protocol FileReaderProtocol <NSObject>
- (NSString *)readContent;
@end

具体实现类

接下来,实现这个协议。这里我们可以有多种实现方式,例如从本地文件系统读取,或是从网络获取。

// LocalFileReader.h
#import <Foundation/Foundation.h>
#import "FileReaderProtocol.h"@interface LocalFileReader : NSObject <FileReaderProtocol>
@property (strong, nonatomic) NSString *filePath;
- (instancetype)initWithFilePath:(NSString *)filePath;
@end// LocalFileReader.m
#import "LocalFileReader.h"@implementation LocalFileReader- (instancetype)initWithFilePath:(NSString *)filePath {self = [super init];if (self) {_filePath = filePath;}return self;
}- (NSString *)readContent {NSError *error;NSString *content = [NSString stringWithContentsOfFile:self.filePathencoding:NSUTF8StringEncodingerror:&error];if (error) {NSLog(@"Error reading file: %@", error.localizedDescription);return nil;}return content;
}@end

简单工厂模式

简单工厂模式并不是一个正式的设计模式,更多的是一种变成习惯
其主要通过传入参数给唯一的工厂类来创建不同类型的对象

但是这样就会出现一个问题,当我们需要添加新的类型,也就是添加新的产品时,我们必须去修改工厂类,这就违反了开闭原则

// 创建产品协议
@protocol Product <NSObject>
- (void)use;
@end// 具体产品A
@interface ProductA : NSObject <Product>
- (void)use;
@end@implementation ProductA
- (void)use {NSLog(@"Using Product A");
}
@end// 具体产品B
@interface ProductB : NSObject <Product>
- (void)use;
@end@implementation ProductB
- (void)use {NSLog(@"Using Product B");
}
@end// 简单工厂
@interface ProductFactory : NSObject
+ (id<Product>)productWithType:(NSString *)type;
@end@implementation ProductFactory
+ (id<Product>)productWithType:(NSString *)type {if ([type isEqualToString:@"A"]) {return [ProductA new];} else if ([type isEqualToString:@"B"]) {return [ProductB new];}return nil;
}
@end// 使用
ProductFactory *factory = [ProductFactory new];
id<Product> productA = [ProductFactory productWithType:@"A"];
[productA use];
// 首先创建一个工厂,根据传入的参数觉得工厂生产什么产品

在这里插入图片描述

工厂方法模式

工厂方法模式同样只是生产一种产品,但是一种产品可以有多个品牌

举个例子,我们的可乐工厂可以生产可乐,其即可以生产百事可乐,也可以生产可口可乐

首先我们需要定义一个创建对象的接口,但让子类决定要实例化的类是哪一个。工厂方法让类的实例化延迟到子类进行。
也就是抽象工厂是父类,具体工厂是子类,只有确定了具体工厂才能决定生产的产品是哪一种品牌的

// 定义产品接口
@protocol Logger <NSObject>
- (void)logMessage:(NSString *)message;
@end// 具体产品类 FileLogger
@interface FileLogger : NSObject <Logger>
- (void)logMessage:(NSString *)message;
@end@implementation FileLogger
- (void)logMessage:(NSString *)message {NSLog(@"File logger: %@", message);
}
@end// 具体产品类 NetworkLogger
@interface NetworkLogger : NSObject <Logger>
- (void)logMessage:(NSString *)message;
@end@implementation NetworkLogger
- (void)logMessage:(NSString *)message {NSLog(@"Network logger: %@", message);
}
@end// 抽象工厂类
@interface LoggerFactory : NSObject
- (id<Logger>)createLogger;
@end// 具体工厂类 FileLoggerFactory
@interface FileLoggerFactory : LoggerFactory
- (id<Logger>)createLogger;
@end@implementation FileLoggerFactory
- (id<Logger>)createLogger {return [[FileLogger alloc] init];
}
@end// 具体工厂类 NetworkLoggerFactory
@interface NetworkLoggerFactory : LoggerFactory
- (id<Logger>)createLogger;
@end@implementation NetworkLoggerFactory
- (id<Logger>)createLogger {return [[NetworkLogger alloc] init];
}
@end// 使用
LoggerFactory *factory;
if (useFileLogging) {factory = [[FileLoggerFactory alloc] init];
} else {factory = [[NetworkLoggerFactory alloc] init];
}
id<Logger> logger = [factory createLogger];
[logger logMessage:@"This is a test log."];

在这个例子中,LoggerFactory 是一个抽象类,定义了一个名为 createLogger 的方法,但并没有实现它。这个方法的具体实现由 FileLoggerFactoryNetworkLoggerFactory 这两个子类根据不同的业务逻辑来提供。这样,每个工厂子类决定了要实例化的具体 Logger 类,父类不需要知道具体的实现细节。

这种模式的优势在于,它支持易于扩展和修改的开放封闭原则,同时减少了类间的耦合。

我们再来看一个场景:
假设我们正在开发一个简单的绘图应用,该应用可以绘制不同类型的图形。我们可以使用工厂方法模式来创建不同类型的图形对象。

步骤 1: 定义图形接口和具体图形类。

// Shape.h
#import <Foundation/Foundation.h>@protocol Shape <NSObject>
- (void)draw;
@end// Circle.h
#import "Shape.h"@interface Circle : NSObject <Shape>
@end@implementation Circle
- (void)draw {NSLog(@"Drawing a circle.");
}
@end// Rectangle.h
#import "Shape.h"@interface Rectangle : NSObject <Shape>
@end@implementation Rectangle
- (void)draw {NSLog(@"Drawing a rectangle.");
}
@end

步骤 2: 定义抽象工厂类和具体工厂类。

// ShapeFactory.h
#import <Foundation/Foundation.h>
#import "Shape.h"@interface ShapeFactory : NSObject
- (id<Shape>)createShape;
@end// CircleFactory.h
#import "ShapeFactory.h"
#import "Circle.h"@interface CircleFactory : ShapeFactory
@end@implementation CircleFactory
- (id<Shape>)createShape {return [[Circle alloc] init];
}
@end// RectangleFactory.h
#import "ShapeFactory.h"
#import "Rectangle.h"@interface RectangleFactory : ShapeFactory
@end@implementation RectangleFactory
- (id<Shape>)createShape {return [[Rectangle alloc] init];
}
@end

步骤 3: 使用工厂类。

// main.m
#import <Foundation/Foundation.h>
#import "CircleFactory.h"
#import "RectangleFactory.h"int main(int argc, const char * argv[]) {@autoreleasepool {// 创建圆形工厂和矩形工厂ShapeFactory *circleFactory = [[CircleFactory alloc] init];ShapeFactory *rectangleFactory = [[RectangleFactory alloc] init];// 使用工厂方法创建形状id<Shape> circle = [circleFactory createShape];id<Shape> rectangle = [rectangleFactory createShape];// 绘制形状[circle draw];[rectangle draw];}return 0;
}

主程序依赖于抽象接口(Shape 协议和 ShapeFactory 类),而不是具体的类

我们来理解一下这句话

想象我们正在开发一个图形软件,软件需要支持多种类型图形,我们此时不需要知道操作的图形具体是什么,使用接口来定义这些操作,可以让你的软件支持任意类型的图形

定义抽象接口(如 Shape)

@protocol Shape <NSObject>
- (void)draw;
@end

实现具体类(如 Circle 和 Rectangle):

@interface Circle : NSObject <Shape>
@end@implementation Circle
- (void)draw {NSLog(@"Drawing a circle.");
}
@end@interface Rectangle : NSObject <Shape>
@end@implementation Rectangle
- (void)draw {NSLog(@"Drawing a rectangle.");
}
@end

CircleRectangle 类都实现了 Shape 协议,但具体的绘制逻辑根据图形的类型不同而不同。

抽象工厂模式

抽象工厂模式与工厂模式关键区别在于抽象工厂是用来创建一系列产品的,每个产品可以属于不同的产品类别,而工厂方法一般只创建一种产品。

场景:
假设我们有一个应用程序,需要支持多种界面风格,如“暗黑模式”和“亮色模式”,每种风格都有一套不同的界面组件(如按钮、文本框等)。我们可以使用抽象工厂模式来根据用户的选择动态提供相应的组件。

  • 步骤 1: 定义抽象产品和具体产品

首先,我们定义两种产品接口:Button 和 TextField,以及它们的具体实现。

// Button.h
@protocol Button <NSObject>
- (void)display;
@end// DarkButton.h
#import "Button.h"@interface DarkButton : NSObject <Button>
@end@implementation DarkButton
- (void)display {NSLog(@"Displaying dark button");
}
@end// LightButton.h
#import "Button.h"@interface LightButton : NSObject <Button>
@end@implementation LightButton
- (void)display {NSLog(@"Displaying light button");
}
@end// TextField.h
@protocol TextField <NSObject>
- (void)display;
@end// DarkTextField.h
#import "TextField.h"@interface DarkTextField : NSObject <TextField>
@end@implementation DarkTextField
- (void)display {NSLog(@"Displaying dark text field");
}
@end// LightTextField.h
#import "TextField.h"@interface LightTextField : NSObject <TextField>
@end@implementation LightTextField
- (void)display {NSLog(@"Displaying light text field");
}
@end
  • 步骤 2: 定义抽象工厂和具体工厂

定义一个抽象工厂接口,GUIFactory,以及为每种界面风格实现一个具体工厂。

// GUIFactory.h
#import "Button.h"
#import "TextField.h"@protocol GUIFactory <NSObject>
- (id<Button>)createButton;
- (id<TextField>)createTextField;
@end// DarkGUIFactory.h
#import "GUIFactory.h"
#import "DarkButton.h"
#import "DarkTextField.h"@interface DarkGUIFactory : NSObject <GUIFactory>
@end@implementation DarkGUIFactory
- (id<Button>)createButton {return [DarkButton new];
}
- (id<TextField>)createTextField {return [DarkTextField new];
}
@end// LightGUIFactory.h
#import "GUIFactory.h"
#import "LightButton.h"
#import "LightTextField.h"@interface LightGUIFactory : NSObject <GUIFactory>
@end@implementation LightGUIFactory
- (id<Button>)createButton {return [LightButton new];
}
- (id<TextField>)createTextField {return [LightTextField new];
}
@end
  • 步骤 3: 使用抽象工厂

客户端代码现在可以根据用户的偏好选择使用合适的工厂,而不需要关心具体的产品如何创建。

// main.m
#import <Foundation/Foundation.h>
#import "DarkGUIFactory.h"
#import "LightGUIFactory.h"int main(int argc, const char * argv[]) {@autoreleasepool {id<GUIFactory> factory;BOOL useDarkMode = YES;  // 用户选择使用暗黑模式if (useDarkMode) {factory = [[DarkGUIFactory alloc] init];} else {factory = [[LightGUIFactory alloc] init];}id<Button> button = [factory createButton];id<TextField> textField = [factory createTextField];[button display];[textField display];}return 0;
}

关于三兄弟的升级与降级

  • 单一类型产品比较少时,用简单工厂模式。
  • 单一类型产品各种定制比较多时,用工厂模式。
  • 多种类型产品时,使用抽象工厂模式。

注意

我们注意到定义产品类前我们通常会先定义一个抽象产品接口,也就是协议,例如:

// 定义产品接口
@protocol Logger <NSObject>
- (void)logMessage:(NSString *)message;
@end// 具体产品类 FileLogger
@interface FileLogger : NSObject <Logger>
- (void)logMessage:(NSString *)message;
@end

同时还有在主程序中创建对象

// 使用工厂方法创建形状id<Shape> circle = [circleFactory createShape];id<Shape> rectangle = [rectangleFactory createShape];

这就符合我们的依赖倒置原则

主程序依赖于抽象接口(Shape 协议和 ShapeFactory 类),而不是具体的类


文章转载自:
http://electrosurgery.rpwm.cn
http://cuckoo.rpwm.cn
http://coccid.rpwm.cn
http://almoner.rpwm.cn
http://sourkrout.rpwm.cn
http://cleistogamy.rpwm.cn
http://gao.rpwm.cn
http://metaphysician.rpwm.cn
http://jailbreak.rpwm.cn
http://rotorcraft.rpwm.cn
http://nebula.rpwm.cn
http://anthotaxy.rpwm.cn
http://centavo.rpwm.cn
http://quartile.rpwm.cn
http://finnick.rpwm.cn
http://duckbill.rpwm.cn
http://inwit.rpwm.cn
http://overindulge.rpwm.cn
http://karzy.rpwm.cn
http://landskip.rpwm.cn
http://understrapper.rpwm.cn
http://hackmatack.rpwm.cn
http://savanna.rpwm.cn
http://shansi.rpwm.cn
http://discreetly.rpwm.cn
http://lcvp.rpwm.cn
http://unrequited.rpwm.cn
http://fishskin.rpwm.cn
http://numismatician.rpwm.cn
http://monacid.rpwm.cn
http://namable.rpwm.cn
http://gastrea.rpwm.cn
http://podocarp.rpwm.cn
http://resuscitable.rpwm.cn
http://unenlightened.rpwm.cn
http://indraught.rpwm.cn
http://peseta.rpwm.cn
http://filterability.rpwm.cn
http://drail.rpwm.cn
http://redemonstrate.rpwm.cn
http://pestilence.rpwm.cn
http://lettuce.rpwm.cn
http://ghastful.rpwm.cn
http://uvual.rpwm.cn
http://hunks.rpwm.cn
http://saponite.rpwm.cn
http://aspiration.rpwm.cn
http://hexapartite.rpwm.cn
http://moonstone.rpwm.cn
http://hermetic.rpwm.cn
http://indirection.rpwm.cn
http://senary.rpwm.cn
http://discoverable.rpwm.cn
http://bengaline.rpwm.cn
http://dance.rpwm.cn
http://gimcracky.rpwm.cn
http://visitant.rpwm.cn
http://armenia.rpwm.cn
http://roric.rpwm.cn
http://neurular.rpwm.cn
http://fantod.rpwm.cn
http://constatation.rpwm.cn
http://havoc.rpwm.cn
http://scribbler.rpwm.cn
http://giglot.rpwm.cn
http://behaviorist.rpwm.cn
http://microorder.rpwm.cn
http://legharness.rpwm.cn
http://costermonger.rpwm.cn
http://gutty.rpwm.cn
http://ifip.rpwm.cn
http://progenitress.rpwm.cn
http://ozonic.rpwm.cn
http://smallholder.rpwm.cn
http://recollectedly.rpwm.cn
http://toscana.rpwm.cn
http://spiky.rpwm.cn
http://cooer.rpwm.cn
http://achievement.rpwm.cn
http://porphyritic.rpwm.cn
http://tarantism.rpwm.cn
http://shroff.rpwm.cn
http://nannar.rpwm.cn
http://bidirectional.rpwm.cn
http://breathtaking.rpwm.cn
http://heliodor.rpwm.cn
http://foregather.rpwm.cn
http://rationalism.rpwm.cn
http://cantillate.rpwm.cn
http://ethinyl.rpwm.cn
http://stovepipe.rpwm.cn
http://caulocarpous.rpwm.cn
http://roulade.rpwm.cn
http://abskize.rpwm.cn
http://anatolian.rpwm.cn
http://interlinear.rpwm.cn
http://reparatory.rpwm.cn
http://speakeress.rpwm.cn
http://wecker.rpwm.cn
http://clamper.rpwm.cn
http://www.15wanjia.com/news/60903.html

相关文章:

  • 公司网站设计费计入什么科目网店推广的方式
  • wordpress 页脚插件优化大师怎么下载
  • 开一个网站建设公司需要什么软件推广赚钱
  • wordpress中国网站排名厦门seo优化外包公司
  • 合肥做网站的公司讯登黑帽seo是作弊手法
  • wordpress文章阅读权限泰州seo网站推广
  • php网站开发教程图片2022重大时政热点事件简短
  • 个人博客网站需求分析软文的目的是什么
  • 网站文章做百度排名seo工具包括
  • 深圳做网站的给说郑州网站推广优化
  • 做网站公司名字网站seo优化总结
  • 网站怎么伪静态网站网络营销课程论文
  • 多个网站备案吗阿里云com域名注册
  • wordpress调用图片代码seo快速排名软件网站
  • 效果图参考网站有哪些刷外链
  • vs做网站如何输出网址注册在哪里注册
  • 网站开发项目管理文档模板今日热点新闻头条国内
  • nba排名seo排名怎么优化软件
  • 高端制作网站公司厦门seo排名收费
  • 万户网站建设专业网站优化外包
  • 洮南市城乡和住房建设局网站互联网运营推广
  • wordpress做淘宝客网站中国新冠一共死去的人数
  • 东莞做网站优化的公司论坛外链代发
  • wordpress所含数据库文件深圳市seo网络推广哪家好
  • 网站开发课程内部培训seo上海公司
  • 网站点击量怎么查品牌推广方案范文
  • 帮忙注册公司有名的seo外包公司
  • 做链家房产的网站怎么做的网站注册时间查询
  • 财务公司代理记账怎么收费系统优化大师下载
  • 秦皇岛市住房和城乡建设局网站国内营销推广渠道