I'm having a hard time trying to get my team comfortable with interface based programming ... anyone have some suggestions?
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- Object.create() bug?
- std::vector of objects / pointers / smart pointers
- Name for a method that has only side effects
相关文章
- List可以存储接口类型的数据吗?
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- NameError: name 'self' is not defined, eve
- Implementation Strategies for Object Orientation
- Check if the Type of an Object is inherited from a
- When to use Interfaces in PHP
- Can Eclipse auto-generate an interface of a 3rd pa
- Are default parameters bad practice in OOP?
A copy of Head First design patterns or atleast the code examples from Head first Design patterns
Story telling like the way its done in Head First Design Patterns actually works great many time with skeptics
regards Edwards
I like the analogy of Legos, which are the classes and instances, and the pegs/holes on the top/bottom respectively as well as the sizes and shapes, which are the interfaces. For example, I may have a need for for a rectangular block with pegs in a grid of 2x4. These are the interfaces i'd be interested in - the pegged male/female inteface, as well as the rectangle interface. These can be satisfied by either an instance of the Rectangular Block Lego, or another instance of something "composed" of two half-sized Legos, of 2x2 pegs each, thereby still adhering to said interface. Because all the Legos have a common interface (male and female pegs) we can mix and combine however necessary, whether the piece comes from one of those new sets where I can build a giant robot ant, or is just a plain old square or rect. lego that comes in the giant vat you used to get back in the day. Hell, I can even mix official Legos with brand X legos, because the "male/female pegged interface" is so well understood.
Programming to interfaces allows us to mix and match reusable code components like we do Legos.
An interface is like a class's dress code. If a class implements an interface, its public members (its appearance to other classes, if you will) will include what the interface declares at minimum.
I would suggest you show them the benefits that can come from it. Imagine this C# scenario:
Here we have a
User
class that will go and get a first name from somewhere and return it. Now your app is coupled to this class and it will be difficult to later change where you get first name from because you have used theUser
class everywhere. What would happen if you implemented a service and wanted to begin calling that service for theFirstName
value? You would have a bit of refactoring to do.Consider this approach:
Now you can implement the interface in any class you want like:
and just change the factory method like:
and your app code will be none the wiser.
Tell them that interfaces are a trick word. Because the meaning is hidden right out in the open :P
Interfaces describe... an INTERFACE. They tell how you can interface with a class that implements it.
What are they familiar with?
When I learned C++, the teacher built on my existing knowledge of C, for example:
Building on top of that, I might compare an IFile interface to a File class by saying that IFile exposes no implementation details at all: if the application uses IFile instead of File, then you can change the underlying file system / implementation details).