Skip to content

Commit 38f70e9

Browse files
Merge pull request #2 from mikedegeofroy/lab-3
Lab 3
2 parents b29dda2 + eda62d8 commit 38f70e9

36 files changed

+650
-0
lines changed

Itmo.ObjectOrientedProgramming.sln

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab4.Tests", "tests\Lab4.Te
2929
EndProject
3030
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab5.Tests", "tests\Lab5.Tests\Lab5.Tests.csproj", "{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}"
3131
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab3.Console", "src\Lab3.Console\Lab3.Console.csproj", "{9654EFB5-5367-4DAD-8F18-58E113384AA0}"
3233
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lab5", "Lab5", "{99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809}"
3334
EndProject
3435
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Presentation", "Presentation", "{08E62A91-F7B4-4FE8-A1F8-BFD64F7E3A9E}"
@@ -72,6 +73,7 @@ Global
7273
{7E5E46DB-EF55-47E6-822A-6AE20BF2FCD9} = {792FFE57-F1A0-47A4-9E2A-80F75FF2F8DB}
7374
{17689E4A-FE4F-4893-8E28-3BA911DEC8D8} = {792FFE57-F1A0-47A4-9E2A-80F75FF2F8DB}
7475
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB} = {792FFE57-F1A0-47A4-9E2A-80F75FF2F8DB}
76+
{9654EFB5-5367-4DAD-8F18-58E113384AA0} = {8ED774F4-D429-4B82-B768-50DCA8EFF609}
7577
{99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809} = {8ED774F4-D429-4B82-B768-50DCA8EFF609}
7678
{08E62A91-F7B4-4FE8-A1F8-BFD64F7E3A9E} = {99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809}
7779
{D6F0AF28-4B74-4CEF-BAE0-631C60979762} = {99CB5FCA-AC39-4AD0-B4FF-A8C051CC2809}
@@ -122,6 +124,10 @@ Global
122124
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
123125
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
124126
{FA627A62-AD7A-4A72-B15C-EC8AEFD206DB}.Release|Any CPU.Build.0 = Release|Any CPU
127+
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
128+
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
129+
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
130+
{9654EFB5-5367-4DAD-8F18-58E113384AA0}.Release|Any CPU.Build.0 = Release|Any CPU
125131
{9BAA271C-E0CC-42C8-83E7-FC398D53874F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
126132
{9BAA271C-E0CC-42C8-83E7-FC398D53874F}.Debug|Any CPU.Build.0 = Debug|Any CPU
127133
{9BAA271C-E0CC-42C8-83E7-FC398D53874F}.Release|Any CPU.ActiveCfg = Release|Any CPU

src/Lab2.Console/Lab2.Console.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

src/Lab2.Console/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


src/Lab3.Console/Lab3.Console.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Lab3\Lab3.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

src/Lab3.Console/Program.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Drawing;
2+
using Itmo.ObjectOrientedProgramming.Lab3.Decorators;
3+
using Itmo.ObjectOrientedProgramming.Lab3.Display;
4+
using Itmo.ObjectOrientedProgramming.Lab3.Filter;
5+
using Itmo.ObjectOrientedProgramming.Lab3.Logger;
6+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
7+
using Itmo.ObjectOrientedProgramming.Lab3.User;
8+
9+
var message = new Message("Good morning!", "This is a body.", 2);
10+
var user = new User();
11+
var logger = new Logger();
12+
var filter = new PriorityFilter(3);
13+
14+
var proxy2 = new MessageLogger(user, logger);
15+
var proxy1 = new MessageFilter(proxy2, filter);
16+
17+
proxy1.HandleMessage(message);
18+
19+
var display = new DisplayToFile();
20+
21+
display.SetColor(Color.Red);
22+
display.Out("Hello!");

src/Lab3/Decorators/MessageFilter.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Itmo.ObjectOrientedProgramming.Lab3.Filter;
2+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab3.Decorators;
5+
6+
public class MessageFilter : IRecipient
7+
{
8+
private readonly IRecipient _recipient;
9+
private readonly IFilter _filter;
10+
11+
public MessageFilter(IRecipient recipient, IFilter filter)
12+
{
13+
_recipient = recipient;
14+
_filter = filter;
15+
}
16+
17+
public void HandleMessage(IMessage message)
18+
{
19+
if (_filter.Filter(message)) _recipient.HandleMessage(message);
20+
}
21+
}

src/Lab3/Decorators/MessageLogger.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Itmo.ObjectOrientedProgramming.Lab3.Logger;
2+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab3.Decorators;
5+
6+
public class MessageLogger : IRecipient
7+
{
8+
private readonly IRecipient _recipient;
9+
private readonly ILogger _logger;
10+
11+
public MessageLogger(IRecipient recipient, ILogger logger)
12+
{
13+
_recipient = recipient;
14+
_logger = logger;
15+
}
16+
17+
public void HandleMessage(IMessage message)
18+
{
19+
Log(message);
20+
_recipient.HandleMessage(message);
21+
}
22+
23+
private void Log(IRenderable message)
24+
{
25+
_logger.Log(message.Render());
26+
}
27+
}

src/Lab3/Display/Display.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Drawing;
2+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab3.Display;
5+
6+
// TODO
7+
// should have a display that takes in a string.
8+
public class Display : IRecipient
9+
{
10+
private readonly DisplayDriver _displayStrategy;
11+
12+
public Display(IDisplayStrategy displayStrategy)
13+
{
14+
_displayStrategy = new DisplayDriver(displayStrategy);
15+
}
16+
17+
public void HandleMessage(IMessage message)
18+
{
19+
_displayStrategy.OutputText(message.Render(), Color.Red);
20+
}
21+
}

src/Lab3/Display/DisplayDriver.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Drawing;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab3.Display;
4+
5+
public class DisplayDriver
6+
{
7+
private readonly IDisplayStrategy _strategy;
8+
9+
public DisplayDriver(IDisplayStrategy strategy)
10+
{
11+
_strategy = strategy;
12+
}
13+
14+
public void OutputText(string text, Color color)
15+
{
16+
_strategy.Clear();
17+
_strategy.SetColor(color);
18+
_strategy.Out(text);
19+
}
20+
}

src/Lab3/Display/DisplayToConsole.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Drawing;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab3.Display;
5+
6+
public class DisplayToConsole : IDisplayStrategy
7+
{
8+
private Color _color;
9+
10+
public DisplayToConsole()
11+
{
12+
_color = Color.White;
13+
}
14+
15+
public void Clear()
16+
{
17+
Console.Clear();
18+
}
19+
20+
public void SetColor(Color color)
21+
{
22+
_color = color;
23+
}
24+
25+
public void Out(string text)
26+
{
27+
Console.WriteLine(Crayon.Output.Rgb(_color.R, _color.G, _color.B).Text(text));
28+
}
29+
}

src/Lab3/Display/DisplayToFile.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Drawing;
3+
using System.IO;
4+
5+
namespace Itmo.ObjectOrientedProgramming.Lab3.Display;
6+
7+
public class DisplayToFile : IDisplayStrategy
8+
{
9+
private Color _color;
10+
11+
public DisplayToFile()
12+
{
13+
_color = Color.White;
14+
}
15+
16+
public void Clear()
17+
{
18+
Console.Clear();
19+
}
20+
21+
public void SetColor(Color color)
22+
{
23+
_color = color;
24+
}
25+
26+
public void Out(string text)
27+
{
28+
const string path = "test.txt";
29+
30+
if (File.Exists(path)) return;
31+
32+
using StreamWriter sw = File.CreateText(path);
33+
sw.WriteLine(Crayon.Output.Rgb(_color.R, _color.G, _color.B).Text(text));
34+
}
35+
}

src/Lab3/Display/IDisplayStrategy.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Drawing;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab3.Display;
4+
5+
public interface IDisplayStrategy
6+
{
7+
void Clear();
8+
void SetColor(Color color);
9+
void Out(string text);
10+
}

src/Lab3/Filter/IFilter.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab3.Filter;
4+
5+
public interface IFilter
6+
{
7+
bool Filter(IMessage message);
8+
}

src/Lab3/Filter/PriorityFilter.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab3.Filter;
4+
5+
public class PriorityFilter : IFilter
6+
{
7+
private int _priorityFilter;
8+
9+
public PriorityFilter(int priorityLevel = 0)
10+
{
11+
_priorityFilter = priorityLevel;
12+
}
13+
14+
public void SetFilter(int priorityLevel)
15+
{
16+
_priorityFilter = priorityLevel;
17+
}
18+
19+
public bool Filter(IMessage message)
20+
{
21+
return message.Priority >= _priorityFilter;
22+
}
23+
}

src/Lab3/Group/GroupRecipient.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab3.Group;
5+
6+
public class GroupRecipient : IRecipient
7+
{
8+
private readonly IEnumerable<IRecipient> _recipients;
9+
10+
public GroupRecipient(IEnumerable<IRecipient> recipients)
11+
{
12+
_recipients = recipients;
13+
}
14+
15+
public void HandleMessage(IMessage message)
16+
{
17+
foreach (IRecipient recipient in _recipients)
18+
{
19+
recipient.HandleMessage(message);
20+
}
21+
}
22+
}

src/Lab3/IRecipient.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Itmo.ObjectOrientedProgramming.Lab3.Message;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab3;
4+
5+
public interface IRecipient
6+
{
7+
void HandleMessage(IMessage message);
8+
}

src/Lab3/IRenderable.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab3;
2+
3+
public interface IRenderable
4+
{
5+
string Render();
6+
}

src/Lab3/Lab3.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<RootNamespace>Itmo.ObjectOrientedProgramming.Lab3</RootNamespace>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Crayon" />
9+
</ItemGroup>
10+
711
</Project>

src/Lab3/Logger/ILogger.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab3.Logger;
2+
3+
public interface ILogger
4+
{
5+
void Log(string message);
6+
}

src/Lab3/Logger/Logger.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab3.Logger;
4+
5+
public class Logger : ILogger
6+
{
7+
public void Log(string message)
8+
{
9+
Console.WriteLine(message);
10+
}
11+
}

src/Lab3/Message/IMessage.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab3.Message;
2+
3+
public interface IMessage : IRenderable
4+
{
5+
int Priority { get; }
6+
}

src/Lab3/Message/Message.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab3.Message;
2+
3+
public class Message : IMessage
4+
{
5+
private readonly string _header;
6+
private readonly string _body;
7+
8+
public Message(string header, string body, int priority)
9+
{
10+
_header = header;
11+
_body = body;
12+
Priority = priority;
13+
}
14+
15+
public int Priority { get; }
16+
17+
public string Render()
18+
{
19+
return _header + '\n' + _body;
20+
}
21+
}

0 commit comments

Comments
 (0)