<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammed DURRA</title>
    <description>The latest articles on DEV Community by Muhammed DURRA (@mhddurrah).</description>
    <link>https://dev.to/mhddurrah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F74148%2Fce597aa4-98d4-46c4-b295-6e2ccdad6758.png</url>
      <title>DEV Community: Muhammed DURRA</title>
      <link>https://dev.to/mhddurrah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mhddurrah"/>
    <language>en</language>
    <item>
      <title>If Injection</title>
      <dc:creator>Muhammed DURRA</dc:creator>
      <pubDate>Thu, 31 Dec 2020 09:17:36 +0000</pubDate>
      <link>https://dev.to/mhddurrah/joke-3f0</link>
      <guid>https://dev.to/mhddurrah/joke-3f0</guid>
      <description>&lt;p&gt;Hackers use 𝗦𝗤𝗟 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 to destroy the database&lt;br&gt;
Developers use 𝗜𝗳 𝗜𝗻𝗷𝗲𝗰𝘁𝗶𝗼𝗻 to destroy the code :D&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Practical Java15 sealed interfaces with design patterns</title>
      <dc:creator>Muhammed DURRA</dc:creator>
      <pubDate>Tue, 24 Nov 2020 17:44:26 +0000</pubDate>
      <link>https://dev.to/mhddurrah/cor-and-tm-design-patterns-with-java-sealed-interface-1cn8</link>
      <guid>https://dev.to/mhddurrah/cor-and-tm-design-patterns-with-java-sealed-interface-1cn8</guid>
      <description>&lt;p&gt;As a quick review of &lt;a href="https://en.wikipedia.org/wiki/Template_method_pattern" rel="noopener noreferrer"&gt;template method&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" rel="noopener noreferrer"&gt;chain of responsibility&lt;/a&gt; patterns we can define a template of the chain (no to have a broken chain) with an abstract class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class MiddlewareBase {
    static MiddlewareBase of(MiddlewareBase... middlewares) {
        for (int i = 0; i &amp;lt; middlewares.length - 1; i++) {
            middlewares[i].setNext(middlewares[i + 1]);
        }
        return middlewares[0];
    }
    private MiddlewareBase next;
    public void setNext(MiddlewareBase next) {
        this.next = next;
    }
    public final void check(Request request) {
        checkInternal(request);
        if (next != null) {
            next.checkInternal(request);
        }
    }
    abstract void checkInternal(Request request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until now this is good and seems perfect, now to add more abstraction and to achieve the program to interface not implementation simple principle let us define the Middleware interface to represent our chain from interface perspective&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Middleware {
    void check(Request request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then we can re-write our MiddlewareBase as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract class MiddlewareBase implements Middleware {
    static Middleware of(MiddlewareBase... middlewares) {
        for (int i = 0; i &amp;lt; middlewares.length - 1; i++) {
            middlewares[i].setNext(middlewares[i + 1]);
        }
        return middlewares[0];
    }
    private MiddlewareBase next;
    public void setNext(MiddlewareBase next) {
        this.next = next;
    }
    public final void check(Request request) {
        checkInternal(request);
        if (next != null) {
            next.checkInternal(request);
        }
    }
    abstract void checkInternal(Request request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with this and the two following simple examples of Middleware&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CsrfMiddleware extends MiddlewareBase {
    @Override
    void checkInternal(Request request) {
        System.out.println("csrf check");
    }
}
class XssMiddleware extends MiddlewareBase {
    @Override
    void checkInternal(Request request) {
        System.out.println("xss check");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can define and use our middleware chain like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Middleware middleware = MiddlewareBase.of(new CsrfMiddleware(), 
                                        new XssMiddleware());
middleware.check(new Request());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but nothing can prevent others from implementing the Middleware interface and break the chain implementation.&lt;br&gt;
— — — — — —&lt;br&gt;
Starting from version 15 and as a preview feature JDK presented the sealed class/interface feature to restrict which other classes or interfaces may extend or implement them.&lt;br&gt;
we can define a sealed interface/class with &lt;code&gt;sealed&lt;/code&gt; modifier, Then, after any &lt;code&gt;extends&lt;/code&gt; and &lt;code&gt;implements&lt;/code&gt; clauses, the &lt;code&gt;permits&lt;/code&gt; clause specifies the classes that are permitted to extend the sealed class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sealed interface Handler permits HandlerTemplate {
  void handle(Handleable handleable);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with this definition no class except &lt;code&gt;HandlerTemplate&lt;/code&gt; can implement the &lt;code&gt;Handler&lt;/code&gt; interface.&lt;br&gt;
Let us now redefine the Middleware example with sealed interface so no one can implement it and break the main purpose of it as a CoR implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sealed interface Middleware permits MiddlewareBase {
    void check(Request request);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The MiddlewareBase class will have no change except adding non-sealed modifier which means no restriction on implementation classes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;abstract non-sealed class MiddlewareBase implements Middleware {
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also &lt;code&gt;CsrfMiddleware&lt;/code&gt; and &lt;code&gt;XssMiddleware&lt;/code&gt; will not change.&lt;br&gt;
now if we tried to create another implementation of &lt;code&gt;Middleware&lt;/code&gt; compiler will fail complaining: &lt;code&gt;class is not allowed to extend sealed class: Middleware&lt;/code&gt; or with an IDE like intellij we get directly.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqd9okwu816mrg27xyyky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqd9okwu816mrg27xyyky.png" alt="Alt Text" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;sealed classes/interfaces have many useful usecases and eventought it is new -still in preview- in Java it is presented in other languages like &lt;a href="https://kotlinlang.org/docs/reference/sealed-classes.html" rel="noopener noreferrer"&gt;Kotlin&lt;/a&gt; (only sealed classes) and &lt;a href="https://docs.scala-lang.org/tour/traits.html" rel="noopener noreferrer"&gt;Scala&lt;/a&gt; (sealed trait) with different intents.&lt;br&gt;
I approached the sealed interfaces with core design principles and patterns to express the fact that the richer the language features the better design we get, comments are welcomed.&lt;/p&gt;

</description>
      <category>java</category>
      <category>patterns</category>
      <category>sealedclass</category>
      <category>abstraction</category>
    </item>
    <item>
      <title>Spring Stereotypes</title>
      <dc:creator>Muhammed DURRA</dc:creator>
      <pubDate>Thu, 23 Jul 2020 10:20:02 +0000</pubDate>
      <link>https://dev.to/mhddurrah/spring-stereotypes-4fhi</link>
      <guid>https://dev.to/mhddurrah/spring-stereotypes-4fhi</guid>
      <description>&lt;p&gt;Spring stereotype annotations are aliases for @Component annotation, like Service, Repository and Controller.&lt;br&gt;
Hence you can define your own stereotype and customize it like the Api component here and spring container will take the rest on initializing the suitable component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcn49230dd2njvytwkfq3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcn49230dd2njvytwkfq3.jpg" alt="Spring" width="800" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>repository</category>
      <category>annotation</category>
    </item>
    <item>
      <title>Inheritance without extends</title>
      <dc:creator>Muhammed DURRA</dc:creator>
      <pubDate>Sun, 26 May 2019 00:47:23 +0000</pubDate>
      <link>https://dev.to/mhddurrah/inheritance-without-extends-22hn</link>
      <guid>https://dev.to/mhddurrah/inheritance-without-extends-22hn</guid>
      <description>&lt;p&gt;Yes, inheritance means adding or changing the behavior of a class by a sub class&lt;br&gt;
the traditional way of doing inheritance is by extending a class adding new methods.&lt;br&gt;
Let us take an example from Java world&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8fvgxe95uykhpckyvva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8fvgxe95uykhpckyvva.png" alt="Image" width="800" height="162"&gt;&lt;/a&gt;&lt;/p&gt;
Super class



&lt;p&gt;extending this class to add a new behavior can be done directly and easily:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdl464m9uf9h9vdxldxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdl464m9uf9h9vdxldxh.png" alt="Image" width="800" height="163"&gt;&lt;/a&gt;&lt;/p&gt;
Inherited



&lt;p&gt;nice !&lt;/p&gt;

&lt;p&gt;But here the inheritance is static, I need to create a new class for every new functionality.&lt;br&gt;
Another way of adding functionality come from Design Patterns world, it is&lt;br&gt;
the Visitor Pattern which basically “represents an operation to be performed on the elements of an object structure.” but also can be utilized to define a new operation without changing the class.&lt;br&gt;
One mandatory pre-requirement for this pattern is to implement it :), the implementation is adding a behavior to the class to accept the Visitor.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakzmm387pae562uhs1a4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakzmm387pae562uhs1a4.png" alt="Image" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;
Visitor



&lt;p&gt;Visitor is an interface with single (or multiple) method(s)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdj4ejwyddz334d68tj33.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdj4ejwyddz334d68tj33.png" alt="Image" width="620" height="132"&gt;&lt;/a&gt;&lt;/p&gt;
Visitor Interface



&lt;p&gt;Let us implement the Sub class simulator -&amp;gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9mf23uj3d8na2cd76ca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9mf23uj3d8na2cd76ca.png" alt="Image" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;
Visitor Implementation



&lt;p&gt;But we had to add a new class for the new functionality! , Yes, but the good thing is that we can add the functionality dynamically to single objects of the class (the same idea applied in Decorator Pattern)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4pu8pu96a0fn527om74j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4pu8pu96a0fn527om74j.png" alt="Image" width="742" height="315"&gt;&lt;/a&gt;&lt;/p&gt;
Visitor Tester



&lt;p&gt;adding another Visitor with lambda expressions&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xnj9q2117ipzkfoc1zs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8xnj9q2117ipzkfoc1zs.png" alt="Image" width="795" height="154"&gt;&lt;/a&gt;&lt;/p&gt;
Using Lambda



&lt;p&gt;A full snippet&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F057psba3tg9flvijt8bb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F057psba3tg9flvijt8bb.png" alt="Image" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will not give a full control and capabilities of traditional inheritance but it helps :)&lt;/p&gt;

&lt;p&gt;As a final note: try to support visitor pattern whenever possible in your classes :)&lt;/p&gt;

&lt;p&gt;all comments are welcomed.&lt;/p&gt;

</description>
      <category>java</category>
      <category>inheritance</category>
      <category>patterns</category>
      <category>visitor</category>
    </item>
    <item>
      <title>Deployment of EJB server application and client independently</title>
      <dc:creator>Muhammed DURRA</dc:creator>
      <pubDate>Sun, 26 May 2019 00:18:02 +0000</pubDate>
      <link>https://dev.to/mhddurrah/deployment-of-ejb-server-application-and-client-independently-3eaf</link>
      <guid>https://dev.to/mhddurrah/deployment-of-ejb-server-application-and-client-independently-3eaf</guid>
      <description>&lt;p&gt;A friend asked me to work on an EJB and EJB client project in an independent separated deployment package for each.&lt;/p&gt;

&lt;p&gt;I get used to use Netbeans to do that along time ago, but I decided to do it with maven. Looking for that I found that Apache provided plugins of EAR and EJB application for maven:&lt;br&gt;
&lt;a href="https://maven.apache.org/plugins/maven-ear-plugin/" rel="noopener noreferrer"&gt;https://maven.apache.org/plugins/maven-ear-plugin/&lt;/a&gt; for deploying artifacts as a Java EE Enterprise Archive (EAR)&lt;br&gt;
&lt;a href="https://maven.apache.org/plugins/maven-ejb-plugin/" rel="noopener noreferrer"&gt;https://maven.apache.org/plugins/maven-ejb-plugin/&lt;/a&gt; which generates Java Enterprise JavaBean (EJB) file as well as the associated client jar.&lt;br&gt;
That was almost perfect though the documentation was not clear enough. then I looked for someone how used it and fortunately I found an example on github, it was good to some extent, but wasn’t organized well and I had build issues with maven.&lt;/p&gt;

&lt;p&gt;Making some efforts to re-organize the pom structure and fix some configurations I was able to build the EJB application as an isolated EAR file (because of dependencies on Hibernate it should be EAR not JAR).&lt;br&gt;
the plugin also generates EJB jar and EJB client jar for you and with maven you can add it easily as a dependency for your client application.&lt;/p&gt;

&lt;p&gt;The build of the EJB client application was straightforward with JSF, primefaces and the created EJB client jar built in the first step. then I used Glassfish server to deploy both artifacts.&lt;/p&gt;

&lt;p&gt;This was just a simple try to build isolated Enterprise Java applications but things may go complex with other requirements.&lt;/p&gt;

&lt;p&gt;You can check github for sources and build/deploy instructions&lt;br&gt;
&lt;a href="https://github.com/mhddurrah/ejb-application" rel="noopener noreferrer"&gt;https://github.com/mhddurrah/ejb-application&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/mhddurrah/ejb-jsf-client" rel="noopener noreferrer"&gt;https://github.com/mhddurrah/ejb-jsf-client&lt;/a&gt;&lt;br&gt;
and don’t hesitate to contact me for updates or other issues/ queries.&lt;/p&gt;

</description>
      <category>ejb</category>
      <category>jsf</category>
      <category>javaee</category>
    </item>
  </channel>
</rss>
