享元模式

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介:   /* * 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 myMemon
 
/*
 * 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

 

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
1月前
|
设计模式 Java
Java设计模式-享元模式(12)
Java设计模式-享元模式(12)
|
2月前
|
设计模式 存储 数据库连接
何时使用享元模式
【8月更文挑战第22天】
20 0
|
5月前
|
Java 数据库
享元模式~
享元模式~
|
存储 设计模式 缓存
2023-6-28-第十式享元模式
2023-6-28-第十式享元模式
83 0
|
存储 容器
享元模式(Flyweight)
享元模式(Flyweight)
57 0
|
存储 设计模式 Java
浅谈JAVA设计模式之——享元模式(Flyweight)
运用共享技术有效地支持大量细粒度的对象。
135 0
浅谈JAVA设计模式之——享元模式(Flyweight)
|
设计模式 缓存 前端开发
关于享元模式我所知道的
关于享元模式我所知道的
60 0
|
存储 缓存 Java
结构型模式-享元模式
结构型模式-享元模式
119 0
|
设计模式 缓存 Java
我学会了,享元模式
享元模式属于结构型模式,这个类型的设计模式总结出了 类、对象组合后的经典结构,将类、对象的结构和使用解耦了,花式的去借用对象。
158 0
我学会了,享元模式
|
设计模式 缓存 Java
Java设计模式 ->享元模式
Java设计模式 ->享元模式
102 0