| Start |
|---|
| Singleton Singletons are a frequently used design pattern which consists of a single instance of a class that is shared throughout a program. In the following example, we create a Usage: Be sure to remember the This makes sure your singletons are truly unique and prevents outside objects from creating their own instances of your class through virtue of access control. Since all objects come with a default public initializer in Swift, you need to override your init and make it private. KrakenDev (opens new window) |
| Builder Pattern The builder pattern is an **object creation software design pattern**. Unlike the abstract factory pattern and the factory method pattern whose intention is to enable polymorphism, the intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern. The telescoping constructor anti-pattern occurs when the increase of object constructor parameter combination leads to an exponential list of constructors. Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once. [**-Wikipedia**](https://en.wikipedia.org/wiki/Builder_pattern)The main goal of the builder pattern is to setup a default configuration for an object from its creation. It is an intermediary between the object will be built and all other objects related to building it. Example:To make it more clear, let's take a look at a Car Builder example. Consider that we have a Car class contains many options to create an object, such as:
Creating a car object: The problem arises when creating a car object is that the car requires many configuration data to be created. For applying the Builder Pattern, the initializer parameters should have default values which are changeable if needed. CarBuilder class: The Let's build new cars by using the The benefit of applying the Builder Pattern is the ease of creating objects that should contain much of configurations by setting default values, also, the ease of changing these default values. Take it Further:As a good practice, all properties that need default values should be in a separated protocol, which should be implemented by the class itself and its builder. Backing to our example, let's create a new protocol called The benefit of declaring the properties that need default value into a protocol is the forcing to implement any new added property; When a class conforms to a protocol, it has to declare all its properties/methods. Consider that there is a required new feature that should be added to the blueprint of creating a car called "battery name": After adding the new property, note that two compile-time errors will arise, notifying that conforming to After adding Again, let's build new cars by using the |
| Factory Method In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. Wikipedia reference (opens new window) By doing that we don't depend on the real implementation of the class, making the In this case all we need to know is that a sender will handle the deliver and exposes a method called Within object-oriented design, interfaces provide layers of abstraction that facilitate conceptual explanation of the code and create a barrier preventing dependencies.Wikipedia reference (opens new window) |
| Observer The observer pattern is where an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. The Observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern.Wikipedia reference (opens new window) Basically the observer pattern is used when you have an object which can notify observers of certain behaviors or state changes. First lets create a global reference (outside of a class) for the Notification Centre : Great now we can call this from anywhere. We would then want to register a class as an observer... This adds the class as an observer for "readForMyFunc". It also indicates that the function myFunc should be called when that notification is received. This function should be written in the same class: One of the advantages to this pattern is that you can add many classes as observers and thus perform many actions after one notification. The notification can now simply be sent(or posted if you prefer) from almost anywhere in the code with the line: You can also pass information with the notification as a Dictionary But then you need to add a notification to your function: |
| Chain of responsibility In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of Setting up the classes that made up the chain of responsibility. First we create an interface for all the Then we create the Finally, creating objects that made up the chain of responsibility. Initiate and chaining it together : The mechanism for chaining up objects here is property access Creating request to run it : |
| Iterator In computer programming an iterator is an object that enables a programmer to traverse a container, particularly lists. [Wikipedia](https://en.wikipedia.org/wiki/Iterator) And usage example would be |
Read Article
https://develop.horje.com/learn/1434/reference