Domanda

Can you tell me if it is Factory, Strategy or MVC design pattern?

public interface MainObject<T>
{
    void add();
    T get();
}

class Person1 : MainObject<Person1>
{
    public Person1(int id, string name) 
    { 
        // define
    }

    public void add() 
    {
        // add
    }

    public Person1 get()
    {
        // return
    }
}

class Person2 : MainObject<Person2>
{
    public Person2(int id, string name, bool status) 
    { 
         // define
    }

    public void add() 
    {
        // add
    }

    public Person2 get()
    {
        // return
    }
}

class Client
{
    public User()
    {
    }

    public void add<T>(T obj) where T : Object<T>
    {
        obj.add();
    }

    public T get<T>(T obj) where T : Object<T>
    {
        return obj.get();
    }
}

static class Program
{
    static void Main()
    {
        Client client = new Client();
        client.add( new Person1(123,"Duke") );
        client.add( new Person2(456,"Dave",true) );
        Person1 foundPerson1 = client.get( new Person1(123,null) ); // (123,"Duke")
        Person2 finedPerson2 = client.get( new Person1(null,"Dave",null) ); // (456,"Dave",true)
    }
}

I wrote my code with factory and strategy patterns, but i saw here the realization of MVC MVC pattern differences, and it is as my code. Now i confused what pattern is my code.

È stato utile?

Soluzione

The Strategy pattern is a way of providing different behaviour without the calling code being concerned by the implementation. For example:

interface IPersonRepository{
    IPerson GetPerson();
}

class PersonRepository : IPersonRepository{
    public IPerson GetPerson(){
        return GetDataFromDatabase();
    }

    private IPerson GetDataFromDatabase(){
        // Get data straight from database.
    }
}

class PersonRespositoryWithCaching : IPersonRepository{
    public IPerson GetPerson(){
        IPerson person = GetDataFromCache();
        // Is person in cache?
        if(person!=null){
            // Person found in cache.
            return person;
        }else{
            // Person not found in cache.
            person = GetDataFromDatabase();
            StoreDataInCache(person);
            return person;
        }
    }

    private IPerson GetDataFromCache(){
        // Get data from cache.
    }

    private IPerson GetDataFromDatabase(){
        // Get data straight from database.
    }
}

class Program{
    static void Main(){
        IPersonRepository repository = null;

        if(cachingEnabled){
            repository = new PersonRepositoryWithCache();
        }else{
            repository = new PersonRepository();
        }

        // At this point the program doesn't care about the type of
        // repository or how the person is retrieved.
        IPerson person = repository.GetPerson();
    }
}

The Factory pattern is similar to the Strategy pattern in structure but rather than implement different generic behaviours factories create different objects. For example:

interface IPerson{}

class SomePerson : IPerson{}

class OtherPerson : IPerson{}

interface IPersonFactory{
    void IPerson Create();
}

class SomePersonFactory : IPersonFactory{
    public void IPerson Create(){ return new SomePerson(); }
}

class OtherPersonFactory : IPersonFactory{
    public void IPerson Create(){ return new OtherPerson(); }
}

The MVC pattern is a way of ensuring that the code to display data to the user isn't mixed in with the code to retrieve it or manipulate the application. The Model holds the data in memory, the View displays it to the user and the Controller allows the user to interact with the data and the application.

Optional patterns such as Repository and Service keeps separate the code that interacts with the external data source or performs any business logic.

The advantage of the MVC patterns is that you can swap implementations of the various components to do things such as retrieve data from a database or a web service, or display the data as a table or a graph. Again, this is similar to the Strategy pattern.

Your code has elements of a Model and a Repository. However (as well as missing the other components) the code is mixed together so it isn't truly MVC.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top