B
B
brammator2014-07-20 11:30:41
Programming
brammator, 2014-07-20 11:30:41

How to schedule classes for an application a la pastebin.org?

I want to make an analogue of pastebin with the following logic:
1 The user pastes the text
2 The machine determines one of the types
3 Depending on the type, a brief brief is issued.
I write like this (Python code, but I think it is clear to any programmer):

class Paste(object):
    def save():
        # сериализация и сброс в базу
    def guesstype():
        # угадать тип пасты
    def list():
        # список недавних
    def __str__():
        # показать бриф пасты
class PythonCode(Paste):
    def brief():
        # найти на гитхабе используемые модули, вывести активность коммитов по ним
    def wrapsnippet():
        # ...
class MayakovskyPoetry(Paste):
    def brief():
        # подсчитать слоги в чётных строках
    def printhardcoverbook():
        # ...

Am I right? Some people advise "to write an all-encompassing logic with markers and expand it through the config in the future, since the code cannot be touched because it is impossible and that's it", but it seems to me that it will be easier to rewrite guesstype() of the base class and add a descendant class for the new type , more logical and clearer.
The logic of dividing into types is very non-trivial, with a bunch of "but if, then" and does not fit well with the "universal analyzer"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Pavlov, 2014-07-20
@brammator

You can try a separate resolver class with a single (?) method - to determine the type of text. It can also be a Factory to create the desired class. (I use c# style pseudo code, I think any programmer will also understand)

class Paste {
  string Brief() {}
  // other methods
}

class PythonCode : Paste {
  string Brief() {}
}
class MayakovskyPoetry : Paste {
  string Brief() {}
}

class Resolver {
  Paste Guess(string text) {}
}

And usage:
Paste text = Resolver.Guess(userText);
string brief = text.Brief();

S
Sergey, 2014-07-20
Protko @Fesor

guesstype shouldn't be in the base class at all, because it doesn't make sense. You can make an abstract isMatches method in the base class (just an empty method that throws an exception) and overload it in descendants. This method will determine if the given piece of code is suitable for the selected language. Although it would be even better to shove this logic into a separate component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question