Skip to content

Commit

Permalink
Merge pull request #2 from mikedegeofroy/lab-3
Browse files Browse the repository at this point in the history
Lab 3
  • Loading branch information
mikedegeofroy authored Mar 15, 2024
2 parents b29dda2 + eda62d8 commit 38f70e9
Show file tree
Hide file tree
Showing 36 changed files with 650 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Itmo.ObjectOrientedProgramming.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.Tests", "tests\Lab4.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab5.Tests", "tests\Lab5.Tests\Lab5.Tests.csproj", "{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab3.Console", "src\Lab3.Console\Lab3.Console.csproj", "{9654EFB5-5367-4DAD-8F18-58E113384AA0}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lab5", "Lab5", "{99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Presentation", "Presentation", "{08E62A91-F7B4-4FE8-A1F8-BFD64F7E3A9E}"
Expand Down Expand Up @@ -72,6 +73,7 @@ Global
{7E5E46DB-EF55-47E6-822A-6AE20BF2FCD9} = {792FFE57-F1A0-47A4-9E2A-80F75FF2F8DB}
{17689E4A-FE4F-4893-8E28-3BA911DEC8D8} = {792FFE57-F1A0-47A4-9E2A-80F75FF2F8DB}
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB} = {792FFE57-F1A0-47A4-9E2A-80F75FF2F8DB}
{9654EFB5-5367-4DAD-8F18-58E113384AA0} = {8ED774F4-D429-4B82-B768-50DCA8EFF609}
{99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809} = {8ED774F4-D429-4B82-B768-50DCA8EFF609}
{08E62A91-F7B4-4FE8-A1F8-BFD64F7E3A9E} = {99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809}
{D6F0AF28-4B74-4CEF-BAE0-631C60979762} = {99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809}
Expand Down Expand Up @@ -122,6 +124,10 @@ Global
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}.Release|Any CPU.Build.0 = Release|Any CPU
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Release|Any CPU.Build.0 = Release|Any CPU
{9BAA271C-E0CC-42C8-83E7-FC398D53874F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BAA271C-E0CC-42C8-83E7-FC398D53874F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BAA271C-E0CC-42C8-83E7-FC398D53874F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
10 changes: 10 additions & 0 deletions src/Lab2.Console/Lab2.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions src/Lab2.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

14 changes: 14 additions & 0 deletions src/Lab3.Console/Lab3.Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Lab3\Lab3.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/Lab3.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Drawing;
using Itmo.ObjectOrientedProgramming.Lab3.Decorators;
using Itmo.ObjectOrientedProgramming.Lab3.Display;
using Itmo.ObjectOrientedProgramming.Lab3.Filter;
using Itmo.ObjectOrientedProgramming.Lab3.Logger;
using Itmo.ObjectOrientedProgramming.Lab3.Message;
using Itmo.ObjectOrientedProgramming.Lab3.User;

var message = new Message("Good morning!", "This is a body.", 2);
var user = new User();
var logger = new Logger();
var filter = new PriorityFilter(3);

var proxy2 = new MessageLogger(user, logger);
var proxy1 = new MessageFilter(proxy2, filter);

proxy1.HandleMessage(message);

var display = new DisplayToFile();

display.SetColor(Color.Red);
display.Out("Hello!");
21 changes: 21 additions & 0 deletions src/Lab3/Decorators/MessageFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Itmo.ObjectOrientedProgramming.Lab3.Filter;
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3.Decorators;

public class MessageFilter : IRecipient
{
private readonly IRecipient _recipient;
private readonly IFilter _filter;

public MessageFilter(IRecipient recipient, IFilter filter)
{
_recipient = recipient;
_filter = filter;
}

public void HandleMessage(IMessage message)
{
if (_filter.Filter(message)) _recipient.HandleMessage(message);
}
}
27 changes: 27 additions & 0 deletions src/Lab3/Decorators/MessageLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Itmo.ObjectOrientedProgramming.Lab3.Logger;
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3.Decorators;

public class MessageLogger : IRecipient
{
private readonly IRecipient _recipient;
private readonly ILogger _logger;

public MessageLogger(IRecipient recipient, ILogger logger)
{
_recipient = recipient;
_logger = logger;
}

public void HandleMessage(IMessage message)
{
Log(message);
_recipient.HandleMessage(message);
}

private void Log(IRenderable message)
{
_logger.Log(message.Render());
}
}
21 changes: 21 additions & 0 deletions src/Lab3/Display/Display.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Drawing;
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3.Display;

// TODO
// should have a display that takes in a string.
public class Display : IRecipient
{
private readonly DisplayDriver _displayStrategy;

public Display(IDisplayStrategy displayStrategy)
{
_displayStrategy = new DisplayDriver(displayStrategy);
}

public void HandleMessage(IMessage message)
{
_displayStrategy.OutputText(message.Render(), Color.Red);
}
}
20 changes: 20 additions & 0 deletions src/Lab3/Display/DisplayDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Drawing;

namespace Itmo.ObjectOrientedProgramming.Lab3.Display;

public class DisplayDriver
{
private readonly IDisplayStrategy _strategy;

public DisplayDriver(IDisplayStrategy strategy)
{
_strategy = strategy;
}

public void OutputText(string text, Color color)
{
_strategy.Clear();
_strategy.SetColor(color);
_strategy.Out(text);
}
}
29 changes: 29 additions & 0 deletions src/Lab3/Display/DisplayToConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Drawing;

namespace Itmo.ObjectOrientedProgramming.Lab3.Display;

public class DisplayToConsole : IDisplayStrategy
{
private Color _color;

public DisplayToConsole()
{
_color = Color.White;
}

public void Clear()
{
Console.Clear();
}

public void SetColor(Color color)
{
_color = color;
}

public void Out(string text)
{
Console.WriteLine(Crayon.Output.Rgb(_color.R, _color.G, _color.B).Text(text));
}
}
35 changes: 35 additions & 0 deletions src/Lab3/Display/DisplayToFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Drawing;
using System.IO;

namespace Itmo.ObjectOrientedProgramming.Lab3.Display;

public class DisplayToFile : IDisplayStrategy
{
private Color _color;

public DisplayToFile()
{
_color = Color.White;
}

public void Clear()
{
Console.Clear();
}

public void SetColor(Color color)
{
_color = color;
}

public void Out(string text)
{
const string path = "test.txt";

if (File.Exists(path)) return;

using StreamWriter sw = File.CreateText(path);
sw.WriteLine(Crayon.Output.Rgb(_color.R, _color.G, _color.B).Text(text));
}
}
10 changes: 10 additions & 0 deletions src/Lab3/Display/IDisplayStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;

namespace Itmo.ObjectOrientedProgramming.Lab3.Display;

public interface IDisplayStrategy
{
void Clear();
void SetColor(Color color);
void Out(string text);
}
8 changes: 8 additions & 0 deletions src/Lab3/Filter/IFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3.Filter;

public interface IFilter
{
bool Filter(IMessage message);
}
23 changes: 23 additions & 0 deletions src/Lab3/Filter/PriorityFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3.Filter;

public class PriorityFilter : IFilter
{
private int _priorityFilter;

public PriorityFilter(int priorityLevel = 0)
{
_priorityFilter = priorityLevel;
}

public void SetFilter(int priorityLevel)
{
_priorityFilter = priorityLevel;
}

public bool Filter(IMessage message)
{
return message.Priority >= _priorityFilter;
}
}
22 changes: 22 additions & 0 deletions src/Lab3/Group/GroupRecipient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3.Group;

public class GroupRecipient : IRecipient
{
private readonly IEnumerable<IRecipient> _recipients;

public GroupRecipient(IEnumerable<IRecipient> recipients)
{
_recipients = recipients;
}

public void HandleMessage(IMessage message)
{
foreach (IRecipient recipient in _recipients)
{
recipient.HandleMessage(message);
}
}
}
8 changes: 8 additions & 0 deletions src/Lab3/IRecipient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Itmo.ObjectOrientedProgramming.Lab3.Message;

namespace Itmo.ObjectOrientedProgramming.Lab3;

public interface IRecipient
{
void HandleMessage(IMessage message);
}
6 changes: 6 additions & 0 deletions src/Lab3/IRenderable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Itmo.ObjectOrientedProgramming.Lab3;

public interface IRenderable
{
string Render();
}
4 changes: 4 additions & 0 deletions src/Lab3/Lab3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<RootNamespace>Itmo.ObjectOrientedProgramming.Lab3</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Crayon" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Lab3/Logger/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Itmo.ObjectOrientedProgramming.Lab3.Logger;

public interface ILogger
{
void Log(string message);
}
11 changes: 11 additions & 0 deletions src/Lab3/Logger/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Itmo.ObjectOrientedProgramming.Lab3.Logger;

public class Logger : ILogger
{
public void Log(string message)
{
Console.WriteLine(message);
}
}
6 changes: 6 additions & 0 deletions src/Lab3/Message/IMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Itmo.ObjectOrientedProgramming.Lab3.Message;

public interface IMessage : IRenderable
{
int Priority { get; }
}
21 changes: 21 additions & 0 deletions src/Lab3/Message/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Itmo.ObjectOrientedProgramming.Lab3.Message;

public class Message : IMessage
{
private readonly string _header;
private readonly string _body;

public Message(string header, string body, int priority)
{
_header = header;
_body = body;
Priority = priority;
}

public int Priority { get; }

public string Render()
{
return _header + '\n' + _body;
}
}
Loading

0 comments on commit 38f70e9

Please sign in to comment.