Using Interfaces - A Design Aspect

I am looking for a good and short article + examples of how to work with interfaces. I'm not interested in the technical part, but I need a piece of design. For example, how to program using interfaces, when and how to create implementations, develop patterns for regular development using interfaces.

I have many similar classes that repeat in several ways. I want to use interfaces and abstract classes to make things more modular, but I can't figure out how to do this properly.

+4
source share
4 answers

The interface defines the contract. This is a promise that the object will behave in a certain way. Before exploring interfaces, you usually think about objects in specific terms. For example, suppose we have a list of products:

List<string> products = new List<string>() { "desktop", "laptop", "server" }; 

And we have a method that outputs our products:

 void PrintProducts(List<string> products) { foreach (string product in products) { Console.WriteLine(product); } } 

Our method is associated with a specific type of list. Does it need to be? There are many different types of collections in C #: lists, arrays, ReadOnlyCollection, etc. All you really need to do is cycle through them. There are many methods on the list that are not in the array, but you are not using them here. Fortunately, they all implement the IEnumerable interface. This means that they are all β€œcontracted” so that they can be listed.

Method change as follows:

 void PrintProducts(IEnumerable<string> products) { foreach (string product in products) { Console.WriteLine(product); } } 

means that now you can pass an array, or a list, or some unique container that you yourself create.

Another example: suppose you have a data repository:

 public class DatabaseRepository { public void AddProduct(Product product) { // connect to the database // add the product } } 

And you have a class that needs this database:

 public class ProductManager { DatabaseRepository _repository; public ProductManager(DatabaseRepository repository) { _repository= repository; } } 

Unfortunately, this class is bound to your database. What to do if you decide to go to storage as an XML file or save it in some kind of storage of key cloud value values. You will have to modify the ProductManager, which is complex and error prone. Assume instead that we have defined an interface:

 public interface IRepository { void AddProduct(Product product); } 

Modifying our ProductManager class to use this interface:

 public class ProductManager { IRepository _repository; public ProductManager(IRepository repository) { _repository= repository; } } 

means that no matter what repository of this type, we know that there will always be an AddProduct (Product product) method. Now we can create our XML repository:

 public class XMLRepository : IRepository { public void AddProduct(Product product) { // write to an XML file } } 

Now we can go to any repository:

 ProductManager manager = new ProductManager(new DatabaseRepository()) 

or

 ProductManager manager = new ProductManager(new XMLRepository()) 

and our ProductManager behaves exactly the same. He absolutely does not know what a particular type is.

This becomes very useful when you get into unit testing. Inversion of control is what you want to read when you get a clear idea of ​​how interfaces work.

+13
source

This book is a canonical reference for design patterns and contains examples of the use of interfaces. However, you can start from an even more fundamental level. Thinking in Java is what led me to object-oriented programming, probably similar names for C #, although the content of the book should be mostly an agnostic language. You can also search online object-oriented programming tutorials.

Change A big book about patterns and their implementations and uses in C # C # 3.0 Design Patterns

+2
source

When to use interfaces An interface allows someone to start from scratch in order to implement their interface or implement their interface in some other code, the original or main purpose of which was very different from your interface. For them, your interface is only random, which must be added to their code in order to be able to use your package. The disadvantage is that each method in the interface must be publicly available. You may not want to reveal everything.

When to use abstract classes A, on the contrary, provides a large structure. It usually defines some default implementations and provides some tools useful for a full implementation. The trick using code is to use your class as a base. This can be very inconvenient if other programmers who want to use your package have already developed their own class hierarchy. In Java, a class can inherit from only one base class.

or read the following: http://mindprod.com/jgloss/interfacevsabstract.html

+1
source

@Naor seems to be based on your comment above: "It seems logical to create an interface for every class that I have, right?" what is the best book for you - HEAD FIRST; DESIGN PATTERNS - It has an incredible and easy way to learn how to use design patterns. I first read them in this book, and it finally changed my mind !!! After reading this, you will be able to read more complex things, such as Martin Fowler, "Enterprise Application Architecture Templates" - in my opinion, practical applications. Going straight to GoF, or M. Fowler, or more complex things can refuse you or make you just waste time.

+1
source

Source: https://habr.com/ru/post/1345011/


All Articles