-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mikedegeofroy/lab-3
Lab 3
- Loading branch information
Showing
36 changed files
with
650 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Itmo.ObjectOrientedProgramming.Lab3; | ||
|
||
public interface IRenderable | ||
{ | ||
string Render(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.