简体   繁体   中英

Is HashSet implementation in Java a good example for Adapter Pattern

I have recently looked into the source code of HashSet Collection class and found out that It is backed by HashMap. HashSet is delegating all its method calls to HashMap. So I'm just wondering if this is a good example for adapter pattern .

I would say it's not an adapter.

Adapters wrap other objects to change their method signatures to match some other interface. That's not really the case here. HashSet isn't turning a Map into a Set. It just uses a set internally to do its work.

If you made a class that had a String field, would say you are adapting the string? No, you are just using it.

Not really, because functionality is acutally being lost. A java.util.Set only stores keys whereas a java.util.Map stores keys and values. They are not equivelant.

An adapter is typically something which adapts one interface to another where both interfaces do the same / similar thing, but have different signatures. An adapter could be made to adapt a java.util.Enumeration to a java.util.Iterator for example. Or perhaps an adapter between java.io.InputStream and java.io.Writer such as the java.io.InputStreamWriter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM