Saturday, May 08, 2021

Proxy pattern php

Create interface Subject add code:
public function request(): void;
create class RealSubject implements Subject implement missing method repace TODO with
echo "RealSubject: Handling request.<br/>";
create Proxy and have it implements Subject and add code to request function: The most common applications of the Proxy pattern are lazy loading, caching, controlling the access, logging, etc. A Proxy can perform one of these things and then, depending on the result, pass it to the execution to the same method in a linked RealSubject object.
if ($this->checkAccess()) {
    $this->realSubject->request();
    $this->logAccess();
}
The Proxy maintains a reference to the object of the RealSubject class. It can be either lazy-loaded or passed to the Proxy by the client. We then add code
private $realSubject;

public function __construct(RealSubject $realSubject)
{
    $this->realSubject = $realSubject;
}
at the bottom we add:
private function checkAccess(): bool
{
    // Some real checks should go here.
    echo "Proxy: Checking access prior to firing a real request.<br/>";
     return true;
}

private function logAccess(): void
{
    echo "Proxy: Logging the time of request.<br/>";
}
last step is to go to index and add the following includes
include_once ('Subject.php');
include_once ('RealSubject.php');
include_once ('Proxy.php');
we also add the function
function clientCode(Subject $subject)
{
    // ...
    $subject->request();
    // ...
}
and to run our demo we add:
echo "Client: Executing the client code with a real subject:<br/>";
$realSubject = new RealSubject;
clientCode($realSubject);

echo "<br/>";

echo "Client: Executing the same client code with a proxy:<br/>";
$proxy = new Proxy($realSubject);
clientCode($proxy);
**RUN**
Client: Executing the client code with a real subject:
RealSubject: Handling request.

Client: Executing the same client code with a proxy:
Proxy: Checking access prior to firing a real request.
RealSubject: Handling request.
Proxy: Logging the time of request.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Friday, May 07, 2021

Proxy pattern c#

Proxy is a structural design pattern that lets you provide a substitute/placeholder for another object. The proxy controls access to the original object, allowing you to perform work before or after the request to the original object. First create the interface Subject. The Subject interface declares common operations for both the RealSubject and the Proxy. As long as the client works with RealSubject using this interface, you'll be able to pass it to a proxy instead of the real subject.
public interface Subject
{
   void Request();
}
Create class RealSubject and remove the public that was added with the code generation. Extend with : Subject to Request the code is:
class RealSubject : Subject
{
    public void Request()
    {
       Console.WriteLine("Handling Request.");
    }
}
Create a class called Proxy and extend it with : Subject remove the public ans implment missing members.
class Proxy : Subject
 {
    private RealSubject _realSubject;
        
    public Proxy(RealSubject realSubject)
    {
       this._realSubject = realSubject;
    }
        
    public void Request()
    {
        if (this.CheckAccess())
        {
            this._realSubject.Request();
            this.LogAccess();
        }
    }
        
    public bool CheckAccess()
    {
        // Some real checks should go here.
        Console.WriteLine("Checking access prior to firing a real request.");
        return true;
    }
        
    public void LogAccess()
    {
        Console.WriteLine("Proxy: Logging the time of request.");
    }
}
Create a class called Proxy and extend it with : Subject The client code is supposed to work with all objects (both subjects and proxies) via the Subject interface in order to support both real subjects and proxies. In real life, however, clients mostly work with their real subjects directly. In this case, to implement the pattern more easily, you can extend your proxy from the real subject's class. We creat the class Client and keep public
public class Client
    {
    public void ClientCode(Subject subject)
    {
        subject.Request();
    }
}
We now goto the Program file and we add the static void Main. The code will be
static void Main(string[] args)
{
    Client client = new Client();
            
    Console.WriteLine("Executing the client code with a real subject:");
    RealSubject realSubject = new RealSubject();
    client.ClientCode(realSubject);

    Console.WriteLine();

    Console.WriteLine("Executing the same client code with a proxy:");
    Proxy proxy = new Proxy(realSubject);
    client.ClientCode(proxy);
}
When we compile and run we get:
Executing the client code with a real subject:
Handling Request.

Executing the same client code with a proxy:
Checking access prior to firing a real request.
Handling Request.
Proxy: Logging the time of request.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Thursday, May 06, 2021

Proxy pattern java

Key Points About Proxy Design Pattern
  • The Proxy pattern helps us to create an intermediary that acts as an interface to another resource and also hiding the underlying complexity of the application.
  • Proxy means ‘in place of’, representing’ or ‘on behalf of’ are dictionary meanings of proxy and that directly explains Proxy Design Pattern.
  • Consider a heavy Java object (like a JDBC connection or a Hibernate SessionFactory) that requires some initial configuration.
  • If we want such objects to be initialized on demand and once they are, we would like to reuse them for all further calls.

When to Use Proxy Design Pattern:
  • When we want to reduce the complexity of a complex or heavy object.
  • In this case, we may represent it with a skeleton object which loads the original object on demand, also called as lazy initialization. This is known as the Virtual Proxy
  • When the original object copy is present in different address spaces and we want to represent it locally.
  • We can create a proxy which does all the necessary boilerplate stuff like creating and maintaining the connection, encoding, decoding, etc., while the client accesses it as it was present in their local address space. This is called the Remote Proxy
  • When we want to add a layer of security to the original underlying object to provide controlled access based on access rights of the client. This is called Protection Proxy
To the proxy package let's add the interface VeryExpensiveProcess.
public interface VeryExpensiveProcess {
    public abstract void process();
}
We add the package impl we add the class VeryExpensiveProcessProxy. This class is responsible to utilize the Proxy design pattern and initialize our object on demand. Now we will utilize the Proxy pattern and initialize our Expansive object on demand.
import TheRayCode.proxy.*;

public class VeryExpensiveProcessProxy implements VeryExpensiveProcess {

    private static VeryExpensiveProcess veryExpensiveObject;

    @Override
    public void process() {
        if (veryExpensiveObject == null) {
            veryExpensiveObject = new VeryExpensiveProcessImpl();
        }
        veryExpensiveObject.process();
    }
}
Also to the package impl we add another class that implements VeryExpensiveProcess.
import TheRayCode.proxy.*;

public class VeryExpensiveProcessImpl implements VeryExpensiveProcess {
    public VeryExpensiveProcessImpl() {
        heavyInitialConfigurationSetup();
    }
    @Override
    public void process() {
        System.out.println("Processing is done");
    }
    private void heavyInitialConfigurationSetup() {
        System.out.println("Setting up initial configuration");
    }
}

Last we add a main method to are Demo class.
import TheRayCode.impl.*;
import TheRayCode.proxy.*;

public class Demo {
    public static void main(String[] args) {

        VeryExpensiveProcess veryExpensiveProcess = new VeryExpensiveProcessProxy();
        veryExpensiveProcess.process();
        veryExpensiveProcess.process();
    }
}
When we compile and run we should get:
Setting up initial configuration
Processing is done
Processing is done

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Wednesday, May 05, 2021

Proxy pattern c++

The Proxy pattern is a structural design pattern that provides an object that acts as a substitute for a real service object used by the client. The Proxy receives client requests, does some work and then passes the request to a service object.

The Subject is an interface that declares the common operations for both RealSubject and the Proxy. As long as the client works with the RealSubject using this interface, you'll be able to pass it a proxy instead of a real subject.

The code for the Subject interface will be:
class Subject {
public:
    virtual void Request() const = 0;
};

Now let's turn our attention to the RealSubject.

The RealSubject contains some core business logic. Usually, the RealSubject(s) are capable of doing some useful work.
class RealSubject : public Subject {
 public:
  void Request() const override {
    std::cout << "The RealSubject is Handling the request.\n";
  }
};
A Proxy can solve these issues without any changes in the RealSubject's code because the Proxy has an interface identical to the RealSubject. The Proxy maintains a reference to an object of the RealSubject class. It can be either lazy-loaded or passed to the Proxy by the client.

The most common applications of the Proxy pattern are lazy loading, caching, controlling the access, logging, etc. A Proxy can perform one of these things and then, depending on the result, pass the execution to the same method in a linked RealSubject object.
#include "RealSubject.h"

class Proxy : public Subject {

private:
    RealSubject *real_subject_;

    bool CheckAccess() const {
        // Some real checks should go here.
        std::cout << "Checking access prior to firing a real request.\n";
        return true;
    }
    void LogAccess() const {
        std::cout << "Logging the time of request.\n";
    }

public:
    Proxy(RealSubject *real_subject) : real_subject_(new RealSubject(*real_subject)) {
    }

    ~Proxy() {
        delete real_subject_;
    }
    void Request() const override {
        if (this->CheckAccess()) {
            this->real_subject_->Request();
            this->LogAccess();
        }
    }
};
Now let's build an example in the main.cpp We need to incude the Proxy.h
#include "Proxy.h"

void ClientCode(const Subject &subject) {
    subject.Request();
}

And when we create the main method we declare a new RealSubject and do some work.
int main() {
    std::cout << "Executing the client code with a real subject:\n";
    RealSubject *real_subject = new RealSubject;
    ClientCode(*real_subject);
    std::cout << "\n";
    std::cout << "Executing the same client code with a proxy:\n";
    Proxy *proxy = new Proxy(real_subject);
    ClientCode(*proxy);

    delete real_subject;
    delete proxy;
    return 0;
}
We now compile and run. We should get:
Executing the client code with a real subject:
The RealSubject is Handling the request.


Executing the same client code with a proxy: Checking access prior to firing a real request. The RealSubject is Handling the request. Logging the time of request.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Tuesday, May 04, 2021

Flyweight pattern java

Let's create a package called trees. Inside this package we place the class called TreeFactory.
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class TreeFactory {
    static Map<String, TreeType> treeTypes = new <HashMap>();

    public static TreeType getTreeType(String name, Color color, String otherTreeData) {
        TreeType result = treeTypes.get(name);
        if (result == null) {
            result = new TreeType(name, color, otherTreeData);
            treeTypes.put(name, result);
        }
        return result;
    }
}
To the same package we add the class TreeType.
import java.awt.*;

public class TreeType {
    private String name;
    private Color color;
    private String otherTreeData;

    public TreeType(String name, Color color, String otherTreeData) {
        this.name = name;
        this.color = color;
        this.otherTreeData = otherTreeData;
    }

    public void draw(Graphics g, int x, int y) {
        g.setColor(Color.BLACK);
        g.fillRect(x - 1, y, 3, 5);
        g.setColor(color);
        g.fillOval(x - 5, y - 10, 10, 10);
    }
}
Let's also add a class called Tree.
import java.awt.*;

public class Tree {
    private int x;
    private int y;
    private TreeType type;

    public Tree(int x, int y, TreeType type) {
        this.x = x;
        this.y = y;
        this.type = type;
    }

    public void draw(Graphics g) {
        type.draw(g, x, y);
    }
}
Now let's create a package called forest and inside that package we place a class also called Forest. The Forest class is extended with the JFrame utility.
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Forest extends JFrame {
    private List<Tree> trees = new ArrayList<>();

    public void plantTree(int x, int y, String name, Color color, String otherTreeData) {
        TreeType type = TreeFactory.getTreeType(name, color, otherTreeData);
        Tree tree = new Tree(x, y, type);
        trees.add(tree);
    }

    @Override
    public void paint(Graphics graphics) {
        for (Tree tree : trees) {
            tree.draw(graphics);
        }
    }
}
Now let's put this altogether in the main method we place in the Demo file.
import java.awt.*;

public class Demo {
    static int CANVAS_SIZE = 500;
    static int TREES_TO_DRAW = 1000000;
    static int TREE_TYPES = 2;

    public static void main(String[] args) {
        Forest forest = new Forest();
        for (int i = 0; i < Math.floor(TREES_TO_DRAW / TREE_TYPES); i++) {
            forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE),
                    "Summer Oak", Color.GREEN, "Oak texture stub");
            forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE),
                    "Autumn Oak", Color.ORANGE, "Autumn Oak texture stub");
        }
        forest.setSize(CANVAS_SIZE, CANVAS_SIZE);
        forest.setVisible(true);

        System.out.println(TREES_TO_DRAW + " trees drawn");
        System.out.println("---------------------");
        System.out.println("Memory usage:");
        System.out.println("Tree size (8 bytes) * " + TREES_TO_DRAW);
        System.out.println("+ TreeTypes size (~30 bytes) * " + TREE_TYPES + "");
        System.out.println("---------------------");
        System.out.println("Total: " + ((TREES_TO_DRAW * 8 + TREE_TYPES * 30) / 1024 / 1024) +
                "MB (instead of " + ((TREES_TO_DRAW * 38) / 1024 / 1024) + "MB)");
    }

    private static int random(int min, int max) {
        return min + (int) (Math.random() * ((max - min) + 1));
    }
}
Let's compile and run this. We should get a neat graphic.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Monday, May 03, 2021

Flyweight pattern C#

The Flyweight pattern has a single purpose: minimizing memory intake. If your program doesn’t struggle with a shortage of RAM, then you might just ignore this pattern for a while. Let's create a Car.cs class.
public class Car
{
    public string Owner { get; set; }
    public string Number { get; set; }
    public string Company { get; set; }
    public string Model { get; set; }
    public string Color { get; set; }
 }
The Flyweight Factory creates and manages the Flyweight objects. It ensures that flyweights are shared correctly. When the client requests a flyweight, the factory either returns an existing instance or creates a new one, if it doesn't exist yet. getKey(Car key) returns a Flyweight's string hash for a given state. GetFlyweight(Car sharedState) Returns an existing Flyweight with a given state or creates a new one.
public class FlyweightFactory
{
    private List<Tuple<Flyweight, string>> flyweights = new List<Tuple<Flyweight, string>>();
    public FlyweightFactory(params Car[] args)
    {
        foreach (var elem in args)
        {
            flyweights.Add(new Tuple<Flyweight, string>(new Flyweight(elem), this.getKey(elem)));
        }
    }

    public string getKey(Car key)
    {
        List<string> elements = new List<string>();
        elements.Add(key.Model);
        elements.Add(key.Color);
        elements.Add(key.Company);
        if (key.Owner != null && key.Number != null)
        {
            elements.Add(key.Number);
            elements.Add(key.Owner);
        }
        elements.Sort();
        return string.Join("_", elements);
   }
   public Flyweight GetFlyweight(Car sharedState)
   {
       string key = this.getKey(sharedState);

       if (flyweights.Where(t => t.Item2 == key).Count() == 0)
       {
           Console.WriteLine("FlyweightFactory: Can't find a flyweight, creating new one.");
           this.flyweights.Add(new Tuple<Flyweight, string>(new Flyweight(sharedState), key));
       }
       else
       {
           Console.WriteLine("FlyweightFactory: Reusing existing flyweight.");
       }
       return this.flyweights.Where(t => t.Item2 == key).FirstOrDefault().Item1;
   }
   public void listFlyweights()
   {
       var count = flyweights.Count;
       Console.WriteLine($"\nFlyweightFactory: I have {count} flyweights:");
       foreach (var flyweight in flyweights)
       {
           Console.WriteLine(flyweight.Item2);
       }
   }
}
The Flyweight class stores a common portion of the state (also called intrinsic state) that belongs to multiple real business entities. The Flyweight accepts the rest of the state (extrinsic state, unique for each entity) via its method parameters.
using System;
using Newtonsoft.Json;

public class Flyweight
{
    private Car _sharedState;
    public Flyweight(Car car)
    {
        this._sharedState = car;
    }
    public void Operation(Car uniqueState)
    {
        string s = JsonConvert.SerializeObject(this._sharedState);
        string u = JsonConvert.SerializeObject(uniqueState);
        Console.WriteLine($"Flyweight: Displaying shared {s} and unique {u} state.");
    }
}
Now let's put this altogether in the Program class. First we add a client function **addCarToPoliceDatabase** The client code either stores or calculates extrinsic state and passes it to the flyweight's methods.
public static void addCarToPoliceDatabase(FlyweightFactory factory, Car car)
{
    Console.WriteLine("\nClient: Adding a car to database.");
    var flyweight = factory.GetFlyweight(new Car
    {
        Color = car.Color,
        Model = car.Model,
        Company = car.Company
    });
    flyweight.Operation(car);
}
Now let's create the Main method The client code usually creates a bunch of pre-populated flyweights in the initialization stage of the application.
static void Main(string[] args)
{
    var factory = new FlyweightFactory(
        new Car { Company = "Chevrolet", Model = "Camaro2018", Color = "pink" },
        new Car { Company = "Mercedes Benz", Model = "C300", Color = "black" },
        new Car { Company = "Mercedes Benz", Model = "C500", Color = "red" },
        new Car { Company = "BMW", Model = "M5", Color = "red" },
        new Car { Company = "BMW", Model = "X6", Color = "white" }
    );
    factory.listFlyweights();

    addCarToPoliceDatabase(factory, new Car {
         Number = "CL234IR",
        Owner = "James Doe",
        Company = "BMW",
         Model = "M5",
         Color = "red"
    });

     addCarToPoliceDatabase(factory, new Car {
         Number = "CL234IR",
         Owner = "James Doe",
         Company = "BMW",
         Model = "X1",
         Color = "red"
    });
    factory.listFlyweights();
}
Let's compile and run. We should get:
FlyweightFactory: I have 5 flyweights:
Camaro2018_Chevrolet_pink
black_C300_Mercedes Benz
C500_Mercedes Benz_red
BMW_M5_red
BMW_white_X6

Client: Adding a car to database.
FlyweightFactory: Reusing existing flyweight.
Flyweight: Displaying shared {"Owner":null,"Number":null,"Company":"BMW","Model":"M5","Color":"red"} and unique {"Owner":"James Doe","Number":"CL234IR","Company":"BMW","Model":"M5","Color":"red"} state.

Client: Adding a car to database.
FlyweightFactory: Can't find a flyweight, creating new one.
Flyweight: Displaying shared {"Owner":null,"Number":null,"Company":"BMW","Model":"X1","Color":"red"} and unique {"Owner":"James Doe","Number":"CL234IR","Company":"BMW","Model":"X1","Color":"red"} state.

FlyweightFactory: I have 6 flyweights:
Camaro2018_Chevrolet_pink
black_C300_Mercedes Benz
C500_Mercedes Benz_red
BMW_M5_red
BMW_white_X6
BMW_red_X1
The Ray Code is AWESOME!!! [Wikipedia](https://en.wikipedia.org/wiki/Flyweight_pattern) ---------------------------------------------------------------------------------------------------- Find Ray on: [facebook](https://www.facebook.com/TheRayCode/) [youtube](https://www.youtube.com/user/AndradeRay/) [The Ray Code](https://www.RayAndrade.com) [Ray Andrade](https://www.RayAndrade.org)

Saturday, May 01, 2021

Flyweight pattern c++

Flyweight is a structural design pattern that lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object. Let's create a struct called UniqueState.
#include <iostream>

struct UniqueState
{
    std::string owner_;
    std::string plates_;

    UniqueState(const std::string &owner, const std::string &plates)
            : owner_(owner), plates_(plates)
    {
    }

    friend std::ostream &operator<<(std::ostream &os, const UniqueState &us)
    {
        return os << "[ " << us.owner_ << " , " << us.plates_ << " ]";
    }
};
We create another struct we call the SharedState.
#include <iostream>

struct SharedState
{
    std::string brand_;
    std::string model_;
    std::string color_;

    SharedState(const std::string &brand, const std::string &model, const std::string &color)
            : brand_(brand), model_(model), color_(color)
    {
    }

    friend std::ostream &operator<<(std::ostream &os, const SharedState &ss)
    {
        return os << "[ " << ss.brand_ << " , " << ss.model_ << " , " << ss.color_ << " ]";
    }
};
The Flyweight stores a common portion of the state (also called intrinsic state) that belongs to multiple real business entities. The Flyweight accepts the rest of the state (extrinsic state, unique for each entity) via its method parameters. Let's create a Flyweight class.
Flyweight
{
private:
    SharedState *shared_state_;

public:
    Flyweight(const SharedState *shared_state) : shared_state_(new SharedState(*shared_state))
    {
    }
    Flyweight(const Flyweight &other) : shared_state_(new SharedState(*other.shared_state_))
    {
    }
    ~Flyweight()
    {
        delete shared_state_;
    }
    SharedState *shared_state() const
    {
        return shared_state_;
    }
    void Operation(const UniqueState &unique_state) const
    {
        std::cout << "Flyweight: Displaying shared (" << *shared_state_ << ") and unique (" << unique_state << ") state.\n";
    }
};
Let's put this in a class we call FlyweightFactory. The Flyweight Factory creates and manages the Flyweight objects. It ensures that flyweights are shared correctly. When the client requests a flyweight, the factory either returns an existing instance or creates a new one, if it doesn't exist yet. An unordered_map returns a Flyweight's string hash for a given state.
#include "Flyweight.h"
#include <unordered_map>

class FlyweightFactory
{
private:
    std::unordered_map flyweights_;
    std::string GetKey(const SharedState &ss) const
    {
        return ss.brand_ + "_" + ss.model_ + "_" + ss.color_;
    }

public:
    FlyweightFactory(std::initializer_list<SharedState> share_states)
    {
        for (const SharedState &ss : share_states)
        {
            this->flyweights_.insert(std::make_pair(this->GetKey(ss), Flyweight(&ss)));
        }
    }

    Flyweight GetFlyweight(const SharedState &shared_state)
    {
        std::string key = this->GetKey(shared_state);
        if (this->flyweights_.find(key) == this->flyweights_.end())
        {
            std::cout << "FlyweightFactory: Can't find a flyweight, creating new one.\n";
            this->flyweights_.insert(std::make_pair(key, Flyweight(&shared_state)));
        }
        else
        {
            std::cout << "FlyweightFactory: Reusing existing flyweight.\n";
        }
        return this->flyweights_.at(key);
    }
    void ListFlyweights() const
    {
        size_t count = this->flyweights_.size();
        std::cout << "\nFlyweightFactory: I have " << count << " flyweights:\n";
        for (std::pair pair : this->flyweights_)
        {
            std::cout << pair.first << "\n";
        }
    }
};
Let's put this together in the main.cpp. We add a function called AddCarToPoliceDatabase. The client code either stores or calculates extrinsic state and passes it to the flyweight's methods. The client code usually creates a bunch of pre-populated flyweights in the initialization stage of the application.
#include "FlyweightFactory.h"

void AddCarToPoliceDatabase(
        FlyweightFactory &ff, const std::string &plates, const std::string &owner,
        const std::string &brand, const std::string &model, const std::string &color)
{
    std::cout << "\nClient: Adding a car to database.\n";
    const Flyweight &flyweight = ff.GetFlyweight({brand, model, color});
    // The client code either stores or calculates extrinsic state and passes it
    // to the flyweight's methods.
    flyweight.Operation({plates, owner});
}
int main()
{
    FlyweightFactory *factory = new FlyweightFactory({{"Chevrolet", "Camaro2018", "pink"}, 
    				{"Mercedes Benz", "C300", "black"}, {"Mercedes Benz", "C500", "red"}, 
    				{"BMW", "M5", "red"}, {"BMW", "X6", "white"}});
    factory->ListFlyweights();

    AddCarToPoliceDatabase(*factory,
                           "CL234IR",
                           "James Doe",
                           "BMW",
                           "M5",
                           "red");

    AddCarToPoliceDatabase(*factory,
                           "CL234IR",
                           "James Doe",
                           "BMW",
                           "X1",
                           "red");
    factory->ListFlyweights();
    delete factory;

    return 0;
}
When we compile and run we get:
FlyweightFactory: I have 5 flyweights:
BMW_M5_red
Mercedes Benz_C500_red
Mercedes Benz_C300_black
BMW_X6_white
Chevrolet_Camaro2018_pink

Client: Adding a car to database.
FlyweightFactory: Reusing existing flyweight.
Flyweight: Displaying shared ([ BMW , M5 , red ]) and unique ([ CL234IR , James Doe ]) state.

Client: Adding a car to database.
FlyweightFactory: Can't find a flyweight, creating new one.
Flyweight: Displaying shared ([ BMW , X1 , red ]) and unique ([ CL234IR , James Doe ]) state.

FlyweightFactory: I have 6 flyweights:
BMW_M5_red
Mercedes Benz_C500_red
Mercedes Benz_C300_black
BMW_X1_red
BMW_X6_white
Chevrolet_Camaro2018_pink

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Python Singleton Design Pattern:

Python Singleton Design Pattern: What It Is and Why Study It The Singleton Design Pattern is a creational design pattern ...