开发者

Should I separate data from Facade Class?

开发者 https://www.devze.com 2023-01-05 11:58 出处:网络
I\'m making a \"back-end\" engine o开发者_如何学Cf HomeCAD. I have a facade class that do many things. But should I separate data (like array of object) from that class?

I'm making a "back-end" engine o开发者_如何学Cf HomeCAD. I have a facade class that do many things. But should I separate data (like array of object) from that class? Many thanks


Not entirely clear on what you are asking here - any class will require some data structures to work.

From the wikipedia page on Facade:

A facade is an object that provides a simplified interface to a larger body of code

You can see that the definition is rather loose, so in my opinion a facade can have data structures, so long as it simplifies the use of your other code.


Usually, a Facade class will simplify related code, for example, creating a simple class that groups together a sub system such as data adapters. If the purpose of your Facade is to simplify data access for a subsystem, you should not separate data from that class.

Note-- one example of a subsystem that comes to mind is having a library of data adapters that belong to several different data bases and have their own data sets.


You should not artificially separate data, because everybody understands under a class a collection of data and routines operating on this data. If you have for example methods updatePath or connectXY or redefine everybody assumes that there is some sort of internal state in your class that gets updated by these methods. In C++ you would even mark your method as not const, so then everybody is aware of the fact that you update the internal state of an object when calling such methods.

When you have separated out the data, then this relation between the methods names and the objects's states is lost. And typically your code becomes more complicated because you manually have to maintain the relation between your data and your object. But why should you do so if the class mechanic of Java or C++ can do this for you.

But typically you want to have the internal state invisible. So it is either hidden in a separate data object and linked into your objects of privately inherited from a base class. Especially when creating an API you don't want to expose anything of your data.

If your things over which you build the facade are not (ownable) data objects (but e.g. global data), then of course your facade will also not contain data.

0

精彩评论

暂无评论...
验证码 换一张
取 消