• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not able to decide which Design is better and why ?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

General Query: An Object `O` has 2 different behaviors X and Y. There is another class `C` which is using `O`. O does X in one set of conditions and does Y in another set of conditions. Now there are 2 types of thoughts amongst us

First: Give another behavior to `O ` say R and let R decide what to do, either X or Y based on arguments to R.
Second: Let client `C` decide which behavior of `O` needs to be called based on conditions because conditions are not related to the state of an object O

Questions
1. Which one is better and why?
2. Are there any design principles which dictates which one to use?
3. Does design vary, if `decision` by class `O` is based on its state? Because we think then it could be encapsulated but otherwise not.

Concrete Example in Our Code Base:
Object O  = AwsEncryption
Client C = Controller
X = client_side_encryption
Y =  server_side_encryption
R = encrypt(content)

First Approach :

Inside Controller class :

 A. If the content is sensitive and client_side_encryption is enabled (Feature Flag) then O.client_side_encryption(content.value)
 B. If the content is sensitive and server_side_encryption is enabled (Feature Flag) and size of content is less than 4096 bytes then O.server_side_encryption(content.value)
 C  If content is not sensitive then do not call `O`

Inside KmsEncryption Class

  No decision, based on which method is called, I will do that

Second Approach :

Inside Controller Class:
 Call O.encrypt(content)  and let O decide whether to encrypt with client side or server side or not do encryption at all.

Inside KmsEncryption Class
 
  A. If the content is sensitive and client_side_encryption is enabled (Feature Flag) then call client_side_encryption(content.value)
  B. If the content is sensitive and server_side_encryption is enabled (Feature Flag) and size of contenr is less than 4096 bytes then O.server_side_encryption(content.value)
  C.  Do nothing
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing that came to mind were the GRASP principles.

Seems like you have a pretty good handle on the considerations, which I must say I find quite rare. Perhaps 1 in 10 understand the implications of their design choices.

I find these particularly astute: "... because conditions are not related to the state of an object O" and "Because we think then it could be encapsulated but otherwise not"

I would say go by the Principle of Least Astonishment when deciding which way to go.  Have one or more colleagues who are not familiar with the design try to look for the logic of where the decision is made. See how they go through the code to find it. Where do they look first? Why did they look there? If their reasoning is logical, that might be the most natural place to put that logic. If their reasoning is not sound, then maybe they need to be educated a little bit.

One thing I'll say: I wouldn't put it in the controller.  For me, a controller should just delegate to the component that knows the business rules for routing the request further. So given that you said the conditions are not dependent on the state of the O object, I would find the object or class where it makes most sense to put the "knowledge" of deciding whether to do client- or server-side encryption. That said, I would lean towards the second approach you described.

Caveat: I haven't seen your code so there's really no telling which way is better. For me, reading code that makes sense and that I can intuitively understand is what helps me decide whether I've made a good design choice or not.

Hope this helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic