Php simple factory mode example php design mode Getting Started Tutorial
/**
- * Example
- *
- * A farm needs to sell fruit to the market
- * There are three types of fruits, apples, and grapes on the farm.
- * Our ideas: 1. fruits have multiple attributes, each of which is different. However, they share a common place. | growth, planting, receiving, and eating
- * 2. new fruits may be added in the future. we need to define an interface to standardize the methods they must implement.
- * 3. we need to get a fruit class. we need to get an instance of a fruit from a farmer to know how to grow, plant, receive, and eat it.
- */
/**
- * Virtual product interface
- * Define the implementation methods.
- */
Interface fruit {
/**
- * Growth
- */
- Public function grow ();
/**
- * Planting
- */
- Public function plant ();
/**
- * Gains
- */
- Public function harvest ();
/**
- * Eat
- */
- Public function eat ();
-
- }
/**
- * Define specific product categories for Apple
- * First, we need to implement the methods defined by the inherited interfaces.
- * Then define Apple's unique attributes and methods
- */
- Class apple implements fruit {
// Apple tree has an age
- Private $ treeAge;
// Apple Color
- Private $ color;
Public function grow (){
- Echo "grape grow ";
- }
Public function plant (){
- Echo "grape plant ";
- }
Public function harvest (){
- Echo "grape harvest ";
- }
Public function eat (){
- Echo "grape eat ";
- }
// The age of the apple tree
- Public function getTreeAge (){
- Return $ this-> treeAge;
- }
// Set the apple tree age
- Public function setTreeAge ($ age ){
- $ This-> treeAge = $ age;
- Return trie;
- }
}
/**
- * Define specific products
- * First, we need to implement the methods defined by the inherited interfaces.
- * Then define the unique attributes and methods of grapes.
- */
- Class grape implements fruit {
// Check whether grapes have seeds
- Private $ seedLess;
Public function grow (){
- Echo "apple grow ";
- }
Public function plant (){
- Echo "apple plant ";
- }
Public function harvest (){
- Echo "apple harvest ";
- }
Public function eat (){
- Echo "apple eat ";
- }
// Seed values
- Public function getSeedLess (){
- Return $ this-> seedLess;
- }
// Set seedless
- Public function setSeedLess ($ seed ){
- $ This-> seedLess = $ seed;
- Return true;
- }
- }
/**
- * The farmer class is used to obtain the instantiated fruit.
- *
- */
- Class farmer {
// Define a static factory method
- Public static function factory ($ fruitName ){
- Switch ($ fruitName ){
- Case 'apple ':
- Return new apple ();
- Break;
- Case 'grape ':
- Return new grape ();
- Break;
- Default:
- Throw new badFruitException ("Error no the fruit", 1 );
- Break;
- }
- }
- }
Class badFruitException extends Exception {
- Public $ msg;
- Public $ errType;
- Public function _ construct ($ msg = '', $ errType = 1 ){
- $ This-> msg = $ msg;
- $ This-> errType = $ errType;
- }
- }
/**
- * Method for obtaining fruit instantiation
- */
- Try {
- $ AppleInstance = farmer: factory ('apple ');
- Var_dump ($ appleInstance );
- } Catch (badFruitException $ err ){
- Echo $ err-> msg. "_______". $ err-> errType;
- }
-
|