One of the design patterns: Introduction

Source: Internet
Author: User

The function of design pattern is to solve some repetitive public problems in software design. They are some of the templates to help you write code and reuse your code more easily. They may also help you create low-coupling code, and you can easily modify and replace the components in them.

If you are already familiar with the design pattern, I have some good suggestions here. First, you've used a lot of Cocoa to create IOS design patterns, and the best practice is to use them. Second, this tutorial will give you a full overview of all the latest IOS design patterns, and they are generally written by Cocoa.

This tutorial is divided into several sections. In each section, you can read the following instructions:

    • What is design mode

    • Why should you use her?

    • How to use, where to use, how to pay attention to some common pitfalls in using

In this tutorial, you will create a music library APP that will display your music albums and some related information.

In the process of developing this APP, you will learn a lot about common Cocoa design patterns.

    • Create (creational): Singleton (Singleton) and abstract factory (abstraction Factory)

    • Structure (Structural): MVC, Adorner (Decorator), adapter (Adapter), appearance (facade) and composite (Composite)

    • Behavior (Behavioral): Observer (Observer), memorandum (Memento), Chain of responsibility (Chain of Responsibility) and commands (command)

Don't be biased, it's just a theory article; you'll be using a lot of design patterns in your music APP. At the end of this tutorial, your APP should look something like this:

Getting Started Guide

Download startup item:cdn2.raywenderlich.com/wp-content/uploads/2013/07/bluelibrary-starter.zip (copy to PC download), extract file from zip file , open bluelibrary.xcodeproj with Xcode.

This does not have many files, just a default Viewcontroller file and an empty executable simple HTTP client.

 提示:你知道吗,当你创建一个新的 Xcode 项目的时候,你的代码已经使用被设计模式?MVC,代理,协议,单例模式 ─ 这些你都能免费得到!:]

When you start researching the first design pattern, you have to create two classes to store and display the album data.

From "File\new\file ..." (or using shortcut key Command + N). Select IOS > Cocoa Touch, then select Objective-c class and click Next. The name of the set class is Album and the subclass is NSObject. Click Next to create.

Open the Album.h file and add the following properties and method prototypes to the @interface and @end:

@propery (nonatomic, copy, readonly) NSString *title, *artist, * genre, *coverUrl, *year;- (id)initWithTitle:(NSString *)title artist:(NSString *)artist coverUrl:(NSString *)coverUrl year:(NSString *)year;

Note that all of the properties here are Readonly,album objects that cannot be modified after they are created.

This method is used to initialize the object. When you create a new album, you need to provide the album name, author, cover URL, and year.

Now open the ALBUM.M file and add the following code between the @implementation and @end:

- (id)initWithTitle:(NSString*)title artist:(NSString*)artist coverUrl:(NSString*)coverUrl{    self = [super init];    _title = title;    _artist = artist;    _coverUrl = coverUrl;    _year = year;    _genre = @"Pop";    return self;}

There is nothing complicated here, just a simple initialization method that creates a Album instance.

Now select Cocoa Touch from the menu file\new\file, then select Object-c class and click Next. The name of the set class is Albumview and the subclass is UIView. Click Next to create the file.

提示:如果你会使用快捷键,创建这些会更容易,Command+N 创建新文件,Command+Option+N 创建新的文件夹,Command+B 创建新项目,Command+R 运行项目。

Open AlbumView.h, add a method prototype in @interface and @end

- (id)initWithFrame:(CGRect)fram albumCover:(NSString*)albumCover;

Now open the albumview.m file and replace all the code behind @implementaton with the following code:

@implementation AlbumView{    UIImageView *coverImage;    UIActivityIndicatorView *indicator;}- (id)initWithFrame:(CGRect)frame albumCover:(NSString*)albumCover{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor blackColor];        // the coverImage has a 5 pixels margin from its frame        coverImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, frame.size.width-10, frame.size.height-10)];        [self addSubview:coverImage];        indicator = [[UIActivityIndicatorView alloc] init];        indicator.center = self.center;        indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;        [indicator startAnimating];        [self addSubview:indicator];        }    return self;}@end

The first place to note is that Coverimage is an instance variable. This variable represents the cover picture of the album. The second variable is a prompt that prompts the user to download when the cover image starts downloading.

During initialization you need to set the background to black, create a picture view that is 5px larger than the cover picture, and then create and add an activity indicator.

提示:为什么私有变量定义在执行文件而不是定义在接口文件?这是因为其它外部类不需要知道 AlbumView 类里的这些变量,这些变量只会在这个类的内部方法中使用到。如果你要开发一些库或者框架给别的开发者使用,遵守这个习俗是非常重要的。

Build your project to make sure all the code is running. No problem? Your first design pattern is ready.

One of the design patterns: Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.