A simple factory method
Before the order
The simple factory method belongs to the factory pattern, so before we talk about the simple factory method, let’s talk about the factory pattern.
Definition of factory pattern: One class (factory class) is specifically defined to create instances of other classes (product class), often with a common parent. It is generally possible for a factory class to dynamically decide which instance of a product class to create based on the parameters passed in.
Factory mode is divided into factory method, simple factory method and abstract factory method. The factory pattern takes advantage of one of the three main features of object orientation – polymorphism, the property that a parent pointer points to a subclass object. The parent class defines the method, and the child class implements it.
Polymorphism: A parent class can have multiple subclasses, each subclass inherits the method of the parent class, rewrite the method of the parent class separately, then the method of each subclass is different, a parent class method is rewritten into a variety of forms, called polymorphism.
participants
- Factory role: Accepts requests from clients and is responsible for creating corresponding product objects through the requests.
- Abstract Product roles: Are parent classes or co-owned methods of objects created by the factory pattern.
- Product-specific roles: Objects created in the factory pattern are instances of this role (Product1 or Produc2)
Application: personal understanding, when the third party can be used, easy to replace. The system methods NSString and NSNumber apply the abstract factory method.
Reference links:
www.jianshu.com/p/6310deea5…
www.jianshu.com/p/c85531b24…
1. The model diagram
Characteristics of 2.
There is only one factory class, which centralizes the logic for all product creation. Static methods are usually used, which makes factory classes difficult to extend and cannot be inherited by subclasses.
3. Develop
Simple factory method can be divided into ordinary simple factory, multi-method simple factory and static simple factory. The principle is the same.
4. Call example
Ii. Factory Method
1. The model diagram
Definition 2.
The factory method uses OOP (Object-oriented Programming) polymorphism to abstract both the factory and the product into a base class, define unified methods in the base class, and then create the specific product in the specific factory.
3. Participants
- Abstract Factory role: Is the parent (Factory) or co-owned method of the Factory that created the object.
- Concrete factory roles: Concrete classes (Factory1, Factory2) that implement abstract factory methods and are called by the application to create product objects.
- Abstract Product roles: Are the parent products of objects created by factory methods or co-owned methods.
- Concrete product roles: This role implements what abstract product roles are known for. The objects created by the factory method are instances of a specific product role (Product1, Product2).
Characteristics of 4.
The cohesion of factory class is reduced, the hierarchical relationship between classes is satisfied, and the single responsibility principle in object-oriented design is met. It is beneficial to the expansion of the program. Each time a new product is needed, only a new specific factory is needed to generate a new product.
5. Call example
Abstract factory method
1. The model diagram
Principle 2.
Each abstract product is derived from multiple concrete product classes, each abstract factory is derived from multiple concrete factory classes, and each concrete factory is responsible for creating multiple (series) instances of concrete products. Applications :NSString, NSNumber, and so on all use this design.
3. Differences between abstract factory mode and factory mode:
Factory pattern: Each abstract product is derived from multiple concrete product classes, each abstract factory class is derived from multiple concrete factory classes, each concrete factory class is responsible for the creation of an instance of a concrete product; Abstract Factory pattern: Each abstract factory derives multiple concrete factory classes, and each concrete factory is responsible for creating multiple (series) instances of concrete products.
4. Participants
Abstract Factory role: What plays this role is the core of the factory method pattern. Initialize different concrete factory instances based on the incoming parameters. Concrete factory roles: Concrete classes that implement abstract factory methods and are called by the application to create product objects. Abstract Product roles: Are parent classes (Product, ProductPro) or co-owned methods of objects created by factory methods. Concrete product Roles: Any product object created by the Abstract factory pattern is an instance of a concrete product class.
5. Call example
Four singleton patterns
1. Example of singleton class
UIApplication(Application instance class)
NSNotificationCenter(Message Center class)
NSFileManager(File Management class)
NSUserDefaults(Application Settings)
NSURLCache(Request cache class)
NSHTTPCookieStorage(Application cookies pool)
2. Location of singletons in the phone’s memory
Once a singleton is created, the object pointer is stored in the static area, and the memory allocated by the singleton in the heap is freed only after the application terminates
3. sample code
Apple officially recommends dispatch_once_t
4. Advantages and disadvantages of the singleton pattern
-
Advantages:
-
In the entire program will only be instantiated once, so in the program if there is a problem, you can quickly locate the problem;
-
Because there is only one object in the whole program, the system memory resources are saved and the running efficiency of the program is improved.
-
-
disadvantages
-
Can’t be inherited, can’t have subclasses;
-
Not easily rewritten or extended (taxonomy can be used);
-
At the same time, because the singleton object will occupy system memory as long as the program runs, it cannot be destroyed when idle, and consumes system memory resources when idle.
-
5. Pay attention to
To avoid colleagues creating singletons more than once, disable these methods in the.h file and get an error when called.
+(instanceType)new __attribute__((unavailable(" The OneTimeClass class can only be initialized once ")); -(instanceType)copy __attribute__((unavailable("OneTimeClass class can only be initialized once ")); -(InstanceType) mutableCopy __attribute__((unavailable(" The OneTimeClass class can only be initialized once "));Copy the code
5. Intermediary model
Learning links: www.jianshu.com/p/8f103040f…
1. The model diagram
2. The role
- AbstractMediator: an AbstractMediator role that defines the interface between colleague objects and mediator objects, typically implemented as an abstract class. (UML diagrams should be dashed lines and triangles for interface inheritance)
- ConcreteMediator: ConcreteMediator role, inherited from abstract mediator, that implements methods defined by the parent class, receiving messages from concrete colleague objects and issuing commands to concrete colleague objects.
- Colleague(AbstractClient) : An abstract Colleague class role that defines an interface to a mediator object and is aware of only the mediator but not of other Colleague objects and holds the abstract mediator. Colleagues here mean classes that want to communicate directly, such as buying and selling a duplex through an intermediary.
- ConcreteColleague1, ConcreteColleague2: specific colleagues kind of role, inherit colleagues on abstract classes, each class specific colleagues know itself in a small range of behaviors, and don’t know the purpose of on a larger scale.
3. Demo
Demo Text analysis
Note: Intermediaries and colleagues can hold each other, note that references need to be weak.
- 1) AbstractMediator: AbstractMediator role, which defines the interface between colleague objects and mediator objects, generally implemented in the form of abstract classes. (UML diagrams should be dashed lines and triangles for interface inheritance)
- (2) ConcreteMediator(MediatorLJ) : ConcreteMediator role, inheriting from abstract mediator, implements methods defined by the parent class. It receives messages from concrete colleague objects and issues commands to concrete colleague objects.
- (3) Colleague(AbstractClient) : an abstract Colleague class role that defines an interface to an intermediary object and is only aware of the intermediary but not of other Colleague objects and holds an abstract intermediary. Colleagues here mean classes that want to communicate directly, such as buying and selling a duplex through an intermediary.
- (4) ConcreteColleague1 (Buyer), ConcreteColleague2 (Seller) : specific colleagues kind of role, inherit colleagues on abstract classes, each class specific colleagues know itself in a small range of behaviors, and don’t know the purpose of on a larger scale.
Strategy Mode Strategy
Definition 1.
This design pattern is a wrapper around behavior and algorithm, assigning the same abstract method to different subclass object implementations. And make them interchangeable. Strategy mode is the decoupling of multi-branch structures such as IF-else and switch-case, and encapsulates each case into a Strategy to simplify the controller code.
2. The difference between policy mode and factory mode
The factory pattern is more like managing objects, while the policy pattern is managing behavior.
3. The advantages and disadvantages
- advantages
- Algorithms can be switched freely.
- Avoid using multiple conditional judgments.
- Good scalability.
- disadvantages
- Policy classes will increase.
- All policy classes need to be exposed.
Write in the last
To be continued, I will fill in the rest of the design patterns when I have time