Some design pattern to practice

Overview

Design Pattern by HeadFirst Design Pattern Book.

1. Strategy Pattern (Policy Pattern)

diagram

Design Principle:

  • Identify the aspects of your application that vary and seperate them from what stays the same.

    when you have an codes to change, investigate new requirements carefully then you should know what behavior need to separates from code block and what need stays the same.

Design Principle:

  • Program to an interfacce, not an implementation ( 1 of 5 principals in SOLID )

2. Observer Pattern

diagram

Design Principle:

  • The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

3. Decorator Pattern

Design Principle:

  • Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

4.1 Factory Method (a.k.a Virtual Constructor)

  • This part is an example for Factory Method. Part 2 belongs to Aabstract factory is here.
This belongs to creational design pattern that provides an interface for creating objects in a supperclass, but allows subclasses to alter/decide type of object that will be created.

Design Principle:

  • Depend upon abstractions. Do not depend upon concrete classes.
  • Factory Pattern encapsulate object creation

Real life example:

  • Imagine you're using Mysql database connections in your backend system.
  • One day customer want to switch to use MYSQL Server for some purposes.
  • Then we need create new class for MYSQL Server with existing interface using by Mysql before.
  • And edit you backend code to use new connection.
  • In future, customer wants to change to other DB connector then you needs to do the same thing above...
  • Imagine you have 300 lines of code using this connector, then you need edit all of them, it's terrible.

Why Factory Pattern helps here:

  • Decoupling the creation creation of object/interface from your code.
  • When you need to edit, add or remove some connector, just need to edit 1 method without affect other classes.

Cons:

  • Loose Coupling
  • Reusability
  • Easier to maintain

Design Pattern by HeadFirst Design Pattern Book.

4.2 Abstract Factory

Provides an interface for creating families of related or dependent object without specifying their concrete class

abstract factory

An example for PizzaStore:

abstract factory

Pizza_and_Stores_Diagrams

Ingredient_Factory

Result:

result

The factory pattern construct a single object != The abstract factory construct multiple objects

As you can see in 2 pictures above, the Abstract Factory actually looks like a Factory Method. Each method is declared abstract and subclasses override it to create some objects.

The job of an Abstract Factory is to define an interface for creating a set of products. Each method in that interface is responsible for creating a concrete product, and we implement a subclass of Abstract Factory to supply those implementations. So factory methods are a natural way to implement your product methods in your factories.

Absctract factory contains multiple factory methods inside
You might also like...
OpenEMR is a Free and Open Source electronic health records and medical practice management application

OpenEMR is a Free and Open Source electronic health records and medical practice management application. It features fully integrated electronic health records, practice management, scheduling, electronic billing, internationalization, free support, a vibrant community, and a whole lot more. It runs on Windows, Linux, Mac OS X, and many other platforms.

 Just HTTP Status Codes is a great way to empower your project with clean practice 💫
Just HTTP Status Codes is a great way to empower your project with clean practice 💫

The Simplest & Cleanest HTTP Status Codes for PHP. All PHP HTTP Status Codes are stored into beautiful constant names 🎨 Ideal when you develop an API that involves various HTTP codes 🚀

Here are few exercises to practice how to implement API Security with NGINX App-Protect WAF.

api-security-lab This repo contains files for customers and partners to practice an API Security with NGINX App-Protect WAF. To demonstrate the capabi

Demo and practice application with Laravel 8 and InertiaJS. (From laracasts course)
Demo and practice application with Laravel 8 and InertiaJS. (From laracasts course)

InertiaJS playground ⚽️ Started with the Laracasts: Build Modern Laravel Apps Using Inertia.js course and decided to share all my code here, I'll be a

A simple crud (Create-Read-Update-Delete). A little practice with Laravel v6*

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Practice-php - Repositório para praticar a sintaxe de php.

Configuração Inicial para desenvolver em PHP Instalando o PHP no Linux (Ubuntu) sudo apt install php libapache2-mod-php sudo apt-get update Utilizand

This website built for practice using HTML,CSS , Js and PHP - MySQL for backend
This website built for practice using HTML,CSS , Js and PHP - MySQL for backend

SammysBarbeque This website built for practice using HTML,CSS , Js and PHP - MySQL for backend Features added in the website: User:- Authentic and att

A simple but scalable FFA Practice Core featuring one Game Mode & Vasar PvP aspects.
A simple but scalable FFA Practice Core featuring one Game Mode & Vasar PvP aspects.

A simple but scalable FFA Practice Core featuring one Game Mode & Vasar PvP aspects. An example of this Plugin can be found in-game at ganja.bet:19132!

Powerful implementation of the Specification pattern in PHP

RulerZ The central idea of Specification is to separate the statement of how to match a candidate, from the candidate object that it is matched agains

Baum is an implementation of the Nested Set pattern for Laravel's Eloquent ORM.

Baum Baum is an implementation of the Nested Set pattern for Laravel 5's Eloquent ORM. For Laravel 4.2.x compatibility, check the 1.0.x branch branch

[READ-ONLY] A flexible, lightweight and powerful Object-Relational Mapper for PHP, implemented using the DataMapper pattern. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP ORM The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a datamapper pattern the ORM allows you to m

PHP implementation of Fowler's Money pattern.
PHP implementation of Fowler's Money pattern.

Money PHP library to make working with money safer, easier, and fun! "If I had a dime for every time I've seen someone use FLOAT to store currency, I'

PHP implementation of circuit breaker pattern.

What is php-circuit-breaker A component helping you gracefully handle outages and timeouts of external services (usually remote, 3rd party services).

Simplified Repository pattern implementation in Laravel

Laravository - Repository Pattern for Laravel Simplified Repository pattern implementation in Laravel. Requirement Laravel 8.x Installation Execute th

PHP regexp pattern matching Unicode emojis

Emoji pattern This package provides regexp patterns to match Unicode emojis. All forms of emojis are matched, including: Single-character emoji ( 👍 )

A package to implement repository pattern for laravel models

Laravel Model UUID A simple package to use Repository Pattern approach for laravel models . Repository pattern Repositories are classes or components

Simple repository pattern for laravel, with services!

With repository and service you can separate business logic and query logic, slim controller and DRY. Simple generate repository and service with artisan command, automatically bind interface with repository

Repository Pattern implementation for Laravel

This is a Simple Repository Pattern implementation for Laravel Projects and an easily way to build Eloquent queries from API requests.

Releases(v2.0)
Owner
null
Need some filters? This package is based on the Repository Design Pattern to let you create specific queries easily.

DevMakerLab/Laravel-Filters Need some filters? This package is based on the Repository Design Pattern to let you create specific queries easily. Insta

DevMakerLab 19 Feb 20, 2022
This package provides some basic methods to implement a self updating functionality for your Laravel application. Already bundled are some methods to provide a self-update mechanism via Github or some private repository via http.

This package provides some basic methods to implement a self updating functionality for your Laravel 5 application. Already bundled are some methods to provide a self-update mechanism via Github.

Holger Lösken 311 Dec 31, 2022
Some exercises to practice whiteboard interview questions in PHP.

PHP Interview Exercises · A number of exercises to practice whiteboard interview questions in PHP. Inside exercises directory, you can find folders co

Anton Ždanov 421 Jan 9, 2023
Adjacency List’ed Closure Table database design pattern implementation for the Laravel framework.

ClosureTable This is a database manipulation package for the Laravel 5.4+ framework. You may want to use it when you need to store and operate hierarc

Yan Ivanov 441 Dec 11, 2022
Someline Starter is a PHP framework for quick building Web Apps and Restful APIs, with modern PHP design pattern foundation.

Someline Starter PHP Framework Tested and used in production by Someline Inc. Someline Starter is a PHP framework for quick building Web Apps and Rest

Someline 844 Nov 17, 2022
Laravel Design Pattern Generator (api generator)

Laravel Design Pattern Generator (api generator) you can create your restful api easily by using this library and you can filter, sort and include elo

HusseinAlaa 2 Sep 25, 2022
Design Pattern Examples in PHP

Design Patterns in PHP This repository is part of the Refactoring.Guru project. It contains PHP examples for all classic GoF design patterns. Each pat

Refactoring.Guru 909 Dec 22, 2022
Examples of some common design patterns implemented in php

What is a Design Pattern? Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you can c

Bakhtiyor Bahritidinov 4 Feb 11, 2022
FFUF Me - Target Practice For FFUF

FFUF Me - Target Practice For FFUF Intro This is a simple website to get you used to using ffuf against a live target You can either run this repo loc

null 39 Dec 23, 2022
Learn Cookies and Tokens Security in Practice.

The full article is posted on my blog. The video presentation is shared here. The presentation slides are shared here. The exploit codes are shared he

HolyBugx 38 Aug 28, 2022