Factory Method Pattern
Derivatives of the template model?
The Factory that obtains the generated object instance in the Template Method Pattern architecture is Factory Method Pattern.
The factory method mode specifies the object creation method in the parent class, but does not go deep into the specific class name. All the specific complete content is placed in the subclass. Based on this principle, we can roughly divide it into two aspects: the outline (framework) of the generated object instance and the actual production object instance.
Scenario Simulation
Taking a factory as a prototype to work in the factory as a prototype, the worker must first register information and enter the registered information to create a work card. The worker must use the work card to punch in every day to start a day's work. As an example, the UML diagram for programming is as follows:
Program Implementation
Abstract class Product definition abstract method create () indicates the creation of employment card information and use () indicates that the user uses card
Abstract class: the abstract class of the Factory implementation method create. here is why we mentioned above is the template method mode derivatives. And define the Abstract METHODS createProduct () and the abstract method registerProduct () are handled by the subclass.
The class IDcard inherits from the Product implementation methods use and create
The class IDCardFactory inherits from the Factory implementation methods createProduct and registerProduct.
Product abstract class
Public abstract class Product {
Public abstract String create ();
Public abstract String use ();
}
Factory abstract class
Public abstract class Factory {
Public final Product create (String owner ){
Product p = createProduct (owner );
RegisterProduct (p );
Return p;
}
Protected abstract Product createProduct (String owner );
Protected abstract void registerProduct (Product product );
}
IDCard class
Public class IDCard extends Product {
Private String owner;
Public IDCard (String owner ){
// TODO Auto-generated constructor stub
This. owner = owner;
}
@ Override
Public String use (){
// TODO Auto-generated method stub
Return "use" + owner + "card ";
}
Public String getOwner (){
Return owner;
}
@ Override
Public String create (){
// TODO Auto-generated method stub
Return "CREATE" + owner + "card ";
}
}
IDCardFactory class
Public class IDCardFactory extends Factory {
Private Vector <String> owners = new Vector <String> ();
@ Override
Protected Product createProduct (String owner ){
// TODO Auto-generated method stub
Return new IDCard (owner );
}
@ Override
Protected void registerProduct (Product product ){
// TODO Auto-generated method stub
Owners. add (IDCard) product). getOwner ());
}
Public Vector <String> getOwners (){
Return owners;
}
}
The interface code implements FatoryMethodActivity:
Public class FatoryMethodActivity extends Activity implements OnClickListener {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
(Button) findViewById (R. id. Button01). setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Factory factory = new IDCardFactory ();
Product card1 = factory. create ("terry ");
Product card2 = factory. create ("paul ");
Product card3 = factory. create ("jim ");
(EditText) findViewById (R. id. EditText01). setText (card1.create () + ","
+ Card1.use () + "" + card2.create () + "," + card2.use () + ""
+ Card3.create () + "," + card3.use ());
}
}
Final effect: