<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Design Patterns &#8211; Pauls Blog</title>
	<atom:link href="https://sterl.org/tag/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>https://sterl.org</link>
	<description></description>
	<lastBuildDate>Fri, 08 Mar 2024 12:10:24 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Outbound-Pattern Microservices Architektur: Sichere Datenübergabe in verteilten Systemen</title>
		<link>https://sterl.org/2024/03/outbound-pattern-microservices-architektur-sichere-datenuebergabe-in-verteilten-systemen/</link>
					<comments>https://sterl.org/2024/03/outbound-pattern-microservices-architektur-sichere-datenuebergabe-in-verteilten-systemen/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Fri, 08 Mar 2024 12:08:51 +0000</pubDate>
				<category><![CDATA[Pattern & Best Practice]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Micro Services]]></category>
		<category><![CDATA[Outbound Pattern]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[software architektur]]></category>
		<guid isPermaLink="false">https://sterl.org/?p=1020</guid>

					<description><![CDATA[In verteilten Systemen, wie Microservice-Architekturen, stellen sich unweigerlich folgende Fragen: Das Outbound-Pattern kann uns hierbei helfen, dieses Problem zu lösen. Es handelt sich um eine bewährte Methode, um Datenkonsistenz und Transaktionen in verteilten Systemen zu gewährleisten.]]></description>
										<content:encoded><![CDATA[
<p>In verteilten Systemen, wie Microservice-Architekturen, stellen sich unweigerlich folgende Fragen:</p>



<ol class="wp-block-list">
<li>Wie können wir Daten/Updates sicher bzw. transaktional zwischen den Services übergeben?</li>



<li>Wie schaffen wir es, dass die unterschiedlichen Services nicht auseinanderlaufen?</li>
</ol>



<p>Das Outbound-Pattern kann uns hierbei helfen, dieses Problem zu lösen. Es handelt sich um eine bewährte Methode, um Datenkonsistenz und Transaktionen in verteilten Systemen zu gewährleisten.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Outbound-Pattern Microservices Architektur: Sichere Datenübergabe in verteilten Systemen" width="640" height="360" src="https://www.youtube.com/embed/Nzdpcuk5GdQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2024/03/outbound-pattern-microservices-architektur-sichere-datenuebergabe-in-verteilten-systemen/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JavaScript Module Pattern or Just Private Variables</title>
		<link>https://sterl.org/2015/07/javascript-module-pattern-or-just-private-variables/</link>
					<comments>https://sterl.org/2015/07/javascript-module-pattern-or-just-private-variables/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sat, 11 Jul 2015 14:14:01 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Pattern & Best Practice]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[Module Pattern]]></category>
		<guid isPermaLink="false">http://sterl.org/?p=62</guid>

					<description><![CDATA[JavaScript is a wonderful language to get thinks fundamentally wrong from the start. As so where are many patters around to help us to avoid some of these challenges ;-). The module pattern is one of the most known and used. It helps us to create private variables or better to avoid side effects in&#8230;]]></description>
										<content:encoded><![CDATA[
<p>JavaScript is a wonderful language to get thinks fundamentally wrong from the start. As so where are many patters around to help us to avoid some of these challenges ;-).</p>



<p>The module pattern is one of the most known and used. It helps us to create private variables or better to avoid side effects in a larger JS application between the different JS files we load or frameworks we use.</p>



<h2 class="wp-block-heading">The problem</h2>



<p>Overall the problem is that in JavaScript everything is more or less public. Currently, most frameworks register their name in the Global space, also known as the window Object. You know them as <code>angular</code> or just <code>$</code> or maybe even as <code>Y</code>. </p>



<p>Imagine the following code (<a rel="noreferrer noopener" href="http://output.jsbin.com/muxekusezi/1" target="_blank">jsBin</a>): </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-material" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}">// first JS file
var CLASS_NAME = &quot;A1&quot;;
function A1() {}
A1.prototype.sayHallo = function() {
  $('#out').html(CLASS_NAME + &quot;: says hallo!&quot;);
};

// second class in a different JS file
var CLASS_NAME = &quot;B1&quot;;
function B1() {}
B1.prototype.sayHallo = function() {
  $('#out').html(CLASS_NAME + &quot;: says hallo!&quot;);
};

// some code
$('#cmdA1').click(function() {
  new A1().sayHallo();
});
$('#cmdB1').click(function() {
  new B1().sayHallo();
});</pre></div>



<p>It doesn&#8217;t matter which button you press the result will always be <code>B1</code>&nbsp;because we have overwritten the value in <code>CLASS_NAME</code> in the second part.</p>



<p>Even if this sample is somehow of course constructed, this problem can easily happen in a larger JS application. We must take this into account an protect ourself.</p>



<h2 class="wp-block-heading"><strong>The solution</strong></h2>



<p>The solution is pretty straight forward. We just wrap our class definition into an anonymous function. This does not only protect us but it allows us to create really private variables in JS which can&#8217;t be overwritten from outside &#8212; we can protect the access to them with functions.</p>



<p><a href="http://output.jsbin.com/fegexecisu/2" target="_blank" rel="noreferrer noopener">jsBin again</a></p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-material" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;application/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}">// first JS file
MyGlobal = {};
(function($) {
  var CLASS_NAME = &quot;A1&quot;;
  function A1() {}
  A1.prototype.sayHallo = function() {
    $('#out').html(CLASS_NAME + &quot;: says hallo!&quot;);
  };
  MyGlobal.A1 = A1;
})(window.$); // pass any global scope

// second class in a different JS file
(function($) {
  var CLASS_NAME = &quot;B1&quot;;
  function B1() {}
  B1.prototype.sayHallo = function() {
    $('#out').html(CLASS_NAME + &quot;: says hallo!&quot;);
  };
  MyGlobal.B1 = B1;
})(window.$);

// some code
$('#cmdA1').click(function() {
  new MyGlobal.A1().sayHallo();
});
$('#cmdB1').click(function() {
  new MyGlobal.B1().sayHallo();
});</pre></div>



<h2 class="wp-block-heading"><strong>What have we learned?</strong> </h2>



<ul class="wp-block-list"><li>In JavaScript, the best practice is to wrap our code into anonymous functions</li><li>Define one global scope variable which we use to register all our classes (here MyGlobal)</li><li>Anonymous function also allow us to create private/ protected variables</li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2015/07/javascript-module-pattern-or-just-private-variables/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Strong IDs</title>
		<link>https://sterl.org/2015/06/pattern-strong-id/</link>
					<comments>https://sterl.org/2015/06/pattern-strong-id/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sat, 13 Jun 2015 11:01:24 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Pattern & Best Practice]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[Strong ID]]></category>
		<category><![CDATA[StrongID]]></category>
		<guid isPermaLink="false">http://sterl.org/?p=36</guid>

					<description><![CDATA[In some projects you may encounter the so called &#8222;strong id pattern&#8220;. What is it? Strong IDs are simply wrapped primitive or simple types into classes. Just remember what we did in Java before the Enum come around. We had just Strings or Numbers in the code which we used to assign well known values&#8230;]]></description>
										<content:encoded><![CDATA[<p>In some projects you may encounter the so called &#8222;strong id pattern&#8220;.</p>
<h2>What is it?</h2>
<p>Strong IDs are simply wrapped primitive or simple types into classes. Just remember what we did in Java before the <code>Enum</code> come around. We had just Strings or Numbers in the code which we used to assign well known values to specific objects. In some projects these Strings has already been wrapped into classes. Now with <code>Enums</code> this is mostly obsolete. But for Ids we can still apply this pattern.</p>
<p>Imagine the following classes:</p>
<pre class="lang:java decode:true" title="Simple Class">class Office {
  String id;
  String name;
}

class Person {
  String id;
  String name;
}
// we could use any string we want, even the ID from the office
interface PersonService {
  Person getById(String id);
}</pre>
<p>Both classes have an <span style="color: #ffffff; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;"><span style="font-size: 14px; line-height: 21px; background-color: #333333;">id</span></span>, simply just as a <code>String</code>. Keeping just these ids in hand you cannot tell if it is a <code>Person Id</code> or the <code>Office Id</code>. To make it more obvious we could change the code too:</p>
<pre class="lang:default decode:true" title="Simple class with Strong ID">final class OfficeId {
  String value;
}
class Office {
  OfficeId id;
  String name;
}

final class PersonId {
  String value;
}
class Person {
  PersonId id;
  String name;
}
// we can now use the real type, compile time save
interface PersonService {
   Person getById(PersonId id);
}</pre>
<p>If you now have either the <code>OfficeId</code> or <code>PersonId</code> in hand, you cannot accidentally mix them up. You exactly know what you have and what it identifies.</p>
<p>Usually the ID classes are not mutable too.</p>
<h2>Advantages</h2>
<p>In larger projects you have many classes, it is easier to understand and keep different IDs in method signatures. That we use the right type in generic lists or method calls can be ensured now during compile time.</p>
<p>Also nice we can easyer change the type of ID, e.g. from <code>String</code> to <code>Long</code> or to a composite type.</p>
<p>If we move this sample to REST where we usually want to return a URI as an identifier to an object this pattern even gets more attractive. If we assume our IDs have been initially just simple Longs wrapped into a Strong ID we could change the value it a String and create URI / String which we return to the clients.</p>
<h2>Disadvantages</h2>
<p>Well no pro without a contra. Having strong ids in the system makes the handling sometimes awkward. E.g. if you serialize this to JSON you get:</p>
<pre class="lang:js decode:true ">// with strong ID
{ id: {value: "abc"}, name: "Main Office"}

// without strong id, or what you usually want
{ id: "abc", name: "Main Office"}</pre>
<p>Furthermore Hibernate nor simple bean serializer/ deserializer can handle well immutable classes. Overall we would need to add additional code to handle that. e.g. Hibernate custom types etc.</p>
<p><a href="https://github.com/sterlp/training/tree/master/jpa-hibernate/src/main/java/org/sterl/jpa/strong_id">Github JPA example</a></p>


<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Kompakt: JPA | Hibernate | eigene @Id Datentypen |  Strong ID Pattern" width="640" height="360" src="https://www.youtube.com/embed/wPiE4hF6Si0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2015/06/pattern-strong-id/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
