Homework - Week 14 (Design Patterns) : 1. Logger (Singleton)
Homework - Week 14 (Design Patterns) : 1. Logger (Singleton)
Homework - Week 14 (Design Patterns) : 1. Logger (Singleton)
1. Logger (Singleton)
2. Logger (Factory)
Create a Logger interface, with 3 methods (info/warn/error, similar to class from 1, each
should print the given line with a proper prefix..)
Create also a LoggerFactory class, which will be responsible for creating a logger
instance, of one of the available types:
- should have a getLogger(LoggerType) method, which will return a logger
implementation depending on requested type (LoggerType is an enum, currently
with 2 values, for the 2 logger types)
Question2: if you need to add a new type of logger in the future (like a database logger),
what parts of your current code would need to be changed?
Provide an easy/clear way for some other client code to safely build valid User object,
for all combinations of possible parameters (making sure they always give the required
one, allowing to also give any of the optional ones, and doing all needed final
validations for received values)
- Hint: use the Builder pattern :)
Write now a UsersBook class, to store and show info on a list of User objects (think of
it as the next Facebook ;) )
It should have at least 2 methods:
- void add(User... users) - for adding new User instances to current list
- void showUsers() - it will print the list of current users, one per line (hint: add
toString() method to User and use that here..)
4. Student (Adapter)
Having a List of such Student objects, you want now to be able to easily add them to
UsersBook (from ex.3, it accepts only User objects).
How can you do that in an generic/reusable way? (hint: use Adapter pattern..)
Regarding the way the email address is generated for each Student, while converting it
to a User, it is decided later that:
- it needs to be more flexible, so it should support a few different ways to do it (and
be able to easily add new ways in the future)
- should also allow the client code to choose a different way for each Student
object to be adapted (don't force a single way for all)
You should support for now 2 different email generation strategies, generating emails
with this format:
- strategy1: "<first letter of student first name>.<student last name>@tuiasi.ro"
- strategy2: "<student first name>_<student last name>@tuiasi.ro"
Change your adapter class from 4) so that it will receive, besides the Student to wrap,
also an instance of EmailGenerationStrategy, and then use that strategy to generate the
user email address for that student.
Question: if later you need to add a 3rd way to generate email addresses, how much of
your code would need to change?