Programmers try to avoid switch statements because switches are breaking the rule of the Open-Closed-Principle. Also, continuously extending the switch statement with more clauses leads to a high maintenance effort, as you need to modify the written code every time you extend it. Maybe your working unit-tests will fail after the change. You might need to fix your business logic and your tests every time — what a nightmare…
“The Problem with such duplicate switches is that, whenever you add a clause, you have to find all the switches and update them.” (Martin Fowler, [Refactoring])
We use them because switch…
Based on my story Inheritance & Object Composition I want to show you, how you can use some Angular features for reusing code in really nice ways. I prepared some easy examples below.
Inheritance is a very simple principle in Angular. You can combine a base class with a superclass by using the extends term. The superclass inherits fields and methods from the base class.
Subscribing and unsubscribing to an observable in a Redux store is a common repeatable practise. You can DRY, when you separate this logic in a base class.
>>> base.component.tsexport class BaseComponent implements OnInit, OnDestroy…
The Abstract Factory is is the most powerful tool among Creational Patterns. The variation of the creation of the products in the individual factories can be determined by Factory Method, Prototype, and Builder. The factory itself could be a Singleton.
An Abstract Factory provides an interface for creating a related family of objects, so the concrete classes are hidden and isolated behind it — it acts like a Facade. Objects belonging to an object family are instantiated together via a factory.
The Prototype pattern is also known as Cloning. It creates new objects by copying a prototypical instance. The new object differs only in few things from their predecessors, so it is unnecessary to declare concrete classes. This leads to a class mess.
Product addition and removal at runtime enables more flexibility to add new variants in the form of classes, minimizes maintenance effort, and reduces the need for factories.
The Builder Pattern is often used in situations where you need a wanted order of implementation calls or rather where you want to create complex objects according to a construction plan. Furthermore, the pattern separates the handling of generation and presentation mechanisms.
A big advantage is the monitoring of the creational process (controlled by the Director). It is retrieval only when an object has been created.
A Singleton is a popular and often used pattern in modern frameworks. It is a single resource shared by different users, modules, or subsystems in an application or a global configuration. Shortly explained: A Singleton is an existence of a single class instance that can be accessed globally.
Besides the controlled access to a single instance or improved operations and representation, a Singleton is hard to test because you can not create a fake object. However, you could create a subclass and extend it with a static setter for the instance.
The Factory Method is one of the most known Design Patterns and often used when creating things with same behavior, but with different specifications. This is made possible by delegation of the instantiation process to the specified subclass. This includes specialization options for subclasses, as this pattern provides an extended version of the object compared to the direct creation of the object.
If you want to use a Creational Pattern, but are unsure which one, you can start by implementing the Factory Method. …
In my opinion, any form of code pattern is an important template a developer can orientate towards when searching for a general and reusable solution for a commonly occurring problem. It is also essential to have a default language for communicating software issues and solutions when you talk with other developers. It saves a lot of time if you only call the pattern by its name for your proposed solution instead of describing the whole problem and the fix for it.
Patterns are also good alternatives for sharing information flows, software architecture design (e.g. …
Protecting your Angular code with tests has many advantages:
Reusing code and reducing the maintenance effort of code are more and more popular principles of modern programming because the code base of software applications increases very quickly within a short time. Feature pressure is one of many plagues of a software engineer these days.
Besides Object Composition, Inheritance is one of many principles for reusing code. The design pattern Template Method uses this to define the structure of an algorithm in a superclass but allows its subclasses to override specific steps of the algorithm without changing the structure.
In Angular the method for reading URL parameters by using the…
Full Stack Software Engineer | Trained Mathematical-Technical Software Developer | Writer at CP Massive Creative