Skip to content

Commit

Permalink
Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCancio committed Jan 13, 2021
1 parent 2ed17b1 commit 8c586a4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
25 changes: 25 additions & 0 deletions PatternsCriacao/PatternsCriacao.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Singleton\Singleton.csproj", "{0966E9CC-D967-44E6-B950-20DE58379278}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0966E9CC-D967-44E6-B950-20DE58379278}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0966E9CC-D967-44E6-B950-20DE58379278}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0966E9CC-D967-44E6-B950-20DE58379278}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0966E9CC-D967-44E6-B950-20DE58379278}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BDC89CB3-F133-4341-A21C-A9B5EDB05D3F}
EndGlobalSection
EndGlobal
29 changes: 29 additions & 0 deletions PatternsCriacao/Singleton/BolaComSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace Singleton
{
public sealed class BolaComSingleton
{
private static BolaComSingleton instancia = null;

public static BolaComSingleton GetInstancia
{
get
{
if (instancia == null)
{
instancia = new BolaComSingleton();
Console.WriteLine("Bola em Jogo!");
}

return instancia;

}
}

public void Mensagem(string msg)
{
Console.WriteLine(msg);
}
}
}
18 changes: 18 additions & 0 deletions PatternsCriacao/Singleton/BolaSemSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Singleton
{
public class BolaSemSingleton
{
public BolaSemSingleton()
{
Console.WriteLine("Bola em Jogo!");
}
public void Mensagem(string msg)
{
Console.WriteLine(msg);
}
}
}
38 changes: 38 additions & 0 deletions PatternsCriacao/Singleton/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

namespace Singleton
{
class Program
{
static void Main(string[] args)
{
UsandoSingleton();
Console.WriteLine("\n");
UsandoMetodoTradicional();
}

public static void UsandoSingleton()
{
BolaComSingleton jogador_1 = BolaComSingleton.GetInstancia;
jogador_1.Mensagem("Jogador 1: A bola está comigo no meio de campo");

BolaComSingleton jogador_2 = BolaComSingleton.GetInstancia;
jogador_2.Mensagem("Jogador2: recebeu a bola");

BolaComSingleton jogador_3 = BolaComSingleton.GetInstancia;
jogador_3.Mensagem("Jogador 3: recebeu o lançamento na linha de fundo");
}

public static void UsandoMetodoTradicional()
{
BolaSemSingleton jogador_1 = new BolaSemSingleton();
jogador_1.Mensagem("Jogador 1: A bola está comigo no meio de campo");

BolaSemSingleton jogador_2 = new BolaSemSingleton();
jogador_2.Mensagem("Jogador2: recebeu a bola");

BolaSemSingleton jogador_3 = new BolaSemSingleton();
jogador_3.Mensagem("Jogador 3: recebeu o lançamento na linha de fundo");
}
}
}
8 changes: 8 additions & 0 deletions PatternsCriacao/Singleton/Singleton.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>

0 comments on commit 8c586a4

Please sign in to comment.