using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProtectedSınıflar
{
//View
class Program
{
static void Main(string[] args)
{
IHesap hesaplar = new Hesap();
Ucgen ucg = new DikDortgen();
DikdortgenAlanHesapla(hesaplar, ucg, 200);
Ucgen ucg2 = new Prizma();
PrizmaHacimHesapla(hesaplar, ucg2, 200, 10);
}
static void DikdortgenAlanHesapla(IHesap hesaplar, Ucgen ucg, double kenar)
{
double alan = hesaplar.AlanHesapControler(ucg, kenar);
Console.WriteLine("Karenin Alanı {0}", alan.ToString());
}
static void PrizmaHacimHesapla(IHesap hesaplar, Ucgen ucg, double kenar, double yukseklik)
{
double hacim = hesaplar.HacimHesapController(ucg, kenar, yukseklik);
Console.WriteLine("Prizmanın Hacmi{0}", hacim.ToString());
}
}
//Controller
interface IHesap
{
double AlanHesapControler(Ucgen ucg, double kenar);
double HacimHesapController(Ucgen ucg, double kenar, double yukseklik);
}
class Hesap : IHesap
{
public double AlanHesapControler(Ucgen ucg, double kenar)
{
ucg = new DikDortgen();
return ((DikDortgen)ucg).Alan(kenar);
}
public double HacimHesapController(Ucgen ucg, double kenar, double yukseklik)
{
ucg = new Prizma();
return ((Prizma)ucg).Hacim(kenar, yukseklik);
}
}
//Model
class Ucgen
{
protected double kenar;
protected double Alan()
{
return kenar * kenar / 2;
}
}
class DikDortgen : Ucgen
{
public double Alan(double kenari)
{
base.kenar = kenari;
return base.Alan()*2;
}
}
class Prizma : Ucgen
{
public double Hacim(double kenari, double yukseklik)
{
base.kenar = kenari;
return base.Alan() * 2 * yukseklik;
}
}
}
Refactorings
No refactoring yet !
Ants
May 30, 2010, May 30, 2010 22:26, permalink
The problem is that interface to the model was setup incorrectly. You'll want your Ucgen class to actually be an abstract Cokgen class with an abstract Alan() method. (Please forgive me, I don't speak Turkish and I'm just making my best guess based on what I can find on Google for translations.)
The code below was a quick first pass refactoring. Please note that I broke a little bit of the rules for MVC because the View below is instantiating the Model when it is typically the Controller that does this. Considering, though, that the Main() method is acting like a Controller, the MVC rules have been broken anyway...
using System;
namespace ProtectedSınıflar
{
//View
class Program
{
static void Main(string[] args)
{
IHesap hesaplar = new Hesap();
AlanHesapla(hesaplar, new DikDortgen(200));
AlanHesapla(hesaplar, new Prizma(200, 10));
}
static void AlanHesapla(IHesap hesaplar, Içokgen çokgen)
{
Console.WriteLine("Alanı {0}", hesaplar.AlanHesapControler(çokgen));
}
}
//Controller
interface IHesap
{
double AlanHesapControler(Içokgen çokgen);
}
class Hesap : IHesap
{
public double AlanHesapControler(Içokgen çokgen)
{
return çokgen.Alan();
}
}
//Model
interface Içokgen
{
double Alan();
}
class Ucgen : Içokgen
{
double kenar;
public Ucgen(double kenar)
{
this.kenar = kenar;
}
public double Alan()
{
return kenar * kenar / 2;
}
}
class DikDortgen : Içokgen
{
double kenar;
public DikDortgen(double kenar)
{
this.kenar = kenar;
}
public double Alan()
{
return kenar * kenar;
}
}
class Prizma : Içokgen
{
double kenar;
double yukseklik;
public Prizma(double kenar, double yukseklik)
{
this.kenar = kenar;
this.yukseklik = yukseklik;
}
public double Alan()
{
return kenar * kenar * yukseklik;
}
}
}
i try to calculate triangle area : kenar*kenar/2 also Square : 2*triangleArea and Prizm : Square*h but i need refactor mycodes. i need very simple SRP structure please don't change protected structure. We can use MVC design pattern. i think that we can refactor to more flexible and readable codes:)