19 Kasım 2018 Pazartesi

git add seçeneği - Yerel Depoya Dosya Ekler

Giriş
Dosyaları yerel depoya ekler. Daha sonra commit yapmak gerekir. Git remote add seçeneği ile kardeştir.

1. git add .
Açıklaması şöyle
Stage all changes files
2. git add . vs git add *
Her zaman "git add ." şeklinde kullanmak gerekir. Bu komut .gitignore dosyasındaki girdileri de dikkate alıyor. Açıklaması şöyle
In summary:
- You should use git add . not git add *.
 Örnek
Şöyle yaparız.
$ git add .
$ git commit -m 'First commit'
3. git add <file>
Açıklaması şöyle
Stage a file
Örnek
Şöyle yaparız.
$ git add myfile.txt
$ git commit -m initial
Örnek
Şöyle yaparız.
# Add file 1
git add sub_directory/file_1   

# now your file 1 will get the desired message
git commit -m "file_1 description"
-p seçeneği
Açıklaması şöyle
Use git add -p to review your changes and pick and choose which patches you want to be added.

9 Kasım 2018 Cuma

execl metodu - variable argument list

Giriş
Şu satırı dahil ederiz.
#include <unistd.h> 
İmzası şöyle.
int execl(const char *path, const char *arg, ...); 
Uygulama için önce path verilir. Daha sonra tüm parametreler teker teker geçilir. Son parametre yine NULL olmak zorunda.

Örnek
Şöyle yaparız
execl("/bin/sh", "sh", "-c", the_command_line, NULL);
Örnek
Şöyle yaparız
execl("/bin/sh", "sh", "-c", "...", (char *) 0);
Örnek
myprog'a 0 argc geçmek için şöyle yaparız.
execl("./myprog", NULL, (char *)NULL);

SQLite Insert Or Update

Giriş
Insert Or Update yerine kısaca Upsert deniliyor.

Örnek
Şöyle yaparız
INSERT INTO news(id, title, date, content) 
values(?, ?, ?, ?)
ON CONFLICT(Id) DO UPDATE 
    SET tile=?

SQLite Insert Or Replace

Giriş
Söz dizimi şöyle.
INSERT OR REPLACE INTO TABLE (column_list) 
VALUES (value_list);
Belirtilen satır yoksa insert gerçekleşir. Belirtilen satır varsa tüm satırı günceller. Eğer satır için değer verilmemişse default değeri kullanır.


Örnek
Şöyle yaparız.
INSERT OR REPLACE INTO news(id, title, date, content) values(?, ?, ?, ?)

6 Kasım 2018 Salı

GoF- Flyweight Örüntüsü - Immutable Nesneler İçeren Cache

Not : GoF Tasarım Örüntüleri yazısına bakabilirsiniz.

Prototype ile İlişkisi
Prototype yeni nesnelerin yarı bitmiş bir nesneden kopyalanarak yaratılması içindir. Örneğin bir oyundan EnemyPrototype ile bir çok düşman nesnesi yaratılabilir. Flyweight ise hazır nesnelerin paylaşılması içindir.

Memoization İle İlişkisi
Flyweight örüntüsü, Memoization örüntüsü ile ilişkili. Memoizaiton fonksiyon sonucunu saklarken Flyweight nesne saklar.

Factory İle İlişkisi
- Flyweight için tutulan cache bazen Factory içinde tutuluyor. Bu kullanım şeklinde Factory nesnenin yaratılması için gereken biraz daha karmaşık logic kodunu içeriyor

- Bazen de cache Flyweight nesnesinin içinde tutuluyor. Bu kullanım şeklinde nesnenin yaratılması karmaşık bir logic bulunmuyor. Örneğin Java'daki Integer.valueOf() metodu basit sayılır, dolayısıyla nesne içinde

Flyweight (Tüy Sıklet) Yapısal Örüntü
Bu örüntüde bazı nesneler önceden yaratılır ve tekrar tekrar kullanılır. Nesneler paylaşıldığı için immutable olması gerekir. Açıklaması şöyle
- The Flyweight pattern is used to minimize the memory usage by sharing as much data as possible with other similar objects.
- The Flyweight pattern provides a way to reduce the number of objects created and to decrease memory footprint and increase performance.
- The Flyweight pattern tries to reuse already existing similar kind objects by storing them in a collection which act like a cache and creates new object when no matching object is found.
- The Flyweight objects we create as immutable. This means that they cannot be modified once they have been constructed. Making flyweight objects as immutable helps while sharing them with other objects. 
Immutable Nesnelerin Doğası
Açıklaması şöyle. Intrinsic (içsel) veri değişmemeli. Extrinsic (dışsal) veri değişebilir ve flyweight içinde tutulmamalı 
The Flyweight object essentially has two different kind of attributes – 
- Intrinsic - An intrinsic (invariant) state attribute is stored and shared in the flyweight object. It is independent of flyweight’s context. So, as the best practice we should make intrinsic states immutable.

- Extrinsic - An extrinsic (variant) state attribute does not store and share in the flyweight object because it depends on flyweight’s context and varies as context change. Generally, we store and maintain the extrinsic state in the Client objects. We need to pass this extrinsic state to the flyweight object for object creation and processing.
Ekran Listeleri
Flyweight ekranda listelenen static nesneler için güzel bir kullanım şekli olabilir.

ObjectPool gibi düşünmek
Bazıları nesneler tekrar tekrar kullanıldığı için ObjectPool'un özelleşmiş bir hali olarak düşünüyorlar.

En çok bilinen örneklerinden birisi Java'daki Integer önbelleğidir. Ayrıca grafik kütüphanelerinde de bu örüntü sıkça görülebilir. Aşağıdaki şekilde bu örüntü görülebilir.

Flyweight Pattern Implementation - UML Class Diagram
Örnek
Şöyle yaparız. Burada Factory ve Flyweight birlikte kullanılıyor
public class ShapeFactory {

  private static final Map circleMap = new HashMap<>();

  public static Shape getShape(String Color) {
    Circle circle = (Circle) circleMap.get(Color);
    if (circle != null) {
      return circle;
    } else {
      circle = new Circle(Color);
      circleMap.put(Color, circle);
    }
    return circle;
  }
}

Tehlikeler
Eğer cache'e dışarıdan müdahale olursa kodda çok acaip hatalar olabilir
Örnek
Bu kod parçasında FlighWeight örüntüsü yüzünden 2+2 = 5 yapılması gösteriliyor.
import java.lang.reflect.Field;

public class Main {
  public static void main(String[] args) throws Exception {
    Class cache = Integer.class.getDeclaredClasses()[0];
    Field c = cache.getDeclaredField("cache");
    c.setAccessible(true);
    Integer[] array = (Integer[]) c.get(cache);
    array[132] = array[133];

    System.out.printf("%d",2 + 2);
  }
}

Memoization Örüntüsü

Giriş
Açıklaması şöyle.
Memoization is a specific form of caching that involves caching the return value of a function based on its parameters.
Memoization GoF - Flyweight Örüntüsü ile ilişkili ancak nesne saklamak yeriner fonksiyonun sonucunu saklıyor.

Örnek
Elimizde şöyle bir kod olsun
public class SimpleRecursiveFibonacci {

  /** Gets the fibonacci value for n */
  public final BigInteger fibonacci(int n) {
    if (n < 2) {
      return BigInteger.ONE;
    }
    return getFibonacci(n);
  }

  /** Recursively calculates the fibonacci by adding the two previous fibonacci. */
  protected final BigInteger calculateFibbonacci(int n) {
    return fibonacci(n - 2).add(fibonacci(n - 1));
  }

  /** 
   * Somehow get the fibonacci value for n.
   * Could be by calculation, getting it from a cache, or anything.
   */
  protected BigInteger getFibonacci(int n) {
    return calculateFibbonacci(n);
  }
}
Şöyle yaparız
public class MemoizedRecursiveFibonacci extends SimpleRecursiveFibonacci {

  private Map<Integer, BigInteger> cache = new HashMap<>();

  @Override
  protected BigInteger getFibonacci(int n) {
    BigInteger fib = cache.get(n);
    if (fib == null) {
      fib = recursiveCalculateFibbonacci(n);
      cache.put(n, fib);
    }
    return fib;
  }
}