Items in category refactoring

Dependency Injection via Factory

Refactoring
Dependency Injection via Factory
Image generated by ChatGPT (DALL·E)

You know, when coupling is not loose, components depend too much on each other. It makes your entire architecture fragile and immobile. You can check how loose the coupling is by making a unit test for a component. If you have no problem substituting dependencies by e.g. mock objects then everything is ok. Let take a model class. It depends on DB connection, here Lib_Db_Adapter_Interface instance. We cannot just create DB adapter instance within model constructor, because it depends on configuration data which don’t belong to the model. We can pass to the model constructor a settings array with DB configuration data.

Design by Contract and JS

Refactoring
Design by Contract and JS
Image generated by ChatGPT (DALL·E)

Design by contract (DbC) is an approach in application design, which originally came from Eiffel, but now widely used on different languages (particularly in Java). In real world one party (supplier) makes an offer for an agreement (business contract) and another one (client) accepts. The same way can be described relations between objects in software engineering. As a declared agreement is accepted by the client object, the last one is expected to keep its rules.

Flyweight pattern using Mixins

Refactoring
Flyweight pattern using Mixins

In my previous article among other patters I was telling about Flyweight. That is about the objects designed so to minimize memory use by sharing as much data as possible with other similar objects. You can find plenty of Flyweight implementation examples in Internet, though it will be mostly variations of Multiton, a collection (map) keeping only instance per every identical object. And I decided to follow that unwritten rule as it seems to be the simplest way to show the idea. In some cases it is much more efficient to share not the instance itself, but weighty parts among the instances. Imagine, you have user object containing properties and methods relevant for any user. However registered user has extended set of properties and methods. Moderator or admin contain even more.

Design Patterns by PHP and JavaScript examples

Refactoring
Design Patterns by PHP and JavaScript examples
Image generated by ChatGPT (DALL·E)

After having your project fully tested, deployed and running, it seems the application architecture is pretty good enough. All the requirements met and everybody is happy. But then as it happens, the requirements change and you, all of sudden, find yourself in the time of troubles. It comes out that some modules easier to hack than to modify. Change of other ones brings endless changes in a cascade of dependent modules. Or you change one module and whole the application starts to collapse like a house of cards. And, of course, you find out that you can’t reuse already written modules for the new tasks, because the encapsulation of the desired parts would take too much risk and work. Robert C. Martin was very accurate naming those symptoms of rotting design as Viscosity, Rigidity, Fragility and Immobility

Aspect-oriented Software Development and PHP

Refactoring
Aspect-oriented Software Development and PHP
Image generated by ChatGPT (DALL·E)

Aspect Oriented Software Development (AOSD) is a methodology meant to implement new aspects in software component using external components, but without altering the code that implements the core functionality.

The AOSD concept was applied originally by Java developers. They developed a compiler that implements the AOP (Aspect-Oriented Programming, an implementation of AOSD) white box approach, i.e. the compiler the merges the code that implements the software components core functionality with the necessary code alterations to implement the new aspects where necessary.

This paper provides an introduction into the paradigm of aspect-oriented software development (AOSD). It includes a multitude of practical examples, provided with a view to objectify such abstract approach as AOSD, as well as to help the reader easily grasp its essence and advantages. The paper is primarily intended for programmers working with PHP. Its aim is to demonstrate a way of applying AOSD in PHP-based projects already today.