LINUX.ORG.RU

История изменений

Исправление no-dashi, (текущая версия) :

В данной ситуации так и напрашивается решение использовать полиморфизм с наследованием.

В данной ситуации напрашивается решение вынести list_files в отдельный класс (т.н. стратегия, или прокси):

interface AnyVCS {
    Context open_repository(url,user,passwd);
    string [] list_files();
}

public class Mercurial : AnyVCS {
    public Mercurial() { ... }
    Context open_repository(url,user,passwd) { ... }
    string [] list_files(Context ctx) { ... }
}

public class Git : AnyVCS {
    public Git() { ... }
    Context open_repository(url,user,passwd);
    string [] list_files(Context ctx) { ... }
}

class Repository {
    string name;
    VCS vcs;
    Context ctx;
    public Repository(name,vcs) {
        this.name = name;
        this.vcs = vcs;
    }
    public void open(url,user,passwd) { ctx = vcs.open_repository(url,user,passwd); }
    public string [] list_files() { return vcs.list_files(cxt); }
}

Repository r = new Repository("Sample1",new Git(...));
Repository r = new Repository("Sample1",new Mercurial(...));

В результате добавление новой VCS сводится к реализации одного класса, который будет нести всю логику работы с этой VCS, а «модельные» классы (Repository, File, Branch, Version и т.п.) править уже не нужно.

Исходная версия no-dashi, :

В данной ситуации так и напрашивается решение использовать полиморфизм с наследованием.

В данной ситуации напрашивается решение вынести list_files в отдельный класс (т.н. стратегия, или прокси):

interface AnyVCS {
    Context open_repository(url,user,passwd);
    string [] list_files();
}

public class Mercurial : AnyVCS {
    public Mercurial() { ... }
    Context open_repository(url,user,passwd) { ... }
    string [] list_files(Context ctx) { ... }
}

public class Git : AnyVCS {
    public Git() { ... }
    Context open_repository(url,user,passwd);
    string [] list_files(Context ctx) { ... }
}

class Repository {
    string name;
    VCS vcs;
    Context ctx;
    public Repository(name,vcs) {
        this.name = name;
        this.vcs = vcs;
    }
    public void open(url,user,passwd) { ctx = vcs.open_repository(url,user,passwd); }
    public string [] list_files() { return vcs.list_files(cxt); }
}

Repository r = new Repository("Sample1",new Git(...));
Repository r = new Repository("Sample1",new Mercurial(...));

В результате добавление новой VCS сводится к реализации одного класса, который будет нести всю логику работы с этой VCS, а «моедльные» классы (Repository, File, Branch, Version и т.п.) править уже не нужно.