bridge iterator foreach yield

Bridge pattern
The Bridge pattern allows two components, a client and a service, to work together with each component having its own interface. Bridge is a high-level architectural pattern and its main goal is to write better code through two levels of abstraction. It facilitates very loose coupling of objects. It is sometimes referred to as a double Adapter pattern.
An example of the Bridge pattern is an application (the client) and a database driver (the service). The application writes to a well-defined database API, for example ODBC, but behind this API you will find that each driver’s implementation is totally different for each database vendor (SQL Server, MySQL, Oracle, etc.).
Bridge is very similar to strategy!
Bridge dofactory – link
Bridge pattern vs Strategy pattern
“They both look the same on the surface to me as well. The main difference I see is the fact that in the Bridge pattern, the abstraction is PART OF the object, but in the Strategy pattern the abstraction is performed BY the object.
Bridge: ( A structural pattern)
Bridge pattern decouples abstraction and implementation and allows both to vary independently.
Use this pattern when :
- Abstractions and implementations have not been decided at compile time
- Abstractions and implementations should be changed independently
- Changes in implementation of abstraction should not affect caller application
- Client should be insulated from implementation details.
Strategy: ( Behavioural pattern)
Strategy patterns enable you to switch between multiple algorithms from a family of algorithms at run time.
Use Strategy pattern when :
- Multiple versions of algorithms are required
- The behaviour of class has to be changed dynamically at run time
- Avoid conditional statements”
Iterator pattern
The Iterator pattern allows clients to effectively loop over a collection of objects. A common programming task is to traverse and manipulate a collection of objects. These collections may be stored as an array or perhaps something more complex, such as a tree or graph structure. In addition, you may need to access the items in the collection in a certain order, such as, front to back, back to front, depth first (as in tree searches), skip evenly numbered objects, etc. The Iterator design pattern solves this problem by separating the collection of objects from the traversal of these objects by implementing a specialized iterator.
Today, many languages have Iterators built-in by supporting ‘for-each’-type constructs and IEnumerable and IEnumerator interfaces.
Iterator dofactory – link
foreach + yield + IEnumerable



You use a yield return statement to return each element one at a time.
The sequence returned from an iterator method can be consumed by using a foreach statement or LINQ query. Each iteration of the foreach loop calls the iterator method. When a yield return statement is reached in the iterator method, expression is returned, and the current location in code is retained. Execution is restarted from that location the next time that the iterator function is called.
You can use a yield break statement to end the iteration.
… full article
nice article on yield
Class code: foreach yield
Youtube code: foreach yield list
foreach + GetEnumerator



The foreach statement executes a statement or a block of statements for each element in an instance of the type that contains GetEnumerator method. The foreach statement is not limited to those types and can be applied to an instance of any type that satisfies the following conditions:
- has the public parameterless
GetEnumerator method whose return type is either class, struct, or interface type
- the return type of the
GetEnumerator — which has public Current property, public parameterless MoveNext method whose return type is Boolean and public Reset
(of course you can also implement System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interfaces)
Class code: foreach a class using enumerator
Youtube code: foreach my data collection
More topics covered:
- advantages of IEnumerable
- enumerate a non-existing collection
- ASP.NET REST IEnumerable
Links: