English 中文(简体)
所有公共方法是否都符合商业逻辑?
原标题:Should all public methods correspond to business logic?
  • 时间:2012-05-21 10:45:26
  •  标签:
  • c++
  • oop

所有公共方法是否都符合商业逻辑?

  • if yes, how to deal with situation in which two objects need to communicate at lower design layer, and thus need some non-business methods to be public? (or is this antipattern?)
  • if no, how to clearly distinguish between public and business public methods?

以下是我所知道的选择:

  • create business logic interfaces (as Attila and ArjunShankar suggest)
  • #define BUSINESS (in C++) and then use as BUSINESS void myMethod()- not sure if it is good idea

还有别的可能吗?

最佳回答

如果您的语言支持接口, 您可以使用它来暴露基于上下文的不同行为( 如果您的语言支持多重继承, 请使用 ) :

Interface Kickable /* For  business logic .  */
  method kick (Direction)
  method dribble (Technique)

Interface PhysicsObject /* For collaboration with other objects.  */
  method collide_with (PhysicsObject)
  method fall (gravity)

Class Football Implements <Kickable, PhysicsObject>

这是相当干净的, 因为没有人能看到他们应该使用的方法:

Football football = new Football ();
/* Let the physics engine deal with PhysicsObjects.  */
physics_engine.add (PhysicsObject (football));

/* Let players deal with it Kickables.  */
game.set_ball (Kickable (football));
问题回答

public 只是表示该类以外的代码可以访问成员(例如,如果采用方法,可以打电话),它与商业逻辑无关。

如果您需要将对象的界面限制在使用该对象的某些代码中, 您可以让该类执行只列出所需方法的界面, 并通过该界面传递对象( 即方法参数 s 类型为界面), 这样在对象上运行的代码只能访问想要的方法 。

是否真的有可能知道外部使用哪些方法,哪些方法不应该使用? 在我的经验中,随着用户要求的改变,用于各班间内部交流的方法往往在日后以出人意料的方式被使用。所以我首先建议写好好的单位测试,以核实你们班内所有公开方法的行为。

在C++中,有一套用来实现你的目标的智商,这个智商是:https://stackoverflow.com/ questions/60570/Why-should-the-pimpl-idiom-be-used?>为什么使用“PIMPL”智商?





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签