Search Blog

Search duranek.blogspot.com

Wednesday, February 8, 2012

Software Design Principles

Design Principles

1)    Open Close Principle
    Means : open for extension, closed for modification.
    Bir class'i degistirmek istiyorsan, extend et, degistirme.
    Bunun nedeni, backward compatibility, regression testing.

2)     Dependency Inversion Principle

    Dependency injection, for low coupling, inject the dependency,
instead of creating in your body.

3)    Interface Segregation Principle

    Interface'in icine sadece gerekli metodlari koy. Fazla koyarsan
gereksiz yere implementor lar, uygulamak zorunda kalir bu metodlari.

4)    Single Responsibility Principle.
    Eger bir class'in birden cok responsibility'si varsa, o class'i ayirmak lazim.
    Ayirmazsak, o class'i bir nedenden dolayi degistirdigimizde, diger
    nedenlerden dolayi da degistirmek zorunda kalabiliriz.

5)    Liskov's Subtitution Principle

    Child class 'in ozellikleri, base class in ozelliklerini bozmayacak.

    Yani child class'i istedigimiz zaman base class ile degistirebilmeliyiz.
    ve bir sorun yasamamaliyiz

Strongly Typed Language

Tipleri ozellikle belirterek variable yaratilan programlama dili

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.

Saturday, February 4, 2012

Set Up SSH Key For Github

Set Up SSH Key For GitHub

1) SSH key daha onceden var mi
$ cd ~/.ssh
eger no such file or directory diyorsa , daha onceden yok.

2) $ ssh-keygen -t rsa -C "xxx@gmail.com"

file ismi sorarsa enter'a bas
passphrase'ini gir enter'a bas.

C:\Documents and Settings\xxx\.ssh

directory sinde id_rsa.pub bizim ssh key'imiz.

3) Account Settings” > Click “SSH Public Keys” > Click “Add another public key”
e key i ekle. hic degistirme, ctrl-a ctrl-c ctrl-v

4) Daha sonra
$ ssh -T git@github.com
yes diyoruz
passphrase yaziyoruz.
ok aliyoruz.

5) bazi tool lar ssh kullanmadan github a connect olur.
bu durumlarda api token kullanmak lazim. Her password degistiriste token da degisir aklinizda olsun

bunu ekle

$ git config --global github.user username
$ git config --global github.token 0123456789yourf0123456789token

Friday, February 3, 2012

Builder Pattern ( Creational )

Builder Pattern

Aşçı elinde tarif varsa pizzayı pisirebilir.
Her tarif bir builder interface.

interface PizzaBuilder
    buildSouce()
    buildBread()

class HawaiiPizzaBuilder implements PizzaBuilder
    Pizza p;
    buildSauce(p.buildSauce()) //implementation
    buildBread(p.buildBread()) //implementation

class SpicyPizzaBuilder implements PizzaBuilder
    Pizza p;
    buildSauce(p.buildSauce()) //implementation
    buildBread(p.buildBread()) //implementation
    
class Cook ( Director )
    PizzaBuilder pizzaBuilder;
    setPizzaBuilder(Pizzabuilder p )
    constructPizza(pizzabuilder.buildSauce();pizzabuilder.buildBread();)
    getPizza(pizzaBuilder.getPizza())

class BuilderExample {
    main() {
        Cook cook = new Cook();
        PizzaBuilder hpizzabuilder = new HawaiiPizzaBuilder();
        cook.setPizzaBuilder(hpizzabuilder);
        cook.constructPizza();
        cook.getPizza();
    }
}
    

State Pattern (Behavioral)

State Pattern ( Behavioral )

    Once State interface'i yaratilir.
    Burada her state icin yapilacak davranislari listeleriz
    mesela 
        interface LanguageState
            sayNo()
            sayYes()
    
        class GermanState implements LanguageState
            sayNo() //implementation
            sayYes() //implementation
            
        class EnglishState implements LanguageState
            sayNo() //implementation
            sayYes() //implementation
            
        Simdi state'e sahip olacak class
        
        class Person implements LanguageState // O da LanguageState'i implemente ediyor !
            LanguageState ls=null;
            setLanguageState(LanguageState s) //implementation
            sayNo(return ls.sayNo()) //implementation
            sayYes(return ls.sayYes()) //implementation
        
        class Demo
            main() {
                Person p = new Person()
                p.setLanguageState(new GermanState());
                p.sayNo();
                p.sayYes();
            }
            
    Burada state'e gore Person farkli davraniyor. Strategy pattern , online olarak algoritmayi set ediyor. 
    Yani strategy her zaman bir metod. State Pattern'de, state e gore birden fazla metod'a sahip oluyor.
    
    State pattern de, gercek obje de aslinda bir state oluyor, yukarida LanguageState'i implemente ettigi icin.
    
    Runtime de degisiklige ugradigi icin, buna behavioral pattern deniyor.

Thursday, February 2, 2012

HSQLDB

hsqldb

1)    index yok
2)    tablo kolonlari ve size lerde limit yok gibi bir sey
3)    genelde standalone application lar icin yazildi. Her client a 
    mysql server kurmamak icin. multi user distributed client system icin
    uygun degil.
4)    unit test icin kullanilabilir.