background preloader

Pattern

Facebook Twitter

C# 2.0'da Genellemeler - 1. Bu makalemizde ,NET Framework 2,0’la beraber C# diline ve CLR’ye eklenmiş olan generics (genellemeler) lerden bahsetmek istiyorum.

C# 2.0'da Genellemeler - 1

Genellemeleri kullanarak parametre olarak farklı tipleri kabul edebilen ve bunlarla çalışabilen metotlar, sınıflar yazabiliriz. C#’daki genellemeler C++’da yer alan templates (şablonlar) konusuyla benzerlikler gösteriyor. Genellemelere C++ şablonlarının C# uyarlanmış kullanışlı, güvenli yapılarak olarak bakabiliriz. C#’daki genellemelerle C++’daki şablonlar arasında çeşitli benzerlikler ve farklar var. İleriki yazılarda bu farklara değineceğiz. Genellemelere Giriş: C#’da genellemeleri kullanabilmemiz için System.Collections.Generic isim aralığını kodumuza eklememiz gerekir.

Genellemelerin Kullanım Amacı: Buraya kadar yazılanlar belki bize biraz anlamsız görülebilir. Buraya kadar umarız herşey iyi gitmiştir. Ancak ondalıklı sayılardan oluşan bir dizi içerisinde arama yapmak istediğimizde Ara fonksiyonumuz işlerliğini yitirecektir. Lazy initialization. This is typically accomplished by maintaining a flag indicating whether the process has taken place.

Lazy initialization

Each time the desired object is summoned, the flag is tested. If it is ready, it is returned. If not, it is initialized on the spot. In multithreaded code, access to the flag must be synchronized to guard against a race condition. See lazy evaluation for a general treatment of this idea. The "lazy factory"[edit] In a software design pattern view, lazy initialization is often used together with a factory method pattern. Using a factory method to get instances of a class (factory method pattern)Store the instances in a map, so you get the same instance the next time you ask for an instance with same parameter (multiton pattern)Using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern).

Examples[edit] Actionscript 3[edit] The following is an example of a class with Lazy initialization implemented in Actionscript: Basic Usage: Abstract Factory Tasarım Deseni(Design Pattern) Singleton deseni ile başladığım "design pattern" yazı dizisine "Abstract Factory" deseni ile devam ediyoruz.

Abstract Factory Tasarım Deseni(Design Pattern)

Bu yazıda "Creational" desenler grubunun en önemli ve en sık kullanılan deseni olan Abstract Factory(Soyut Fabrika) tasarım deseninin C# ile ne şekilde uygulandığını bir örnek üzerinden göstereceğim. İlk yazımda da bahsettiğim gibi "Creational" grubundaki desenler bir yada daha çok nesnenin çeşitli şekillerde oluşturulması ile ilgili desenlerdir. Bu kategoride ele alınan "Abstract Factory" ise birbirleriyle ilişkili yada birbirlerine bağlı olan nesnelerin oluşturulmasını en etkin bir şekilde çözmeyi hedefler. Bu hedefe ulaşmak için soyut sınıflardan(abstract class) veya arayüzlerden(interface) yoğun bir şekilde faydalanmaktadır.

"Abstract Factory" deseninin ana teması belirli sınıfların içerdiği ortak arayüzü soyut bir sınıf yada arayüz olarak tasarlamaktır. Factory method Design pattern using C# Abstract factory pattern. A factory is the location of a concrete class in the code at which objects are constructed.

Abstract factory pattern

The intent in employing the pattern is to insulate the creation of objects from their usage and to create families of related objects without having to depend on their concrete classes.[2] This allows for new derived types to be introduced with no change to the code that uses the base class. Definition[edit] The essence of the Abstract Factory Pattern is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes. ".[3] Usage[edit] This insulates client code from object creation by having clients ask a factory object to create an object of the desired abstract type and to return an abstract pointer to the object.[4] As the factory only returns an abstract pointer, the client code (that requested the object from the factory) does not know — and is not burdened by — the actual concrete type of the object that was just created.

Structure[edit] Design Patterns Made Simple at C# Online.NET (CSharp-Online.NET) Design Patterns for C# and Visual C# C# Design Patterns illustrates twenty-three classic Gang of Four (GoF) design patterns—as well as the patterns which grew from them—as they are used in the C# language on the .NET framework.

Design Patterns Made Simple at C# Online.NET (CSharp-Online.NET)

These design patterns are standards; but, their implementations can vary with the object-oriented language. In fact, sometimes the source code can look almost identical—as in the Java and C# languages—;yet, the pattern works in one and not the other. Accordingly, you cannot necessarily translate a Java pattern into a C# pattern and have it work and vice versa. Implementations can also vary within the C# language with each version having its own strengths and weaknesses. Adapter and Factory Pattern.