2010-11-22 7 views
2

最初の投稿、長時間の読者。C# - OOP - 正しい軌道に沿っていますか?

私は現在、私は私は、友人のための写真で動作する小さなプログラムを書いている(私はカプセル化までだと/プロパティを設定します)

を「ヘッドファーストのC#」を持つC#を学んでいます私のPictureControllerクラスで正しい行に向かっているのではないかと疑問に思っていますか?私の主な問題は、このクラスで多くのフォームアイテムを設定していることです。クラス内からフォームアイテムを参照し続けるのが難しいと感じています。私のコードを貼り付けています。何か間違っていると、私は最も感謝するでしょう:)

多くのありがとう!

PictureController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Windows.Forms; 

namespace PictureController 
{ 
    class PictureController 
    { 

     private int arrayPosition = 0; 
     private int numFiles = 0; 

     private string[,] arrayPictures; 

     public PictureBox myPictureBox; 
     public RadioButton myCopyButton; 
     public RadioButton myDeleteButton; 
     public TextBox mySource; 
     public ComboBox myDestinations; 

     private FolderBrowserDialog sourceFolder; 
     private FolderBrowserDialog destFolder; 

     public void InitialisePicture() 
     { 

      if (arrayPictures != null && arrayPictures.Length > 0) 
      { 
       myPictureBox.ImageLocation = arrayPictures[arrayPosition, 0]; 
      } 
      else 
      { 
       MessageBox.Show("The folder you have selected contains no pictures..."); 
       myPictureBox.ImageLocation = null; 
      } 
     } 

     public void NavigatePicture(int direction) 
     { 

      if (arrayPosition + direction >= 0 && arrayPosition + direction < numFiles) 
      { 
       arrayPosition += direction; 
       myPictureBox.ImageLocation = arrayPictures[arrayPosition, 0]; 


       myCopyButton.Checked = Convert.ToBoolean(arrayPictures[arrayPosition, 1]); 
       myDeleteButton.Checked = Convert.ToBoolean(arrayPictures[arrayPosition, 2]); 

      } 
     } 

     public void UpdateActions(bool copyChecked, bool deleteChecked) 
     { 
      if (arrayPictures != null) 
      { 
       arrayPictures[arrayPosition, 1] = copyChecked.ToString(); 
       arrayPictures[arrayPosition, 2] = deleteChecked.ToString(); 
      } 

     } 


     public void GetFiles() 
     { 

      sourceFolder = new FolderBrowserDialog(); 
      sourceFolder.ShowDialog(); 

      if (sourceFolder.SelectedPath != "") 
      { 
       string[] arrayTempFiles = Directory.GetFiles(sourceFolder.SelectedPath,"*.jpg"); 
       numFiles = arrayTempFiles.Length; 

       arrayPictures = new string[arrayTempFiles.Length,3]; 

       for (int i = 0; i < arrayTempFiles.Length; i++) 
       { 
        arrayPictures[i, 0] = arrayTempFiles[i]; 
        arrayPictures[i, 1] = "false"; 
        arrayPictures[i, 2] = "false"; 
       } 

       mySource.Text = sourceFolder.SelectedPath; 

       InitialisePicture(); 
      } 

     } 

     public void AddDestinationFolder() 
     { 
      destFolder = new FolderBrowserDialog(); 
      destFolder.ShowDialog(); 

      if (destFolder.SelectedPath != "") 
      { 
       myDestinations.Items.Add(destFolder.SelectedPath); 
      } 

     } 





    } 
} 

のForm1.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace PictureController 
{ 

    public partial class Form1 : Form 
    { 

     PictureController PicControl; 

     public Form1() 
     { 
      InitializeComponent(); 

      PicControl = new PictureController() { myPictureBox = pbPhoto, myCopyButton = rbMove, myDeleteButton = rbDelete, mySource = tbSource, myDestinations = cbDestination }; 
     } 

     private void btnPrev_Click(object sender, EventArgs e) 
     { 
      PicControl.NavigatePicture(-1); 
     } 

     private void btnNext_Click(object sender, EventArgs e) 
     { 
      PicControl.NavigatePicture(1); 
     } 

     private void rbMove_CheckedChanged(object sender, EventArgs e) 
     { 
      if (rbMove.Checked || rbDelete.Checked) 
      { 
       PicControl.UpdateActions(rbMove.Checked, rbDelete.Checked); 
      } 
     } 

     private void rbDelete_CheckedChanged(object sender, EventArgs e) 
     { 
      if (rbMove.Checked || rbDelete.Checked) 
      { 
       PicControl.UpdateActions(rbMove.Checked, rbDelete.Checked); 
      } 
     } 

     private void Form1_KeyDown(object sender, KeyEventArgs e) 
     { 
      switch (e.KeyCode) 
      { 
       case Keys.A: 
        PicControl.NavigatePicture(-1); 
        break; 
       case Keys.D: 
        PicControl.NavigatePicture(1); 
        break; 
       case Keys.W: 
        rbMove.Checked = true; 
        break; 
       case Keys.S: 
        rbDelete.Checked = true; 
        break; 
      } 


     } 

     private void btnGetFiles_Click(object sender, EventArgs e) 
     { 
      PicControl.GetFiles(); 
     } 

     private void btnProcess_Click(object sender, EventArgs e) 
     { 


     } 

     private void btnAddDest_Click(object sender, EventArgs e) 
     { 
      PicControl.AddDestinationFolder(); 
     } 

    } 
} 
+0

あなたはヘッドファーストのC#を楽しむ場合だけサイドノートとして、私は非常に頭最初のデザインパターンをお勧めします。私はHead First PMP、Design Patterns、OO分析とデザイン、ソフトウェア開発を読んだ。デザインパターンは実際にあなたのコーディング習慣を変えるでしょう(あなたがそれを読んだら、あまりにも基本的なので、OOをスキップしてください)。一般的に、頭の最初の本のいくつかは基本的ですが、彼らはかなり簡単に一週間でカバーするために読むことができるのでかなり良いです – i8abug

答えて

2

私はあなたのPictureControllerクラスでコントロールを使用する理由は表示されません。そこでは非形式のデータ型のみを使用し、Formに対話を処理し、PictureControllerクラスのイベントとメソッドを用意して対処する必要があります。

0

コントローラーを持っている点は何ですか?余分なロジックをフォームからカプセル化するだけですか?コントローラをテスト可能にする必要がありますか?ビジネスロジックの抽象モデルを使用しますか?最後に2つの質問に「はい」と答えた場合は、次のようにしてください。

a。 MVCパターン b。 MVPパターン

+0

ありがとう、私は見てよ:) – JuniorDeveloper1208

0

すべてが間違っています。

コントローラー(フォーム/ウィンドウのコントローラーの場合)は、サンプル中にフォームについて知っている必要があります。コントローラーについて知っているフォーム。

コントローラは、データを構築する方法を知っている/して、サンプルのコントローラにpictureBox'esなど

をへの参照を持っており、制御コードからコントローラを「抽出」する必要がないときながら、何らかの形でイベントを処理する必要がありますコントロールの "データ/モデル"が何であるか(型に囲まれていない)が確立されていません。

データがどこにあるかはっきりしない:パスまたは画像の収集。

+0

ありがとう、私は分からない私はあなたが「制御コードからコントローラを抽出する」という意味を理解していますか? – JuniorDeveloper1208

1

私の意見では良いスタートです。

あなたが「間違った」何かをやっている場合、それはあなたが正しいと思う何依存しているため、すべてのプログラマは、彼/彼女自身のスタイルとベストプラクティスのセットを持っており、限り、コードが動作し、効率的であるとして、それはだ伝えるのは難しいです"右"。ローマに至る道はたくさんあります。 :)あなたが個人的な意見やアドバイスを求める場合

とにかく、私はロジックでは二つの主要な変更を加えることになります。

  1. (ご希望の場合はシングルトン)をコントローラが
  2. ないで静的クラスで持っています直接フォームコントロールを渡すか使用してください。代わりに、いくつかのInitializeメソッドでフォームのインスタンスを静的クラスに渡し、そのインスタンスを使用して、コントロールで直接動作しているパブリックメソッドを呼び出します。

2番目の変更のための例:

public static void NavigatePicture(int direction) 
{ 
    if (arrayPosition + direction >= 0 && arrayPosition + direction < numFiles) 
    { 
     arrayPosition += direction; 
     _formInstance.SetPictureLocation(arrayPictures[arrayPosition, 0]); 

     _formInstance.SetCopyStatus(Convert.ToBoolean(arrayPictures[arrayPosition, 1])); 
     _formInstance.SetDeleteStatus(Convert.ToBoolean(arrayPictures[arrayPosition, 2])); 
    } 
} 


//...and in the form: 
public SetPictureLocation(sLocation) 
{ 
    myPictureBox.ImageLocation = sLocation; 
} 
+0

いいえ、私はちょうど場所にはいを設定するフォームのPicturebox名を使用するのだろうか? – JuniorDeveloper1208

+0

@ t84:はい、サンプルコードもあります。 –

関連する問題