7 Mart 2018

C# Abstract Kullanımı İçin Örnek

 

using System;
using System.Linq;

namespace AbstractionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            TextFile tf = new TextFile("test.txt");
            Console.WriteLine(tf.IsExist());
            tf.Write("test");
            FileFactory.ShowContent(tf);
            FileFactory.WriteFile(tf, "degistirilen icerik");
            Console.WriteLine(FileFactory.ReadFile(tf));
            


            BinaryFile bf = new BinaryFile("test_binary.dat");
            Console.WriteLine(bf.IsExist());
            var bytes = new byte[] { 0, 1, 2, 3, 4, 5 };
            bf.Write(bytes);
            Console.WriteLine(bf.Length);
            FileFactory.ShowContent(bf);
            byte[] b = FileFactory.ReadFile(bf);
            b = b.Select(q => (byte)(q + 1)).ToArray();
            FileFactory.WriteFile(bf, b);
            FileFactory.ShowContent(bf);

        }
    }

    public class FileFactory
    {
        //File class ile çalışıyoruz. Artık bizim için hangi dosya türü olduğunun bir önemi yok
        //Biz artık dosya işlemleri için türünden bağımsız olarak ortak metod yazabiliyoruz.
        public static void ShowContent<T>(File<T> file)
        {
            Console.WriteLine(file);
        }

        public static void WriteFile<T>(File<T> file, T content)
        {
            file.Write(content);
        }

        public static T ReadFile<T>(File<T> file)
        {
            return file.Read();
        }
    }

    public abstract class File<T>
    {
        protected string Path { get; set; }
        private Type Type;
        public abstract T Read();
        public abstract void Write(T content);
        public bool IsExist() => System.IO.File.Exists(Path);
        public File(string path, Type type)
        {
            Path = path;
            Type = type;
        }
    }

    public class TextFile : File<string>
    {

        public override string Read()
        {
            return System.IO.File.ReadAllText(Path);
        }

        public override void Write(string content)
        {
            System.IO.File.WriteAllText(Path, content);
        }
        //Text için özel Append Metodu oluşturduk.
        public void Append(string content)
        {
            System.IO.File.AppendAllText(Path, content);
        }

        public TextFile(string path) : base(path, typeof(string))
        {

        }
        public override string ToString()
        {
            return Read();
        }
    }

    public class BinaryFile : File<byte[]>
    {
        //Byte için ekstra özellik.
        public int Length => Read().Length;

        public override byte[] Read()
        {
            return System.IO.File.ReadAllBytes(Path);
        }

        public override void Write(byte[] bytes)
        {
            System.IO.File.WriteAllBytes(Path, bytes);
        }
        public BinaryFile(string path) : base(path, typeof(byte[]))
        {

        }

        public override string ToString()
        {
            return string.Join(" ", Read());
        }

    }
}