简体   繁体   中英

Is Proxy or state design pattern or perhaps iterator pattern more suitable in this case?

In my application, all operations that are done are on the currently selected object which makes me think state design pattern is best fit. However all objects that are selected one by one are essentially the same so there is really no difference in each state except the item is just a different one.

My situation is a lot like directory structure. I have a collection of folders where each folder may have a different files but it is still a folder. Whatever folder is selected, user can only see contents of that folder or add files. My program also iterators through all folders (assume one level) and shows content of all files under the folder.

I am confused, how should I maintain the current state (=currently selected folder) in my application?

Add-on

What is also confusing me is the next state logic which I am not sure where it belongs. Please assume I am working with special folders My Documents , My Pictures and My Music . The transition logic (for simplicity) is if user adds 10 files to any of these folders or the total size of each folder exceeds 10 MB, it should transition to the next folder. Furthermore application should first show My Documents , than transition into My Pictures and than transition into My Music . This is why I was thinking it as a state as well due to each folder need to know which folder it will need to transition.

you can use combination of state and iterator pattern. If the states of the object are simple, you can implement state pattern just by using some enums representing folder state like OPEN or CLOSED. Iterator helps you maintain & access folder objects in general way.

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