developer tip

인터페이스를 사용하는 이유는 무엇입니까?

copycodes 2020. 10. 11. 10:47
반응형

인터페이스를 사용하는 이유는 무엇입니까? 표준화만을위한 것인가?


인터페이스를 사용하는 이유는 무엇입니까?

표준화만을위한 것인가?


인터페이스의 목적

  • 느슨하게 결합 된 소프트웨어 생성
  • 계약 별 지원 설계 (구현자는 전체 인터페이스를 제공해야 함)
  • 플러그 형 소프트웨어 허용
  • 서로 다른 개체가 쉽게 상호 작용하도록 허용
  • 클래스의 구현 세부 정보를 서로 숨 깁니다.
  • 소프트웨어 재사용 촉진

비유 1 : 미국 우주 왕복선과 마찬가지로 러시아 소유즈 우주선과 중국 선저우 5 호는 모두 동일한 도킹 인터페이스를 구현하기 때문에 국제 우주 정거장에 도킹 할 수 있습니다. (이것은 단지 예일뿐입니다. 실생활에서 사실인지 모르겠지만 예를 들어 우리의 불신을 중단합시다)

비유 2 : 다양한 컴퓨터 모니터를 가정용 컴퓨터에 연결할 수 있습니다. 벽면 크기의 TV, 구형 CRT (두꺼운 종류), 20 인치 평면 스크린 또는 시각 장애인이 터치로 "볼"수있는 점자 기계를 연결할 수 있습니다. 이러한 다양한 / 다양한 장치와 사용자의 호환성이 있습니다. 모두 인터페이스 표준에 동의하기 때문입니다.

C # 인터페이스의 세부 사항 -C # / OOP 인터페이스를 사용하면 보이지 않는 / 가상 세계에서 동일한 작업을 수행합니다.

당신은에 대한 올바른있어 표준화 뿐만 아니라, 유연성 , 확장 성 , 확장 성 , 유지 보수성 , 재사용 , 테스트 용이성전원 .

(소프트웨어 인터페이스를 더 많이 사용할수록 이러한 "버즈 워드"가 더 많이 이해 될 것입니다. 그리고 인터페이스가 우리를 똑같이 잘 수행했기 때문에 항상 현실 세계의 인터페이스를 고려하십시오.)


인터페이스는 구현 된 것이 무엇을 할 수 있는지 설명하는 데 사용됩니다. 따라서이 인터페이스 유형과 동일한 인터페이스를 구현하는 여러 개체를 처리 할 수 ​​있습니다.

예를 들면 :

public interface IMyInterface{
    public void DoFirst();
    public int DoSecond();
}


public class A : IMyInterface{
   //class has to implement DoFirst and DoSecond
   public void DoFirst(){
     Console.WriteLine("Blubb1");  
   }

   public int DoSecond(){
     Console.WriteLine("Blubb2");
     return 2;  
   }
}

public class B : IMyInterface{
   //class has to implement DoFirst and DoSecond
   public void DoFirst(){
     Console.WriteLine("Blibb1");  
   }

   public int DoSecond(){
     Console.WriteLine("Blibb2");  
     return 4;
   }
}

클래스는 여러 가지 방법으로 인터페이스를 구현합니다. 그러나 IMyInterface로 사용할 수 있습니다. 예를 들면 :

public static void DoMethodsInInterface(IMyInterface inter){
    inter.DoFirst();
    inter.DoSecond();
}


public static void main(){

   DoMethodsInInterface(new A());
   DoMethodsInInterface(new B());
   //Or use it in a List
   List<IMyInterface> interlist = new List<IMyInterface>();
   interlist.Add(new A());
   interlist.Add(new B());
   foreach(IMyInterface inter in interlist){
      inter.DoFirst();
   }

}

I hope this makes a bit clear why interfaces are useful.


It's for interfacing :), so that you could interface between stuff, it's useful when you have

  • multiple implementations of same stuff
  • when you apply an interface to multiple different classes because you need some sort of convention that these classes are goonna be able to do some stuff or have some functionality

Here's the high level view...

Interfaces play a big role in the concept of Information Hiding.

They basically help you hide the implementation details of your class so that a calling class does has no dependency on that implementation. Therefore, by using interfaces you can modify the implementation without changing the calling class. This all in turns limits the complexity of your code and make it easier to maintain in the long run.

When I first started understanding interfaces they were explained to me as a "contract that provides a description your class." Not sure if that will help you but if you think of an interface for a car you could say that it drives, breaks, and turns. So as long as it gets me from point A to point B, I don't really have to know how those functions are implemented.


The main reason the interfaces are used in languages like C#/Java is because those languages don't support multiple (class) inheritance (see What is the exact problem with multiple inheritance?).

But multiple (interface) implementation is permited allowing classes to be used in diferent ways.


Interfaces are somewhat awkward. They support design by contract just by believing, that same name and implemented interface means the same behaviour. This works only thanks to API documentation, it has to be human-checked. That makes interfaces too weak. One way to get around that could be formal specs. On the other hand, interfaces are too strong, too strict. You cannot evolve interfaces which often gets in the way of reuse. This is solved by protocols - mechanism in dynamic languages, which send messages(call methods) and when that message is not supported by receiver, standard callback gets called. Having concrete protocols with constraints would be imho better.


Think remoting...

There is a client and a server involved here. Lets say they are physically separated by the internet. The client is calling a method whose actual execution happens on the server. From the client's perspective the client doesn't know anything about the object in the server which performs the execution. However it knows what method to call. Because while building the client program, we are only exposed to an interface (or contract). We are not exposed to the whole object which is actually living on the server. Try doing some demo apps in .net remoting, and you'll figure the rest. Happy programming.


Why do we use interfaces?

Some languages implement polymorphic method calls using vtables and discard most of the type information making it hard not to define interfaces.

So sometime we simply use interfaces because the language design requires it.


By starting with an interface, you can implement a proxy, thus allowing for lazy loading or performing some verifications when calling the methods of a concrete implementation.


Interface separates the data type from the implementation logic.


Interface provide prototype modal that just contains declaration of functionality of a specific behavior.

and if u want to implement this behavior into class then u must implement this interface in class then class have this behavior functionality or it can have multiple behavior.

because class can implement multiple interface.


If anyone else is like me and learns by example and doing, rather than only explanation, here is some code....

I found this implementation of a Neural Network in C#, including project download, which makes use of Interfaces in an elegant and useful manner:

http://www.c-sharpcorner.com/UploadFile/rmcochran/AI_OOP_NeuralNet06192006090112AM/AI_OOP_NeuralNet.aspx


Following are the main reasons for the use of interfaces

  1. Extensibility
  2. Implementation Hiding
  3. Accessing object through interfaces
  4. Loose Coupling.

please visit this link to know about interfaces with downloadable code example

참고URL : https://stackoverflow.com/questions/2026054/why-do-we-use-interface-is-it-only-for-standardization

반응형