Factory pattern is a well known creator pattern. Problem with factory pattern is whenever there is a new concrete class that will be instantiate by the creator class (that have factory method), you have to add new ‘case’ in a switch or if / else.
So this violate the Open Close Principle which states open for extensions but close for modifications.
Code below using typescript, but the concept is same for other programming language.
Factory Pattern with Switch or If Else
Factory pattern with switch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
publicstaticcreate(shape:string):Shape{
switch(type){
case'circle':
returnnewCircle();
case'rectangle':
returnnewRectangle();
case'triangle':
returnnewTriangle()
default:
throwError(`Shape isnotsupported`);
}
}
If you want to add Square shape, we need to add another case in the factory.
factory pattern - another case
1
2
case'square':
returnnewSquare()
The concept is the same with If Else
So I need to open my factory class every time there is a new shape to be add in.
Factory Pattern without Switch & If / Else
There are 3 main classes
1) Factory class
– it will import the shape class files in the folder /shapes
– instantiate the object based on shape type
2) Shape abstract class
– abstract method of getType()
3) Shape concrete class
– implement the getType() to tell what type is the shape e.g round
– located in /shapes folder
The concrete class Shape will implement a function to tell it is what kind of Shape.
Factory pattern - class shape
1
2
3
4
5
6
7
8
9
classabstractShape{
publicabstractgetType():string;
}
classRoundextendsShape{
publicgetType():string{
return'round'
}
}
The factory class will import all the shape file path and instantiate the object when call by user class.
factory pattern - dynamic without if else or switch
In my previous post, I was unable to install PHP & MySQL. This time after 2 years, I tried again but still fail, so I decided to use XAMPP to install PHP & MySQL and Apache on MacOS Ventura and it works!
But I had to disable default installed apache2 and change MySQL default port at XAMPP before MySQL and Apache2 can run on XAMPP.
How To Install PHP, MySQL & Apache using XAMPP
1. Download and install XAMPP
Mount the xampp dmg and run it. If you face unknown publish then do Control + Click on the XAMPP installer then run it again.
2. Follow the step by step instruction in installing XAMPP
Just follow the instructions.
3. Run the servers
If you unable to run the MySQL & Apache web server than follow below solution.
If you can run MySQL change it default ports. I use 3316 instead of default 3306.
I tried to stop mysql service but MacOS informed no MySQL service was running. Only by changing MySQL default port it works.
If you can’t run the apache2 server, stop default apache2 server by typing at the terminal.
stop apache2
1
sudo apachectl stop
Then run again Apache Web Server at XAMPP.
4. Test PHP & MySQL by accessing PhpMyAdmin
If you can access phpMyAdmin and create table or user from phpMyAdmin then your XAMPP installation works!
My Steps Installing Nodejs using Node Version Manager (nvm)
1) Firstly list the node version you want to install
nvm list remote
1
nvm ls-remote
2) Secondly, choose the version you want to install. I chose LTS v18 @ lts/hydrogen
node install hydrogen
1
nvm install lts/hydrogen
3) Check if you have installed the right version.
node version
1
node-v
If You Got Checksum Error
At first, I had the checksum error, when I checked on the internet it was due to curl installed using SNAP (which I did). I have to re-install my curl using APT and remove the snap path from the path environment.
Curl still point to snap/bin.
1
curl
1
echo$PATH
Remove the snap/bin from the environment. Edit it using nano
For this demo, you just submit the form by clicking “Submit form”. If you do the the hCaptcha correctly you will get hCaptcha success if not hCaptcha fail.
You can do your programming logic based on the success status.
Check the hCaptcha is ticked or no
There 2 methods to do this
Check the post value of ‘h-captcha-response’
Use data-callback
Method #1: Check the post value of ‘h-captcha-response’
The problem with the form as it is, if a user doesn’t do the hCaptcha test, the user still can submit the form. So we need to add javascript checking on the client side to check whether the user has done the captcha test or no.
If you have done the captcha test, hCaptcha will send a string of response in the $_POST[‘h-captcha-response’]. If not this string will be empty.