javacodegeeks.com

New life of old Visitor design pattern

IntroductionVisitor [1, 2] is a widely known classical design pattern. There are a lot of resources that explain it in details. Without digging into the implementation I will briefly remind the idea of the pattern, will explain its benefits and downsides and will suggest some improvements that can be easily applied to it using Java programming language. Classical Visitor[Visitor] Allows for one or more operation to be

javacodegeeks.com

Two ways to extend enum functionality

PrefaceIn my previous article I explained how and why to use enums instead of switch/case control structure in Java code. Here I will show how to extend functionality of existing enums.IntroductionJava enum is a kind of a compiler magic. In the byte code anyenum is represented as a class that extends abstract class java.lang.Enum and has several static members. Therefore enum cannot extend any other class

javacodegeeks.com

Featured enum instead of switch

Problem and  its solutionSwitch/case is the common control structure implemented in most imperative programming languages. Switch is considered more readable than series of if/else.Here is a simple example:// Switch with int literal switch (c) { case 1: one(); break; case 2: two(); break; case 3: three(); break; default: throw new UnsupportedOperationException(String.format("Operation %d is not supported", c)); }Here is the list of the main problems