Create a design pattern - factory pattern

Simple factory pattern:

../_images/SimpleFactory.jpg


  Simple, only a factory class, to make the pass by the factory object reference plant species to determine which class object is created,
  typically by if, switch statement, the great disadvantage, if we add a new class to be created
  it is necessary to change the source code of the plant can make the plant supports the creation of this new class of objects.
  shi'yong
Package main
Import "FMT"

type interface Product {
Print ()
SET (NUM int)
}


type the Product1 is struct {
NUM int
}
FUNC (the Product1 is IT *) Print () {
fmt.Println (it.num)
}
FUNC ( * the Product1 is IT) SET (NUM int) {
it.num NUM =
fmt.Println (it.num)
}


type Product2 is struct {
NUM int
}
FUNC (IT * Product2 is) Print () {
fmt.Println (it.num)
}
func (it *Product2)set(num int){
it.num = num
fmt.Println(it.num)
}



type Factory struct {

}
func (it *Factory)createFactory(str string) (product Product){
switch str {
case "Product1":
product = new(Product1)
case "Product2":
product = new(Product2)
default:

}
return
}



func main(){
factory := new(Factory)
p1 := factory.createFactory("Product1")
p2 := factory.createFactory("Product2")
p1.set(1)
p2.set(2)
p1.print()
p2.print()

}

Factory Method pattern:
../_images/FactoryMethod.jpg

   First of all we want to achieve is created by this factory pattern to honor the class. We need to have a corresponding class factory. It presents one relationship. And all of the corresponding class factory class implements
an abstract factory class, which is a model factory, other factories are copies of this plant, on the basis of this factory on processing processing (multi-state)
For example, I have a Product1 and Product2 must create objects in this factory mode. Has an interface Factory, two classes corresponding to a class factory to achieve it, Product1Factory
and Product2Factory, these two plants by a method (to write) is to return an instance of the object.
    package main

import "fmt"

type Product interface {
print()
set(num int)
}


type Product1 struct {
num int
}
func (it *Product1)print(){
fmt.Println(it.num)
}
func (it *Product1)set(num int){
it.num = num
fmt.Println(it.num)
}


type Product2 struct{
num int
}
func (it *Product2)print(){
fmt.Println(it.num)
}
func (it *Product2)set(num int){
it.num = num
fmt.Println(it.num)
}




type factory interface {
createProduct()
}


type Product1Factory struct {
}
func(it *Product1Factory)createFactory()(product Product){
product = new(Product1)
return
}


type Product2Factory struct {
}
func(it *Product2Factory)createFactory()(product Product){
product = new(Product2)
return
}

func main(){
p1 := (new(Product1Factory).createFactory())
p2 := (new(Product2Factory).createFactory())
p1.set(1)
p2.set(2)
p1.print()
p2.print()

}

抽象工厂模式:
../_images/AbatractFactory.jpg

  怎么说呢,这个工厂模式,实际上是为了在一个工厂内创建多个类型的实例,比如说我们有A,A1,A2
B,B1,B2 .A,B是两个厂家,但是A1,B1是同一类商品。我们要仅仅创建一个厂家的商品。这时候就需要将不同类型的商品的创建方法放在同一个工厂中
打个最实在的比喻,小米的工厂生产小米的手机和电脑,华为的也是。他们要分开生产自个儿的,对吧。就是这,重点是将一种产品的多个不同实例分开创建
package main

import (
"fmt"
)


type product1 interface {
print()
set (num int)
}

type product1A struct {
num int
}
func(it *product1A)print(){
fmt.Println(it.num)
}
func(it *product1A)set(num int){
it.num = num
}

type product1B struct {
num int
}
func(it *product1B)print(){
fmt.Println(it.num)
}
func(it *product1B)set(num int){
it.num = num
}



type product2 interface {
print()
set(num int)
}

type product2A struct {
num int
}
func(it *product2A)print(){
fmt.Println(it.num)
}
func(it *product2A)set(num int){
it.num = num
fmt.Println(it.num)
}

type product2B struct {
num int
}
func(it *product2B)print(){
fmt.Println(it.num)
}
func(it *product2B)set(num int){
it.num = num
fmt.Println(it.num)
}



type factory interface {
createProduct1() product1
createPRoduct2() product2
}

type AFactory struct {
}
func(it *AFactory)createProduct1() product1{
return new(product1A)
}
func(it *AFactory)createProduct2()product2{
return new(product2A)
}

type BFactory struct {}
func(it *BFactory)createProduct1() product1{
return new(product1B)
}
func(it *BFactory)createProduct2() product2{
return new(product2B)
}

func main(){
AP1 := new(AFactory).createProduct1()
AP2 := new(AFactory).createProduct2()
BP1 := new(BFactory).createProduct1()
BP2 := new(BFactory).createProduct2()
AP1.set(1)
AP2.set(2)
BP1.set(3)
BP2.set(4)
AP1.print()
AP2.print()
BP1.print()
BP2.print()
}




Guess you like

Origin www.cnblogs.com/mcmx/p/11327212.html