享元模式

 
/*
 * User: Administrator
 * Date: 2007-7-3 Time: 14:16
 
*/

using System;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.Remoting;
using System.Collections;

namespace myMemonto
{
    
/// <summary>
    
/// Description of MyShare.
    
/// </summary>
    public class MyShare
    {
        
public MyShare()
        {
        }
    }
    
public class ApartmentType{
        
public string TypeCode;
        
public string TypeName;
        
public ApartmentType(){

        }
    }
    
public class Apartment{
        
protected string name;
        
protected string ap_type;
        
public string roomNumber;
        
public double area;

        
private Form editform;
        
public Apartment()
        {
            editform
=FlyWightFactory.GetEditForm("myMemonto.EditApartment");
            
//editform=new EditApartment();
        }
        
protected Apartment myType;

        
public string Name{
            
get{return name;}
            
set{name=value;}
        }

        
public string ApartmentType{
            
get{return ap_type;}
            
set{ap_type=value;}
        }

        
public virtual bool Edit(){
            EditApartment f
=(EditApartment)editform;
            f.txtName.Text
=name;
            f.txtArea.Text
=area.ToString();
            f.txtType.Text
=ap_type;
            f.txtRoomNumber.Text
=roomNumber;


            
if(f.ShowDialog()== DialogResult.OK){

                name
=f.txtName.Text;
                area
=double.Parse(f.txtArea.Text);
                ap_type
=f.txtType.Text;
                roomNumber
=f.txtRoomNumber.Text;

                MessageBox.Show(
"saved");
                
return true;
            }
            
else
            {
                MessageBox.Show(
"cancelled");
                
return false;
            }

        }

        
public string RoomNumber{
            
get{return roomNumber;}
            
set{roomNumber=value;}
        }
        
public double Area{
            
get{return area;}
            
set{area=value;}
        }
        
public override string ToString()
        {
            
return name;
        }
    }


    
public class TestaEditForm
    {
        
private Form editform;
        
public TestaEditForm(){
            editform
=FlyWightFactory.GetEditForm("myMemonto.MyTest");
        }

        
public bool Edit()
        {
            MyTest a
=(myMemonto.MyTest)editform;
            
if(a.ShowDialog()== DialogResult.OK)
            {
                MessageBox.Show(
"Saved");
                
return true;
            }
            
else
            {
                MessageBox.Show(
"canceled");
                
return false;
            }
        }
        
public Form EditForm{
            
get{return editform;}
            
set{editform=value;}
        }

    }

    
public class TypeFacotry{
        
public TypeFacotry(){}
        
private static ArrayList types=new ArrayList();

        
public static ApartmentType GetApartmentType(string typecode){
            
foreach(ApartmentType at in types){
                
if(at.TypeCode==typecode) return at;
            }

            ApartmentType nat
=new ApartmentType();
            nat.TypeCode
=typecode;
            nat.TypeCode
=typecode;

            types.Add(nat);
            
return nat;
        }
    }

    
public class FlyWightFactory{
        
private static ArrayList editforms=new ArrayList();

        
private FlyWightFactory(){}

        
public static Form GetEditForm(string formType)
        {
            Form editform;
            
foreach(Form ef in editforms)
                
if(ef.GetType().Name==formType) return ef;

            ObjectHandle obj
=Activator.CreateInstance(null,formType);
            editform
=(Form)obj.Unwrap();

            editforms.Add(editform);

            
return editform;
        }
    }


}
 这是写在form里的语句
void Button1Click(object sender, EventArgs e)
{
    Apartment newa
=new Apartment();
    
if(newa.Edit())this.listBox1.Items.Add(newa);
}

void Button2Click(object sender, EventArgs e)
{
    Apartment a
=(Apartment) this.listBox1.SelectedItem;
    
if(a!=null) a.Edit();
}

void Button4Click(object sender, EventArgs e)
{
    TestaEditForm b
=new TestaEditForm();
    
if(b.Edit()) this.listBox1.Items.Add(b);
}

editform

享元模式

shareform

 享元模式

上一篇:访问者模式


下一篇:editplus删除空行