Simple factory with Design Patterns

Source: Internet
Author: User

I bought the book "java and mode" and recorded some information in the course of learning. It is purely a personal learning summary and does not guarantee correctness.

I am very grateful to my predecessors for reading this article and correcting the mistakes in this article.

My recent job is to develop the B/S module of the mobile Integrated Alarm resource management system.
Therefore, the code in this section is related to some related services in the system.

First, introduce the B/S module of the mobile Integrated Alarm resource management system.

The system involves four professional modules: traffic, transmission, dynamic environment, and data. Each major processes relevant professional information. In addition, at this stage, only the first three professional data are available.

According to the simple factory method, you need to first design an interface or abstract class to constrain the processing methods of each professional data.

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data;

Namespace simple factory
{
Public interface ISpeciality <T> where T: IDomain
{

# Add a data record to region
/// <Summary>
/// Add a new data entry
/// </Summary>
/// <Param name = "t"> Data Object </param>
/// <Returns> </returns>
Bool InserOneInfo (T t );
# Endregion

# Region get a message
/// <Summary>
/// Get a message
/// </Summary>
/// <Param name = "strIntId"> </param>
/// <Returns> </returns>
DataTable GetOneInfo (string strIntId );
# Endregion

# Region get all information
/// <Summary>
/// Obtain all information
/// </Summary>
/// <Returns> </returns>
DataTable GetAllInfo ();
# Endregion

# Region Query Information by conditions
/// <Summary>
/// Query information by conditions
/// </Summary>
/// <Param name = "strCtiyId"> city id </param>
/// <Param name = "strBusiType"> professional type 1: traffic 2: transmission 3: dynamic cycle 4: Data </param>
/// <Returns> </returns>
DataTable GetInfoByQuery (string strCtiyId, string strBusiType );
# Endregion

# Region Query Information by condition and export it
/// <Summary>
/// Query information by condition and export it
/// </Summary>
/// <Param name = "strCtiyId"> city id </param>
/// <Param name = "strBusiType"> professional type 1: traffic 2: transmission 3: dynamic cycle 4: Data </param>
/// <Returns> </returns>
DataTable QueryInfoForDowland (string strCtiyId, string strBusiType );
# Endregion

# Region delete a message
/// <Summary>
/// Delete a message
/// </Summary>
/// <Param name = "strIntId"> </param>
/// <Returns> </returns>
Bool DelOneInfo (string strIntId );
# Endregion

# Region modify a data record
/// <Summary>
/// Modify a data record
/// </Summary>
/// <Param name = "t"> Data Object </param>
/// <Returns> </returns>
Bool UpDateOneInfo (T t );
# Endregion
}

}

The above is an interface class, and the following is a specialized information processing class.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data;

Namespace simple factory
{
Public class BtsSite <BtsSiteManagerDomain>: ISpeciality <BtsSiteManagerDomain> where BtsSiteManagerDomain: IDomain
{
Public BtsSite ()
{

}
# Add a data record to region
/// <Summary>
/// Add a new data entry
/// </Summary>
/// <Param name = "t"> Data Object </param>
/// <Returns> </returns>
Public bool InserOneInfo (BtsSiteManagerDomain btsSiteManagerDomain)
{

BtsSiteManagerDomain. Intid = "111 ";

Bool bRet = false;
Return bRet;
}
# Endregion

# Region get a message
/// <Summary>
/// Get a message
/// </Summary>
/// <Param name = "strIntId"> </param>
/// <Returns> </returns>
Public DataTable GetOneInfo (string strIntId)
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region get all information
/// <Summary>
/// Obtain all information
/// </Summary>
/// <Returns> </returns>
Public DataTable GetAllInfo ()
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region Query Information by conditions
/// <Summary>
/// Query information by conditions
/// </Summary>
/// <Param name = "strCtiyId"> city id </param>
/// <Param name = "strBusiType"> professional type 1: traffic 2: transmission 3: dynamic cycle 4: Data </param>
/// <Returns> </returns>
Public DataTable GetInfoByQuery (string strCtiyId, string strBusiType)
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region Query Information by condition and export it
/// <Summary>
/// Query information by condition and export it
/// </Summary>
/// <Param name = "strCtiyId"> city id </param>
/// <Param name = "strBusiType"> professional type 1: traffic 2: transmission 3: dynamic cycle 4: Data </param>
/// <Returns> </returns>
Public DataTable QueryInfoForDowland (string strCtiyId, string strBusiType)
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region delete a message
/// <Summary>
/// Delete a message
/// </Summary>
/// <Param name = "strIntId"> </param>
/// <Returns> </returns>
Public bool DelOneInfo (string strIntId)
{
Bool bRet = false;
Return bRet;
}

# Endregion

# Region modify a data record
/// <Summary>
/// Modify a data record
/// </Summary>
/// <Param name = "t"> Data Object </param>
/// <Returns> </returns>
Public bool UpDateOneInfo (BtsSiteManagerDomain btsSiteManagerDomain)
{
Bool bRet = false;
Return bRet;
}
# Endregion

}
}
You can then define other professional information processing classes.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Data;

Namespace simple factory
{
Class Asite <AsiteDomain>: ISpeciality <AsiteDomain> where AsiteDomain: IDomain
{
Public Asite ()
{

}
# Add a data record to region
/// <Summary>
/// Add a new data entry
/// </Summary>
/// <Param name = "t"> Data Object </param>
/// <Returns> </returns>
Public bool InserOneInfo (AsiteDomain asiteDomain)
{

AsiteDomain. intids = "111 ";

Bool bRet = false;
Return bRet;
}
# Endregion

# Region get a message
/// <Summary>
/// Get a message
/// </Summary>
/// <Param name = "strIntId"> </param>
/// <Returns> </returns>
Public DataTable GetOneInfo (string strIntId)
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region get all information
/// <Summary>
/// Obtain all information
/// </Summary>
/// <Returns> </returns>
Public DataTable GetAllInfo ()
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region Query Information by conditions
/// <Summary>
/// Query information by conditions
/// </Summary>
/// <Param name = "strCtiyId"> city id </param>
/// <Param name = "strBusiType"> professional type 1: traffic 2: transmission 3: dynamic cycle 4: Data </param>
/// <Returns> </returns>
Public DataTable GetInfoByQuery (string strCtiyId, string strBusiType)
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region Query Information by condition and export it
/// <Summary>
/// Query information by condition and export it
/// </Summary>
/// <Param name = "strCtiyId"> city id </param>
/// <Param name = "strBusiType"> professional type 1: traffic 2: transmission 3: dynamic cycle 4: Data </param>
/// <Returns> </returns>
Public DataTable QueryInfoForDowland (string strCtiyId, string strBusiType)
{
DataTable tempTable = new DataTable ();
Return tempTable;
}
# Endregion

# Region delete a message
/// <Summary>
/// Delete a message
/// </Summary>
/// <Param name = "strIntId"> </param>
/// <Returns> </returns>
Public bool DelOneInfo (string strIntId)
{
Bool bRet = false;
Return bRet;
}

# Endregion

# Region modify a data record
/// <Summary>
/// Modify a data record
/// </Summary>
/// <Param name = "t"> Data Object </param>
/// <Returns> </returns>
Public bool UpDateOneInfo (AsiteDomain asiteDomain)
{
Bool bRet = false;
Return bRet;
}
# Endregion
}
}

Finally, a class of real-column objects as needed.
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public sealed class Createor <T> where T: IDomain
{
Public static ISpeciality <T> CreaterSpeciality (string strKey)
{
Switch (strKey)
{
Case "BtsSite ":
Return new BtsSite <T> ();
Break;
Case "Asite ":
Return new Asite <T> ();
Break;
Default:
Return new BtsSite <T> ();
Break;
}
}

}
}
In this mode, the real columns of different classes can be returned based on different input parameters. However, the instances of returned classes are defined in the factory class. For later instances of new classes, cannot meet.
Just as there is no data major in this system at this stage, we will modify the factory class when we add a data major later. According to the Open _ closed principle, the modification is closed and the extension is open. Therefore, we cannot modify the factory class. Then how can we meet the new demand in the future?
Can we design the Createor class in this way?
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public interface ICreator <T> where T: IDomain
{
ISpeciality <T> CreaterSpeciality (string strKey );
}
}

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public class Createor <T>: ICreator <T> where T: IDomain
{
Public virtual ISpeciality <T> CreaterSpeciality (string strKey)
{
Switch (strKey)
{
Case "BtsSite ":
Return new BtsSite <T> ();
Break;
Default:
Return new BtsSite <T> ();
Break;
}
}

}
} And, if there are new requirements in the future, we only need to write a class that inherits from the Createor class.
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public class AddCreator <T>: Createor <T> where T: IDomain
{
Public override ISpeciality <T> CreaterSpeciality (string strKey)
{
Switch (strKey)
{
Case "Asite ":
Return new Asite <T> ();
Break;
Default:
Return new Asite <T> ();
Break;
}
}
}
}
Then, use the following method when using it:
AsiteDomain asiteDomain = new AsiteDomain ();
ICreator <IDomain> creator = new Createor <IDomain> ();
ISpeciality <IDomain> speciality = creator. CreaterSpeciality ("BtsSite ");
Speciality. InserOneInfo (asiteDomain );

Instead of using:
AsiteDomain asiteDomain = new AsiteDomain ();
ISpeciality <IDomain> speciality = Createor <IDomain>. CreaterSpeciality ("BtsSite ");
Speciality. InserOneInfo (asiteDomain );

According to this modification, it is obvious that the new function can be added without violating the principle of "off-closed.
However, according to this modification, this mode is close to the factory method mode.
See the following section: Factory method mode.

Finally, complete the code;
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public interface IDomain
{
String Intid
{
Get;
Set;
}
}
}

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public class BtsSiteManagerDomain: IDomain
{
Private string mIntId;
Public string Intid
{
Get
{
Return mIntId;
}

Set
{
MIntId = value;
}
}

}
}

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace simple factory
{
Public class AsiteDomain: IDomain
{
Private string mIntId;
Public string Intid
{
Get
{
Return mIntId;
}

Set
{
MIntId = value;
}
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.