Welcome to my personal blog!
Here you can find some kind of useful information about development and supporting systems. Now I learn design patterns and after each new pattern I will write note with one example. May be it will be interesting for junior developers.
Thank you for attention. If you have any questions, you can send me email to m@kharev.net
Best Regards,
Mikhail Kharev
SOLID principles
- 1. Single-responsibility principleLet’s talk about single-responsibility principle. We’ll create Employee class, that has name, duty, ID. Also we will add methods for getting employee list and employee with some ID. We received violation of single-responsobility principle. Employee class shouldn’t have to return employee list. How we can change this situation? Employee class should have all this properties. […]
- 2. Open–closed principleOpen-closed principle is about program entities should be opened for extension, but closed for changing. We should create extension points where we will insert abstraction classes and interfaces. You will have possibility add new classes without changing current classes. Also you can delete old irrelevant classes entirely. Let’s speak about this principle at example. We […]
- 3. Liskov substitution principleWhat’s this principle about? Descendant classes shouldn’t conflict with basic classes. Let’s think about this principle at example. We have basic class Document, that has pdf export function and changing text color function. But sometimes, we use document only for reading. You can export such document to pdf, but you can’t change text color. Using […]
- 4. Interface segregation principleInterface segregation principle is easily enough to understand. Let’s create classes for two devices. New multifunctional devices, that can scan to folder, send files to email or telegram. And old devices, that can scan pictures to folders. Both classes realize IMfd interface, but StandardMfd not realized two methods. There is violation of interface segregation principle. […]
- 5. Dependency inversion principleClasses should depend on abstractions, not on concrete details. Let’s see example. We have report forms class, that can connect to database and do some queries. If you don’t know patterns, you can write something like that: This code has report class and mysql class. If we will need connect to other database (for example […]
Creational patterns
- Builder patternSome object types require a lot of actions to create them. Constructor with big count of parameters — not so good idea. Let’s see builder pattern on example. We have report forms class with several parameters — start report date, finish report date, organization name, checkbox about comparing data with last year. You can’t change […]
- Factory method patternLet’s get started from example. We have class Document and we want to initialize object of this class using two cases. At first we can say, that our new document will have concrete number. In second case, we want to create document with some content, but without number (number will be created automatically). C# can’t […]
- Prototype patternAs usual, we speak about pattern using example. I don’t want to copy examples from books, because it may be some difficult for first reading and has extra data. I want to understand pattern using simple example and then tell about this piece of knowledge in my blog. Let’s start. We have Document class, that […]
- Singletone patternA lot of people think that this is bad design pattern, because it has some disadvantages. But sometimes, in rare cases using of Singletone pattern is useful. Let’s talk about this pattern on example. We have api connector that should be initiated only one time. In other cases we have to return instance of connector. […]
Structural patterns
- Proxy patternProxy pattern usually using for control access to other object with intercepting all calls. Let’s see example. We have printer class, that has Print() method. And we have Document class, that has attribute, that allow or disallow print this document. But current realization of Printer class can’t filter these document types and we shouldn’t change […]
- Flyweight patternThis pattern is using for memory optimization. For example, we have business application, where are registered 100 000 users, but some of them have same first names or surnames. In this case we should create unique values of variables and create link for them. Therefore we can optimize memory using. Let’s see example. We created […]
- Facade patternLet’s talk about Facade pattern on example. We search house and basically we want to have building with facade, that hide a lot of things. For example, brick wall, water supply systems, power supply systems, security systems etc. But customer, may don’t know about inner structure of house, he want to know that light works […]
- Decorator patternDecorator pattern is using when you want to extend existing class behavior without it’s changing and without inheritance. Let’s pretend that we have an object and we use to extend its capabilities. And we can’t change current class code or we don’t have access to source code of this class. For example, I want to […]
- Composite patternLet’s imagine that you have graph editor where you can paint different figures. Also you can move all figures, change their colors. And you shouldn’t know for these actions, what kind of object you use. It’s may be one circle or several figures, that you selected with mouse. Composite pattern can organize this object set. […]
Behavioral patterns
- Chain of Responsibility patternExample. We have Client with some documents, that can send these document to addresses using different methods. And send methods depends on document type. For example, secret document we can send using only classic post system. But other document can be delivered with all method simultaneously. How we can realize these concept? We can use […]