jeudi 26 janvier 2017

Decouple dependency using design pattern

I am trying to decouple dependency using design pattern.Can someone please point me best design pattern to decouple BookStorage dependency from Book Class? I tried doing factory pattern and factory method but got confused what to use?

namespace DesignPatterns
    {
        public interface IBook
        {
            string Execute(Guid bookId);
        }
    }

namespace DesignPatterns
{
    public class Book : IBook
    {
        public string Execute(Guid bookId)
        {
            var Id = 2;
            var storage = new BookStorage(Id);// Decouple this dependency? 

            // Get data
            var response = storage.GetBookData("GOF");
            return response;
        }
    }
}

namespace DesignPatterns
{
    public interface IBookStorage
    {
        string GetBookData(string name);
    }
}

namespace DesignPatterns
{
    public class BookStorage : IBookStorage
    {
        public BookStorage(int id)
        {

        }
        public string GetBookData(string bookName)
        {
            throw new NotImplementedException();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire