SOOlutions / HomeTaskApp

True OO implementation in Java of a Home Task Management System. This project is a port and modification from https://github.com/SilasReinagel/HomeTaskManagement
2 stars 1 forks source link

Abstract Injection for Account (Decorate Dependency???) #15

Open goqp opened 6 years ago

goqp commented 6 years ago
  1. Provide an abstraction to add existing transactions to a newly instantiated account object.

Currently, when an account is initialized to represent an existing real-world account, the transactions from that real account must be instantiated and added to the new account object. The current interface for Account provides for the injection of a collection containing the transactions. This is tight coupling that should be avoided. A possible solution is to decorate the Account with a shell that adds each transaction object to the Account by calling the appropriate method (e.g. "apply").

joshua-light commented 6 years ago

Can't we just use something abstract like IEnumerable from C#, which is kinda Sequence or Iterable, so there is NO more abstract way to represent an iterable sequence of elements. Will it be a tight coupling?

joshua-light commented 6 years ago

Another idea, which I've found really useful is kinda Builder pattern (sorry for er thing): we create inner class that allows us to build an account.

For example:

public class BasicAccount implements Account
{
    public class Builder
    {
        private List<Transaction> transactions = new ArrayList<Transaction>();

        public Builder with( Transaction t ) { this.transactions.add( t ); return this; }

        Account account( User user, string currency ) { return new BasicAccount( user, currency, transactions ); }
    }

    private BasicAccount( User user, string currency, List<Transaction> transactions ) { ... }
}

This List<Transaction> can now be treated as encapsulated, because noone knows about it. Building will be like this:

var account = new BasicAccount.Builder()
    .with( new SomeTransaction() )
    .with( new AnotherTransaction() )
    .account( new BasicUser(), "USD" );
goqp commented 6 years ago

Yea thats the right concept but the builder should be disguised as a decorator! So this way we can treat it as a unity, a single concept.

new DummyAccount( new Account() );