Design Pattern Series 1--staticfactory (static Factory), Abstractfactory (abstract Factory) _ Design mode

Source: Internet
Author: User

This article is derived from
Code Big Wet
Code Big Wet

Please pay attention to this series of continuous updates. 1 Static Factory

Static Factory Introduction:

Static Factory mode can implement the principle of interface encapsulation isolation. Static Factory mode can be used when the client knows only the interface and does not know the implementation.

SOURCE Please click on me

Role:

Product: The interface that the client needs to use. 

the concrete realization of concreteproduct:product.

Factory: A factory class that has a static method that returns a Concreteproduct instance.

Client: Only product is known. But do not know concrete implementation.

Uml:

Code

Package design mode. staticfactary;
 * * * August 26, 2016    morning 10:15:22
 * @Author pin-wang
 *@e-mail
1228935432@qq.com
/Public Interface Product {public
    void operation ();

}
Package design mode. staticfactary;
 * * * August 26, 2016    morning 10:15:00
 * @Author pin-wang
 *@e-mail
1228935432@qq.com
/Public Class Concreteproduct implements product{

    @Override public
    void operation () {
        System.out.println (" I am the implementation method in a specific product ");
    }

Package design mode. staticfactary;
 * * * * August 26, 2016    morning 10:47:01
 * @Author pin-wang
 *@e-mail 1228935432@qq.com *
* public class Factory {public
    static Product getproduct () {return
        new concreteproduct ();
    }
}
Package design mode. staticfactary;
 * * * August 26, 2016    morning 10:48:05
 * @Author pin-wang
 *@e-mail
1228935432@qq.com
/Public Class Client {public

    static void Main (string[] args) {
        Product product=factory.getproduct ();
        Product.operation ();
    }

2 Abstractfactory (abstract Factory)

Abstractfactory Introduction:

Abstractfactory is the creation of interfaces for a series of related or interdependent classes, and the client does not have to focus on their implementation. Quite as a abstractfactory implementation creates a product family.

Abstractfactory role:

Abstractproduct: Abstract Product class.
concreteproduct: The specific product realization.
abstractfactory: Abstract Factory.
Concretefactory: Concrete factory implementation.

Uml:

SOURCE Click on Me

Scene: Choose different schemes to install, the main is to let the CPU and motherboard matching.

Code

Abstractproduct

Package design mode. abstractfactory;
 * * * * September 2, 2016 morning 10:18:46
 * @Author pin-wang *@e-mail 1228935432@qq.com * * Public
interface CPUAPI {
    public  void Calculate ();

}

Abstractproduct

Package design mode. abstractfactory;
 * * * * September 2, 2016 morning 10:18:46
 * @Author pin-wang *@e-mail 1228935432@qq.com * * Public
interface MAINBOARDAPI {
    void install ();
}

Concreteproduct

Package design mode. abstractfactory;
 * * * * September 2, 2016 morning 10:20:38
 * @Author pin-wang *@e-mail 1228935432@qq.com * * Public
class INTELLCPU implements cpuapi{
    Integer cpupins;
    Public intellcpu (Integer pins) {
        cpupins=pins;
    }
    @Override public
    Void Calculate () {
        System.out.println ("Intell CPU is computing ..." + "pins:" +cpupins);
    }


Concreteproduct

Package design mode. abstractfactory;
*
 * September 2, 2016 morning 10:20:38
 * @Author pin-wang
 *@e-mail 1228935432@qq.com * * Public

class Amdcpu Implements cpuapi{
    Integer cpupins;
    Public amdcpu (Integer pins) {
        cpupins=pins;
    }
    @Override public
    Void Calculate () {
        System.out.println ("AMD CPU is computing ..." + "pins:" +cpupins);
    }


Package design mode. abstractfactory;
 * * * September 2, 2016 morning 10:24:25
 * @Author pin-wang
 *@e-mail 1228935432@qq.com/

concreteproduct
//Gigabyte motherboard public
class Gigamainboard implements mainboardapi{
    //Insert CPU Interface PIN number
    Integer pins;
    Public Gigamainboard (Integer pins) {
        this.pins=pins;
    }

    @Override public
    Void Install () {
        System.out.println ("gigabyte motherboard is being installed ... pin number:" +pins);
    }


Concreteproduct

Package design mode. abstractfactory;
 * * September 2, 2016 morning 10:24:25
 * @Author pin-wang
 *@e-mail 1228935432@qq.com
*


//MSI motherboard
public Class Msimainboard implements mainboardapi{
    //Insert CPU Interface PIN number
    Integer pins;
    Public Msimainboard (Integer pins) {
        this.pins=pins;
    }

    @Override public
    Void Install () {
        System.out.println ("MSI Motherboard is being installed ... pin number:" +pins);
    }


Abstractfactory

Package design mode. abstractfactory;
 * * * * September 2, 2016 morning 10:29:07
 * @Author pin-wang *@e-mail 1228935432@qq.com * * Public
interface abstractfactory {
    Cpuapi createcpu ();
    Mainboardapi Createmainboardapi ();
}

Concretefactory

Package design mode. abstractfactory;
 * * * * September 2, 2016 morning 10:31:01
 * @Author pin-wang *@e-mail 1228935432@qq.com * * Public
class Concretefactorya implements abstractfactory{

    @Override
    //intell CPU public
    Cpuapi createcpu () {
        return new INTELLCPU (1156);
    }

    Gigabyte Motherboard
    @Override public
    mainboardapi Createmainboardapi () {return
        new Gigamainboard (1156);
    }



Concretefactory

Package design mode. abstractfactory;
 * * * * September 2, 2016 morning 10:31:01
 * @Author pin-wang *@e-mail 1228935432@qq.com * * Public
class Concretefactoryb implements abstractfactory{

    @Override
    //amd CPU public
    Cpuapi createcpu () {
        return new AMDCPU (939);
    }

    MSI motherboard
    @Override public
    mainboardapi Createmainboardapi () {return
        new Msimainboard (939);
    }



Client

 package design mode. abstractfactory; * * September 2, 2016 morning 10:39:15 * @Author Pin-wang *@e-mail 1228935432@qq.com * * public class Engineer {Abstractfactory SC

    Hemal;
    Public Engineer (Abstractfactory schemal) {this.schemal = Schemal;
    public void Setschemal (Abstractfactory schemal) {this.schemal = Schemal;
        public void Makeandusecomputer () {Cpuapi cpu=schemal.createcpu ();
        Mainboardapi Mainboard=schemal.createmainboardapi ();
        Cpu.calculate ();
    Mainboard.install (); }

}
Package design mode. abstractfactory;

Import static org.junit.assert.*;

Import java.nio.channels.NonWritableChannelException;

Import Javax.swing.border.AbstractBorder;

Import Org.junit.Test;

*
 * September 2, 2016 morning 10:35:51
 * @Author pin-wang
 *@e-mail 1228935432@qq.com * * Public
class Client {

    @Test public
    void Test () {
        System.out.println ("installed scheme a");
        Installed mode 1
        abstractfactory schemala=new Concretefactorya ();
        Engineer engineer=new Engineer (Schemala);
        Engineer.makeandusecomputer ();

        SYSTEM.OUT.PRINTLN ("installed scheme B");
        Installed mode 2
        abstractfactory schemalb=new concretefactoryb ();
        Engineer.setschemal (SCHEMALB);
        Engineer.makeandusecomputer ();
    }


Output

Installed solution A
Intell CPU is calculating ... pins:1156
gigabyte motherboard is being installed ... pin number: 1156
installed solution B
AMD CPU is calculating ... pins:939
MSI Motherboard is being installed ... pin number: 939

This article is derived from
Code Big Wet
Code Big Wet

Welcome to the vast coder communication technology
qq:1228935432
wx:wx1228935432

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.