The picture above is the State Pattern implementation I solved in matlab, each state subclass is a Singleton mode, and the VOID * m_data in Array is determined by the current state. The type of pointer is determined. Adoption of this way 1, you can multiplex String, etc. 2, the change type does not need to reassign classes (some implementation requires a reassignment class), for example, Template implementation 3, one-dollar operator does not need loop judgment 4, m_data can Unified processing (the implementation of VCC_CN does not do this) 5, add new types that do not need to modify other programs, if the type and program are related (such as type information in the add program), all of these programs when the type is increased To modify, vast work and potential dangers
6, all STATE subclasses only allocate once, increase efficiency
Bridge, Strategy mode can be used to complete the dynamic selection of algorithm implementation, but the object cost is very large, and only one object is required to use the State mode. Compare these three modes We have found that if the algorithm itself does not have its own data, you can define the algorithm to execute (small trick) in a state. The COMPLEX class used in mathematical operation is relatively small. If all participating operations are defined as complex influence efficiency, there is a case where the type is changed, for example, x = 5 (double), x = X 5i, then the result x must be Complex. If you use the template implementation, you must reassign the class. Using State implementation, as long as the data pointer (void * m_data) is available, turn "Status" by Double into Complex, very smooth, if you run x-> sin (). Calling the implementation of Complex, very Smooth. There are still many problems in this mode, such as complex data types.
The dimension variable variable is also evil. There is no good way to smooth this change, examples: x = [1, 2, 3] (1D number) x (3, 3) = 6i (immediately becoming 3 * 3 2D Complex array), at this time, you should use the Complex function library to transform, it is very inconvenient.
Can send me