开发者

Use of interface from Software Design Point of view [duplicate]

开发者 https://www.devze.com 2023-03-03 08:40 出处:网络
This question already h开发者_StackOverflow社区as answers here: Closed 11 years ago. Possible Duplicate:
This question already h开发者_StackOverflow社区as answers here: Closed 11 years ago.

Possible Duplicate:

Use of Java [Interfaces / Abstract classes]

Hi Friends,

This is not about any new thing this question is just about INTERFACES

I am aware about how interfaces are used and what are interfaces.But I want to know how interfaces actually helps from Design point of view. Any one can please give me explanation for Interface uses. You can give any example for this.

Thanks.


From a design best practice point of view, one of the big advantages of programming to an Interface, along with Dependency Injection (automated using something like Spring or via constructor/setters) is that it reduces coupling in your code. This means that each individual class is more self contained and that changes to undelrying details are less likely to leak throughout other classes.

If you have an application with strong coupling, it results in changes that are made result in a ripple effect throughout the code.

Essentially, if you program to an interface, you should be able to make underlying changes to implementations without having a knock on effect in other classes.

The best practice concept of "Program to an Interface" is brought up in the Gang of Four Design Pattenr book http://en.wikipedia.org/wiki/Design_Patterns

Also, check out page 17 of the following pdf: http://userpages.umbc.edu/~tarr/dp/lectures/OOPrinciples-2pp.pdf

It gives some advantages then a code example.


Both Interfaces and Abstract classes help you program to the interface and not to an concrete implementation.

This mean that you can use more efficiently the OOP principles. I suggest you to read

Head First Object Oriented Design and Analysis.

This books helps me a lot.


From the SCJP 6 book:

When you create an interface you're defining a contract for what a class can do, without saying anything about how the class will do it. An interface is a contract.

For example, when you have a Interface Movable you can define the methods which have to be implemented by a implementation of the interface, like: drive() or setMovingSpeed(). The interface says nothing about how to drive but only that it's there and should be declared by subclasses of the interface (like Bike or Car..)..


You can achieve some valuable principles of a good software design with interfaces (abstraction), the principles/rules like Open/Close Principle, close for modification but open for extension, or use Composition rather than Inheritance etc. You can use benefits of polymorphism with interfaces (abstraction), changing program behaviour dynamically at runtime like described in Strategy Pattern. You can find more stuff in Software Engineering books and i think it isn't possible to discuss all of these in a single thread. Search for Design Patterns, Object oriented programming, Abstraction in Computer Science documents.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号