Skip to content

Commit

Permalink
add singleton pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
joseederangojr committed Oct 15, 2023
1 parent 8fe3f84 commit 60999c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type CloudProvider = 'google' | 'azure' | 'aws'
export interface VirtualMachine {}
export interface Storage {}
export interface Network {}
export interface Database {}

export class AzureVirtualMachine implements VirtualMachine {}
export class GoogleVirtualMachine implements VirtualMachine {}
Expand All @@ -15,6 +16,10 @@ export class AzureNetwork implements Network {}
export class GoogleNetwork implements Network {}
export class AWSNetwork implements Network {}

export class AzureDatabase implements Database {}
export class GoogleDatabase implements Database {}
export class AWSDatabase implements Database {}

export class CloudProviderNotSupportException extends Error {}
export class VirtualMachineNotSupportedException extends Error {}
export class NetworkNotSupportedException extends Error {}
Expand Down
10 changes: 10 additions & 0 deletions src/singleton.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, it, expect } from "bun:test";
import { AzureDatabase } from "~/singleton";

describe("AzureDatabase", () => {
it("should create only one instance", () => {
const instance1 = AzureDatabase.create();
const instance2 = AzureDatabase.create();
expect(instance1).toBe(instance2);
});
});
16 changes: 16 additions & 0 deletions src/singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Database } from "~/main"


export class AzureDatabase {
private static instance: Database

private constructor() {}

static create(): Database {
if (!AzureDatabase.instance) {
AzureDatabase.instance = new AzureDatabase()
}

return AzureDatabase.instance
}
}

0 comments on commit 60999c7

Please sign in to comment.