Skip to content

Instantly share code, notes, and snippets.

@antonioj-mattos
Forked from ralfw/layered.cs
Last active June 19, 2022 19:47
Show Gist options
  • Save antonioj-mattos/773ff7447fd3d35641d0a3aac22fba84 to your computer and use it in GitHub Desktop.
Save antonioj-mattos/773ff7447fd3d35641d0a3aac22fba84 to your computer and use it in GitHub Desktop.
Layered design vs stratified design
using System;
using System.Linq;
namespace layered
{
class MainClass
{
public static void Main(string[] args) {
var data = new DataLayer();
var business = new BusinessLayer(data);
var presentation = new PresentationLayer(business);
presentation.Show();
}
}
class PresentationLayer {
readonly BusinessLayer business;
public PresentationLayer(BusinessLayer business) {
this.business = business;
}
public void Show() {
Console.Write("Text: ");
var text = Console.ReadLine();
var n = this.business.Count_words(text);
Console.WriteLine($"Number of words: {n}");
}
}
class BusinessLayer {
readonly DataLayer data;
public BusinessLayer(DataLayer data) {
this.data = data;
}
public int Count_words(string text) {
var words = Extract_words(text);
return words.Length;
}
private string[] Extract_words(string text) {
var words = text.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
return Remove_stopwords(words);
}
private string[] Remove_stopwords(string[] words) {
var stopwords = this.data.Load_stopwords();
words = words.Except(stopwords).ToArray();
return words;
}
}
class DataLayer {
public string[] Load_stopwords() {
return System.IO.File.ReadAllLines("stopwords.txt");
}
}
}
using System;
using System.Linq;
namespace stratified
{
class MainClass
{
public static void Main(string[] args) {
var data = new Data();
var business = new Business();
var presentation = new Presentation();
var app = new App(presentation, business, data);
app.Run();
}
}
class App {
readonly Presentation presentation;
readonly Business business;
readonly Data data;
public App(Presentation presentation, Business business, Data data) {
this.presentation = presentation;
this.business = business;
this.data = data;
}
public void Run() {
var text = presentation.Ask_for_text();
var n = Count_words(text);
presentation.Display_word_count(n);
}
private int Count_words(string text) {
var stopwords = data.Load_stopwords();
return Business.Count_words(text, stopwords);
}
}
class Presentation {
public string Ask_for_text() {
Console.Write("Text: ");
return Console.ReadLine();
}
public void Display_word_count(int n) {
Console.WriteLine($"Number of words: {n}");
}
}
class Business {
public static int Count_words(string text, string[] stopwords) {
var words = Extract_words(text);
words = Remove_stopwords(words, stopwords);
return words.Count();
}
private static string[] Extract_words(string text) {
return text.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
}
private static string[] Remove_stopwords(string[] words, string[] stopwords) {
return words.Except(stopwords).ToArray();
}
}
class Data {
public string[] Load_stopwords() {
return System.IO.File.ReadAllLines("stopwords.txt");
}
}
}
import scala.io.Source
import scala.io.StdIn.readLine
import scala.util.Using
type Text = String
type Words = List[String]
object Application:
def main(args: Array[String]): Unit = run()
def run(): Unit =
val text = Presentation.askForText()
val count = wordCount(text)
Presentation.displayWordCount(count)
private def wordCount(text: Text): Int =
Business.wordCount(text, Data.fetchStopWords())
object Presentation:
def askForText(): Text =
println("Text: ")
readLine()
def displayWordCount(count: Int): Unit =
println(s"Number of words: $count")
object Business:
def wordCount(text: Text, stopWords: Words): Int =
nonStopWords(words(text), stopWords).length
private def words(text: Text): Words =
text.split(Array(' ', '\t', '\r', '\n')).toList
private def nonStopWords(words: Words, stopWords: Words): Words =
words.filter(!stopWords.contains(_))
object Data:
def fetchStopWords(): Words =
Using(Source.fromFile("stopWords.txt")) { source =>
source.getLines.toList
} getOrElse List()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment