Search Blog

Search duranek.blogspot.com

Wednesday, February 8, 2012

Proxy Pattern ( Structural )

Proxy Pattern ( Structural )

1) Proxy class is used to control access to another class

3) Complex bir objeyi temsil eden basit bir obje olabilir proxy.
Mesela o objenin creation'i expensive ise, onu basit bir obje temsil edebilir.
ve eger cok gerekliyse gercekten yaratilabilir. ( Virtual Proxy ) 

4) surrogate pattern olarak da bilinir

5) 
-> The proxy object has the same interface as the target object

-> The proxy holds a reference to the target object and can forward requests to
the target as required (delegation!)

-> In effect, the proxy object has the authority the act on behalf of the client to
interact with the target object

6) Adapter different interface verir, proxy same interface verir. Decorator
enhanced interface verir.

7) Access controlleri yapan proxy. ( Proctection Proxy )

8) 

public interface Graphic {

        // a method used to draw the image
      public void draw();
}

public class Image implements Graphic {

    private byte[] data;

    public Image(String filename) {
        // Load the image
        data = loadImage(filename);
    }
        
    public void draw() {
        // Draw the image
        drawToScreen(data);
    }
}

// late initialization, caching
public class ImageProxy implements Graphic {

    // Variables to hold the concrete image
    private String filename;
    private Image content;

    public ImageProxy(String filename) {
        this.filename = filename;
        content = null;
    }

    // on a draw-request, load the concrete image
    //   if we haven't done it until yet.
    public void draw() {
        if (content == null) {
            content = new Image(filename);
        }
        // Forward to the Concrete image.
        content.draw();
    }
}

9) virtual proxy, firewall proxy, remote proxy, protection proxy etc..

Tuesday, February 7, 2012

Adapter Pattern ( Structural )

Adapter Pattern ( Structural Design Pattern)

1)    Adaptee Class : esas obje
    Target Interface : Adapter'in implemente edecegi Interface
    Adapter Class extends Adaptee implements TargetInterface

2)  Elimizdeki objeyi baska bir obje gibi
    kullanmak istiyorsak yazdigimiz arac,
    adaptor.
    
    Y yi de X gibi kullanmak icin.
    Amacin X gibi kullanmak, bu durumda
    target interface X
    
    X kapali kutu gibi dusun. Sadece metodlarini
    biliyorsun.Ya da degistirmek istemiyorsun X'i, ya da hakkin yok.
    
    Adapter implements X {
        member Y;  //adaptee
        Adapter(Y);
    }
    
    main () {
        X a = new Adapter(y);
        // boylece Y de x gibi oldu.
    }
    
3)    Legacy code ya da access olmayan
bir objeyi degistirmemek icin kullanilabilir.

4)    Two way adapter, iki interface
implemente etmek gerekiyor.

Sunday, February 5, 2012

SpringMVC Customize DispatcherServlet XML Name

Normalde DispatcherServlet'in mapping 
ismi xxx ise, xxx-servlet.xml isimli dosyayi
arar. Eger bunu degistirmek istiyorsak web.xml'e
asagidaki kodu eklemek lazim

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/SpringMVCBeans.xml</param-value>
  </context-param>
 
  <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>

Spring Prefix For Element Not Bound Hatasi


Eger spring.xml in asagidaki hatayi veriyorsa

<context:component-scan
  base-package="com.deploymentplan.server.user.GwtRpcController" />

the prefix "context" for element "context:component-scan" is not bound.

Bu,         http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd

 xsi:schemaLocation 'a eklenmis fakat xmlns:context="http://www.springframework.org/schema/context" eklenmemis demektir.

Spring MVC DispatcherServlet XML Replacement With Annotation


Spring MVC Controller 2.5.6

1)    @Controller and @RequestMapping to replace the XML configuration.
2)    Controller artik AbstractController'i extend etmiyor ya da Controller interface'ini implemente etmiyor.
    Bunun yerine @Controller yapmamiz yeterli
3)    XML icindeki HandlerMapping yapma geregi kalmadi
4)  <context:component-scan base-package="com.mkyong.common.controller" />
    bu pakette Controller arar.

DAO Design Pattern

DAO Design Pattern

1)    DAO layer yaratmamizim sebebi, persistence ile ilgili degistirmek
istedigimiz bir yer varsa, sadece burayi degistirecegiz.

2)     domain logic --> persistence mechanism

yerine

 domain logic --> DAO's --> persistence mechanism
 
 yapmak.
 
3)    3 tane sorun var: 
 
a) DAO layer yarattigimiz zaman, connection lari nasil handle edecegiz:

    1)    Method Scope  // iki metod icin iki connection mi olacak ? olmaz
    2)    Instance Scope // ya birden fazla dao varsa? Connection 'i kim yaratacak ? Kim kapatacak?
    3)    Thread Scope // ayni dao yu iki farkli thread cagirirsa ne olacak ? 
    
b) Transaction handling de ayni sekilde connection gibi.

c)    Exception handling in de dusunulmesi lazim.

4)    DAO Manager bu sorunlar konusunda devreye girer.
5)    DAOManager butun dao lari cagirir. Single point of access 
    
    

TDD Test Driven Development

1)    TDD : Test Driven Development
first we write the test
    1)    Write Test
    2)    Make it fail
    3)    Write the code
    4)    Pass the test
    5)    Refactor
    
TDD biraz daha yavas. Cunku test
kodlarini da yaziyorsun. Bir de kod stilini
degistirmek lazim.

legacy code ile de TDD yapabilirsin.
Testin yoksa kod degistirmek zor olabilir.
Cok yararli.

2)    Genelde en uygun bir feature icin 2 programci calistirmak.
biri test yazarken digeri kodu yaziyor. Fakat management istemez.

3) Bir degisiklikte test kodlarini da tekrar yazmak lazim.

4) Her developer yapmak istemez. Cunku test zevkli gelmeyebilir.

5) Cok disiplinli olmak gerekir, developer'in ne isterse yapmasini engeller. Mocking tool gerekir.
Ilk basta plani bilmiyorsan, yazilan testler anlamsiz olabilir.

6)    Her bug'a bir test case yazmak iyi fikir gibi.

7)    TDD agile methodology ye cok uygundur. Dokumantasyon vs. olmayan durumlarda
TDD yoksa patlayabilir.