You get a bonus - 1 coin for daily activity. Now you have 1 coin

Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Lecture



  • Mapper (Distributor)
  • Money
  • Special Case (Special Case)
  • Plugin
  • Gateway
  • Separated Interface (Dedicated Interface)
  • Registry (Registry)
  • Service Stub ( Service Stub )
  • Value Object ( Value Object )
  • Record Set ()
  • Layer Supertype (Supertype Level)
  • Singleton

Mapper (Distributor)

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Mapper Design Pattern

Mapper Description

An object that manages communication between objects that are independent of each other.

Sometimes it is necessary to establish a message between two subsystems, which, meanwhile, must remain in ignorance of each other. This may be due to the inability to change these objects, or simply unwillingness to create dependencies between them or between them and the insulating part.

Money

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Money Design Pattern

Money Description

A huge number of computers in the world process data about money. Surprisingly, the Money class is still not basic in any programming language. The lack of this type of data leads to problems, the most notable of which is working with currency. If all calculations in the program are done in one currency, there are no special problems, but as soon as multi-currency is introduced, you need to think about not folding $ 10 with 10 yen without translating currency rates. The problem with rounding is less noticeable. Cash calculations are often rounded to the smallest of existing measures. It is easy to ignore a penny due to rounding errors.

What is really good about OOP is that you can fix these problems by creating a Money class to work with monetary values ​​and avoid common mistakes.

Special Case (Special Case)

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Special Case Design Pattern

Description Special Case

A subclass containing special logic for individual situations.

Null-values ​​in OOP is a clumsy thing, as it hacks polymorphism at the root. It is usually possible to call any method on a variable of a given type without having to worry about whether this variable belongs to a particular class or subclass. In strongly typed languages, this check is done by the compiler. However, due to the fact that the variable may contain null, there is a danger of an error occurring at run time when the method is called on a null value.

If a variable can be null, you need to constantly take care of checks for null and the proper handling of null values. Often, this "correct handling" is the same in most cases, and all this ends in the commission of sin by duplicating the code (literal translation by Martin Fowler).

Null-values ​​are a vivid example of such problems that arise constantly and suddenly. And a lot of them. For example, in many systems one has to work with infinity, which has special rules for, for example, addition and violates ordinary axioms that are valid for natural numbers. Such cases imply a change in the usual type behavior.

Instead of returning null or some additional value, return Special Case (Special Case) - an object with the same interface, but behaving differently than the main one.

Plugin

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Pattern Design Plugin

Plugin Description

Connect classes during configuration, not compilation.

The Separated Interface pattern is often used when one code is executed in several environments and requires different implementations of separate logic. Most developers do this by using a factory pattern. Imagine that you need to generate a primary key using the Separated Interface pattern. For unit testing, you can use a simple counter object, and on a real system - a sequence from the database. The factory method is likely to contain a conditional transition (if), checking if the test flag is set, and return the necessary key generator.

As soon as you have a few more factories, confusion will begin. Creating a new configuration, for example, “launching unit tests on a database without transaction control” or “launching in production on DB2 with full transaction support”, will require changes in conditions in a large number of factories, reassembly and redevelopment.

The configuration should not be scattered across the application, as well as require rebuilding and redeployment. The Plugin pattern solves both of these problems by providing a centralized, dynamic configuration.

Gateway

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Gateway Design Pattern

Gateway Description

An object that encapsulates access to an external system and resource.

Decent software rarely functions in isolation from the outside world. Even the most strictly object-oriented system often has to interact with non-objects, such as a relational database, CICS transactions, or XML structures.

When accessing such external resources, the API is usually used. However, APIs are initially somewhat complex, because they take into account the structure of the resource. Anyone who wants to understand a resource must understand its API - be it JDBC or SQL for relational databases or W3C or JDOM for XML. This makes the software not only less understandable, but it also makes the changes much more complicated, for example, if you are going to switch from SQL to XML over time.

The solution here is to wrap the entire special API into a class whose interface looks like the interface of a regular object. The remaining objects access the resource through this Gateway, which translates these simple calls to the appropriate special API code.

Separated Interface (Dedicated Interface)

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Design Pattern Separated Interface

Description Separated Interface

Selecting an interface to an object in a separate package from the object.

When developing any system, it is possible to achieve improvement of its architecture, reducing connectivity between its parts. This can be done so - by distributing the classes into individual packages and control dependencies by these packages. Then you can follow the rules about how classes from one package can access classes from another package. For example, one that prohibits classes from the data layer from accessing classes from the presentation layer.

However, it may be necessary to implement methods that conflict with the underlying dependency structure. In this case, you can use a dedicated interface to define an interface in one package and implement it in another. Thus, any client that needs a dependency on this interface may not think at all about the implementation of access. The Separated Interface Pattern provides a good connection point for the Gateway pattern.

Registry (Registry)

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Registry Design Pattern

Registry description

A well-known object that is used by other objects to obtain common objects and services.

When you need to find an object, you usually start with another object associated with the target. For example, if you need to find all accounts for a buyer, start with the buyer and use his method of receiving bills. However, in some cases there is no suitable object from which to start. For example, the buyer's ID is known, but there is no link to it. Then you need a kind of search engine object, but then the question arises - how do you find the search engine itself?

The registry (Registry) - is a global object in its essence, or at least it looks like this - it can function only when it is global.

Service Stub ( Service Stub )

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Service Stub Design Pattern

Service Stub Description

Eliminates dependence on external problem services during testing.

Enterprise systems often depend on external services, such as, for example, calculating credit ratings, tax rates, etc. Any developer who has ever dealt with such systems will complain about their extra dependence on an absolutely uncontrolled resource. Often these services do not shine with stability and reliability.

As a result, these problems can slow development. Developers are forced to sit and wait for the remote service to resume its work or embed “crutches” into the code in order to bypass the dependency on the service. Even worse, when it fails to perform tests - the whole development process is broken.

Replacing the service with a stub (Service Stub), which is performed locally and quickly, will improve development.

Value Object ( Value Object )

Description Value Object

A small object for storing values ​​such as money or a range of dates whose equality is not based on identity.

When working with OOP, you come to the conclusion that it is useful to separate reference objects and value objects. The object value is usually much smaller. It is a simple data type from those languages ​​that are not fully object-oriented.

Record set

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Record Set Design Pattern

Record Set Description

Presentation of data from the table in the application.

Over the past twenty years, relational tables have become the main way of presenting data in a database. Almost every new developer uses relational data.

On this basis, a lot of tools appeared to quickly build a UI (user interface). These UI frameworks are based on data relationality and provide various UI elements that are easily customized and managed almost without any programming.

The flip side of the coin is that, despite the incredible ease of outputting and working with data, these elements do not provide for the possibility of adding a business logic code. Checks like "whether this date has the right format" and any rules of execution simply have nowhere to put. And as a result, this logic is either clogged in the database, or mixed in the information output code.

The essence of the Record Set is to provide a data structure that looks exactly like the result of a SQL query, but can be controlled and processed by any part of the system.

Layer Supertype (Supertype Level)

Layer Supertype Description

A type that acts as a parent for all types in its level.

Often, all classes of the same level have the same methods that do not want to duplicate everywhere. In order to avoid duplication, you can transfer all common methods into one class (Layer Supertype), which will be the Supertype (read - parent) of all classes in its level.

Singleton

  Basic Patterns Mapper, Money, Special Case, Plugin, Gateway, Separated Interface, Registry, Service Stub, Value Object, Record Set, Layer Supertype, Singleton

Singleton Design Pattern

Singleton description

In an application, an instance of a particular class must be present guaranteed in one instance.

In essence, a static instance of the class is created: protected from cloning, ordinary instantiation through the constructor, and other ways of obtaining a reference to a single instance — apart from the static constructor method.

Code example

PHP7 implementation example

namespace DesingPatternsRu \ Example;
/ **
*
* @author samizdam
* /
final class singleton
{
private static $ instance = null;
public static function getInstance (): self
{
if (empty (self :: $ instance)) {
self :: $ instance = new self ();
}
return self :: $ instance;
}
private function __conctorctor ()
{
// Protection from direct creation
}
private function __clone ()
{
// Protection against instance cloning
}
private function __sleep ()
{
// Protection against attempting to serialize with subsequent retrieval
}
}

view rawSingleton.php hosted with ❤ by GitHub

Pros and cons

Singleton is often called an “anti-pattern” because when used, it carries the following problems:

  • The static method of obtaining an instance creates a hard link, ignoring the dependency injection.
  • An application implicitly has some state.
  • "Single mother" is problematic to replace in a test environment.

These reasons reduce the testability of the code.

Since this is one of the easiest to understand patterns, it is often used by beginners, while abusing them. At the same time, for some tasks, when you need a global object, and the simplest way to access it from anywhere in the application (for example, Service Locator), the simplest solution is Singleton.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Web site or software design

Terms: Web site or software design