<?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>FactoryPattern.com &#187; architecture</title>
	<atom:link href="http://www.factorypattern.com/category/architecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.factorypattern.com</link>
	<description>Just another Object Oriented Weblog</description>
	<lastBuildDate>Sat, 02 Jan 2010 22:08:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Method Chaining</title>
		<link>http://www.factorypattern.com/method-chaining/</link>
		<comments>http://www.factorypattern.com/method-chaining/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 00:48:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/?p=64</guid>
		<description><![CDATA[Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on &#8220;this&#8221; object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different [...]]]></description>
			<content:encoded><![CDATA[<p>Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on &#8220;this&#8221; object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different objects, usually written in the same line.</p>
<p>For example we can build a rectangle class using using the Method Chaining for setters. You can see the setters are a little bit different than regular setters:</p>
<pre name="code" class="java">class ChainedRectangle
{
    protected int x;
    public ChainedRectangle setX(int x) { this.x = x; return this; }

    protected int y;
    public ChainedRectangle setY(int y) { this.y = y; return this; }

    protected int width;
    public ChainedRectangle setWidth(int width) { this.width = width; return this; }

    protected int height;
    public ChainedRectangle setHeight(int height) { this.height = height; return this; }

    protected String color;
    public ChainedRectangle setColor(String color) { this.color = color; return this; }
}
</pre>
<p>Then if we need to build a rectangle we can write a single line for setting all the required values:</p>
<pre name="code" class="java">new         Rectangle().setX(10).setY(10).setWidth(13).setHeight(15).setColor("red");</pre>
<p>Along with autocomplete option in most of todays IDEs method chaining might provide a suggestive way of doing some actions in less code.<br />
<span id="more-64"></span><br />
For example we can write a simple class to build  sql queries. Then we can invoke in a simple way.</p>
<pre name="code" class="java">public class Query
{
    protected String queryString = "";

    public Query select(String what)
    {
        queryString = "SELECT " + what;
    }

    public Query from(String from)
    {
        queryString = " FROM " + from;
    }

    public Query where(String where)
    {
        queryString = " WHERE " + where;
    }

    public ResultSet Invoke()
    {
        //... database invocation bla bla
        return result;
    }
}</pre>
<p>Then, to build the query we invoke:</p>
<pre name="code" class="java">new Query().select("*").from("table").where("a=b").Invoke();</pre>
<p>One of the problems of the Method Chaining pattern is the fact that it can not ensure the order of the invoked method. In order to do that each method can return a specific interface object which implements only the allowed methods.</p>
<p>For example we define an interface ISelectedQuery with a single method from, IFromQuery with a single method where. ISelectedQuery.from returns and object of type IFromQuery and IFromQuery returns a Query object. All the interfaces are implemented by the Query object. That way the order of invoking the methods is ensured.</p>
<p>Here is how the code looks like:</p>
<pre name="code" class="java">public interface ISelectedQuery
{
    IFromQuery from(String from);
}

public interface IFromQuery
{
    Query where(String where);
}

 public class Query implements ISelectedQuery, IFromQuery
{
    protected String queryString = "";

    public ISelectedQuery select(String what)
    {
        queryString = "SELECT " + what;
        return this;
    }

    public IFromQuery from(String from)
    {
        queryString = " FROM " + from;
        return this;
    }

    public Query where(String where)
    {
        queryString = " WHERE " + where;
        return this;
    }

    public ResultSet Invoke()
    {
        //... database invocation bla bla
        return result;
    }
}</pre>
<p>The above code ensure that only the right methods can be invoked. Further more the syntax autocomplete option from most of today IDEs will show in the drop down only the right method(s).</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/method-chaining/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/method-chaining/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/method-chaining/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>A few thoughts about memory cache</title>
		<link>http://www.factorypattern.com/a-few-thoughts-about-memory-cache/</link>
		<comments>http://www.factorypattern.com/a-few-thoughts-about-memory-cache/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 14:11:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[architecture]]></category>
		<category><![CDATA[memory cache]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[scalability]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/?p=44</guid>
		<description><![CDATA[I was starting today to look on different approaches and techniques that are used in scalable web or non web applications. One technique used in many large systems is simply called &#8220;memory cache&#8221;. It means that data are cached in memory so the data will not be queried again. 
Cache and memory cache exists for [...]]]></description>
			<content:encoded><![CDATA[<p>I was starting today to look on different approaches and techniques that are used in scalable web or non web applications. One technique used in many large systems is simply called &#8220;memory cache&#8221;. It means that data are cached in memory so the data will not be queried again. </p>
<p>Cache and memory cache exists for a long time; even the hardware parts link hard disks cd units have some sort of memory cache. </p>
<p>Why memory cache becomes so important when we talk about web applications? It&#8217;s simple. Because web applications happens to fulfill thousands of requests simultaneously. Or sometimes they have to. It&#8217;s obvious that keeping the data into memory and reuse it the next time you need it it will improve the performance. And it looks very simple. At the first view simple hashmap would do the job, unless&#8230;</p>
<p>There a few facts we need to be consider in real world:</p>
<ul>
<li>What happens when the database is updated?</li>
<li>In most cases a web application create a thread for each http request. The same thing happens in java of php or other languages. The creation of the threads is handled by the web server, web container, and not by the code we write. </li>
<li>The scalable applications run distributed on multiple servers. If one application change the database, the cache system should be informed on all the machines.</li>
</ul>
<p><span id="more-44"></span></p>
<p>The cache cache is very simple. If the data can be retrieved from cache it will be retrieved from cache. If not then it should be retrieved from database and put in cache:</p>
<pre><code>if (data is in cache)
   retrieve data from cache
if (data not in cache)
   retrieve data from database
   add data to cache
use data</code></pre>
<h2>How to handle database updates?</h2>
<p>Even if we have a simple application and we use a simple hashmap as a memory cache we still have to address this issue. When we update a database the cache should be updated with the new data or the old data should be removed from the cache.</p>
<p>Each time we update something in the database we have to remove the updated entities from the cache. We should take a special care because we our changes might affect other entities kept in cache:</p>
<pre><code>update database  
invalidate saved and affected entities from cache</code></pre>
<h2>Memory Cache in Web Applications</h2>
<p>As I said web applications are special applications. For each http request a thread will be created. In order to make sure that everything will work fine we must ensure that:<br />
- All the http request threads will access the same data in cache. There a many ways to achieve it, singletons can be taken into discussion.<br />
- The way the data is accessed is synchronized &#8211; one thread will not read the data while another one is updating it.</p>
<h2>Distributed applications</h2>
<p>The applications where memory cache is required are the applications with many users and usually they run in distributed environments. There are options here:<br />
- each server instance will have its own memory cache. If in one instance an entity is changed the affected entities should be invalidated in the cache on all the other server instances. The server instance changing the database will have to trigger the propagation the changes to other server instances.<br />
- the cache will run as a separate process and all the server instances will use it in common, connecting remotely to read/invalidate cached data.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/a-few-thoughts-about-memory-cache/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/a-few-thoughts-about-memory-cache/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/a-few-thoughts-about-memory-cache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.587 seconds -->
