Layering Design Pattern in Java clean code style

As a Java developer, I am developing trying to use the clean code rules. But in my team we are facing a concrete problem:

We have a business layer offering a service called “createObject”, this service makes a lot of operation which can result to problem. E.g: parentObjectDontExist, objectAlreadyExist, dontHaveAuthorizationToCreate, operationFailed…

and we want the UI using this service to display different information messages depending which error occurred.

In old java dev, we should have create all signed exception type and throw it in createObject.

As Clean code says, it is forbidden to use Exception for business logic AND signed exceptions are evil…
Why not…But i don’t know how to solved this problem and i don’t want to use return code.

How do you do?
Thanks for youre experience return.

3

The answer is to use Callbacks

Create an interface that defines outputs of the system and instead of throwing an exception, call the right method from this interface. You may see it as “convert” one of your signed exception to a method in that interface.

This is also true for non-error “return” values. As you already know, you want to avoid exceptions and return values, in most* of your code. What you really want to do, is to have a callbacks, or multiple types of callbacks to use in order to pass the information to the proper handler.

That gives you the opportunity to create the proper handler that will implement your “error” interface and deal with the specifics of it.

(Same thing for “non-errors” paths!)

This will shape your code in a way that everything will be small, defined and extendable.(Read Single Responsibility Principle and Open Close principle from the SOLID principles, clean code, uncle Bob Martin.) By using the interfaces, you also comply to the L and D of the SOLID principles.. Better than that, if you have created the interfaces in a way that each one of them represents the method of one “domain”, one “client”, than, you also comply to I.

reminder:

S. Single Responsibility Principle
O. Open/Closed Principle
L. Liskov Substitution Principle
I. Interface Segregation Principle.
D. Dependency Inversion Principle.
  • sometimes, it may happen to return a value and use an exception. The trick is to catch the “known” exception and use the callback methods of your output interface.

  • Checked exceptions are not evil: sometimes they can be handy e.g. while writing a critical library. But in fact Runtime Exceptions are prefered in most scenarios.
  • Define exceptions in terms of caller’s needs. Does the caller need to know about all the specific exceptions you mention here: parentObjectDontExist, objectAlreadyExist, dontHaveAuthorizationToCreate, operationFailed? If not, then it will be just enough to have one common exception type (e.g. CreatingObjectException) to be thrown by the service with a relevant information for the UI.

Clean code doesn’t say checked exceptions are evil or forbidden. It just says that in most scenarios they are a burden and it is not necessary to take it.

All your examples are exceptional cases that should not happen, because you defined preconditions and assume they are met. Uncle Bob meant exactly such a case when he said you should favor unchecked exceptions over checked ones.

Nobody says you are not allowed to catch (unchecked) exceptions. So, you may wrap your call in a try-catch block and show the error message, or, more elegant, handle it on a higher level (most UI frameworks have a built in exception handler that shows a generic message).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật