Design Pattern-factory method (Demo)

Source: Internet
Author: User

Design Pattern-factory method (Demo)

 

Factory method

 

The factory method is the same as that of a simple factory. It is a creation-type design model. It solves the disadvantages of a simple factory's violation of open and closed.

 

Story

Host ...... People are tired of cooking, and the maids hold my thigh and say, "I have been shouting tired since I bought her house and didn't cook for a few times ...... Let's look at her poor eyes and look at the source. Let's recruit a few more maids, saying that she hasn't given the first one a name yet. It's called Yao. Later, she recruited yuan and Yue. 'and then let them cook one dish, cook steak remotely, cook red wine with Yuan, and cook pasta with joy. Each time I want to eat steak, I will say 'eat dinner with Yao ', if you drink red wine, you will say "eat" to your yuan, not to mention what to eat. because each of them only serves one dish, and they will want to eat anything else later, they will recruit a maids and only serve one dish. there are existing maids, so they will not complain about themselves, surrounded by beautiful women, drinking, eating meat, and being happy --

 

------ UML diagram

The old factory of the maids was replaced by the interface of the maids, and then a specific maids made food. The advantage is that if you add a food, you only need to add a factory of maids, and then add a food class. just do it. It does not violate the open and closed principle. Unlike a simple factory, we need to modify the factory class.

 

 

Code

 

Public Class Form1 'maids Interface Public Interface IMaid Function IMakeFood () As food' End Interface 'maids YAO Public Class YAO: Implements IMaid Public Function IMakeFood () as Food Implements IMaid. IMakeFood Return New Beef 'back to the Class of steak instantiation End Function End Class 'mahouyuan Public Class yuan: Implements IMaid Public Function IMakeFood () As Food Implements IMaid. IMakeFood Return New RedWine 'returns the class End Function of the red wine instantiation. End Class 'mahouyue Public Class yue: Implements IMaid Public Function IMakeFood () As Food Implements IMaid. IMakeFood Return New Noodles 'Return the End Function End Class Public MustInherit Class Food instantiated from pasta 'abstract Food Class Overridable Function Edible () as String 'edible method End Function End Class Public Class Beef: Inherits food' steak Class Overrides Function Edible () As String Dim str As String str = steak: I was eaten ~ (Too many rows )/~ La la Return str End Function End Class Public Class RedWine: Inherits Food 'Red wine Overrides Function Edible () As String Dim str As String str = red wine: I got drunk ~ (Too many rows )/~ La la Return str End Function End Class Public Class Noodles: Inherits Food 'Red wine Overrides Function Edible () As String Dim str As String str = pasta: I have eaten ~ (Too many rows )/~ La la Return str End Function End Class 'standalone button Private Sub button=click (sender As Object, e As EventArgs) Handles Button1.Click Dim YAO As IMaid = New yao' declares that the maids instantiate the remote Label1.Text = YAO. IMakeFood (). edible () 'Yao cook steak End Sub Private Sub Button2_Click (sender As Object, e As EventArgs) Handles Button2.Click Dim yuan As IMaid = New yuan 'declare the maids instantiate yuan Label1.Text = yuan. IMakeFood (). edible () 'yuan cook steak End Sub Private Sub Button3_Click (sender As Object, e As EventArgs) Handles Button3.Click Dim yue As IMaid = New yue 'declare the maids instantiate yue Label1.Text = yue. IMakeFood (). edible () 'yue cook steak End SubEnd Class


 



 

Actual Effect

 

 

Extension

Add a maids Sakura and a pizza and food.

 

 

Public Class pizz: Inherits Food 'Pizza Overrides Function Edible () As String Dim str As String str = pizza: I was eaten ~ (Too many rows )/~ La la Return str End Function End Class 'nvwa Sakura Public Class ying: Implements IMaid Public Function IMakeFood () As Food Implements IMaid. IMakeFood Return New pizz 'Return the Class endfunction End class' of the pizza instantiation. Add a Private Sub Button4_Click (sender As Object, e As EventArgs) to the single-host button) handles Button4.Click Dim ying As IMaid = New ying 'declares that the maids instantiate Sakura Label1.Text = ying. IMakeFood (). edible () 'sakura cook pizza End Sub


 

 

 

 

 

 

Benefits: Two Images

 

Before using the design pattern, many-to-many relationships, if you add a food, and you and I have to make changes

 

 

 

After using the design pattern, you only need to know the factory class, decouple and expand from the food class, then add a food class plus a factory class, and call the client.

 

 

 

 

 

 

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.