码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式笔记--工厂

时间:2018-03-15 13:25:49      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:logs   default   test   nts   stat   笔记   需要   简单   pac   

简单工厂

简单工厂是一个工厂可以生产各种产品,用户只需要得到这个工厂就可获得需要的产品

优点:对用户友好

缺点:工厂分工不明确导致逻辑复杂

└── simple
    ├── SimpleFactory.java
    └── SimpleFactoryTest.java
package com.xh.pattern.factory.simple;

/**
 * Created by root on 3/15/18.
 */
public class SimpleFactory {

    public void produceComputer(String computerBrands) {
        switch (computerBrands) {
            case "apple":
                System.out.println("MAC COMPUTER");
                break;
            case "thinkpad":
                System.out.println("thinkpad COMPUTER");
                break;
            case "asus":
                System.out.println("asus COMPUTER");
                break;
            default:
                System.out.println("生产不了");
        }
    }
}
package com.xh.pattern.factory.simple;

/**
 * Created by root on 3/15/18.
 */
public class SimpleFactoryTest {
    public static void main(String[] args) {
        SimpleFactory simpleFactory = new SimpleFactory();
        simpleFactory.produceComputer("asus");
    }
}

工厂方法

工厂方法是为了弥补简单工厂的分工不明改进的

优点:工厂分工明确,逻辑简单

缺点:用户需要找到对应的工厂才能获得产品,对用户不友好

├── function
│   ├── AppleFactory.java
│   ├── AsusFactory.java
│   ├── Factory.java
│   ├── FuncFactoryTest.java
│   └── ThinkPadFactory.java
package com.xh.pattern.factory.function;

/**
 * Created by root on 3/15/18.
 */
public interface Factory {
    void produceComputer();
}
package com.xh.pattern.factory.function;

/**
 * Created by root on 3/15/18.
 */
public class AppleFactory implements Factory {
    @Override
    public void produceComputer() {
        System.out.println("MAC COMPUTER");
    }
}
package com.xh.pattern.factory.function;

/**
 * Created by root on 3/15/18.
 */
public class AsusFactory implements Factory {
    @Override
    public void produceComputer() {
        System.out.println("asus COMPUTER");
    }
}
package com.xh.pattern.factory.function;

/**
 * Created by root on 3/15/18.
 */
public class ThinkPadFactory implements Factory {
    @Override
    public void produceComputer() {
        System.out.println("thinkpad COMPUTER");
    }
}
package com.xh.pattern.factory.function;

/**
 * Created by root on 3/15/18.
 */
public class FuncFactoryTest {
    public static void main(String[] args) {
        Factory factory=new AppleFactory();
        factory.produceComputer();
    }
}

抽象工厂

抽象工厂是为了弥补工厂方法的对用户不友好改进的

优点:工厂分工明确,逻辑简单,对用户友好

缺点:???

├── abstr
│   ├── AppleFactory.java
│   ├── AsusFactory.java
│   ├── DefaultFactory.java
│   ├── Factory.java
│   ├── FuncFactoryTest.java
│   └── ThinkPadFactory.java
package com.xh.pattern.factory.abstr;

/**
 * Created by root on 3/15/18.
 */
public abstract class Factory {
    abstract void produceComputer();

    public void produceComputer(String computerBrands) {
        switch (computerBrands) {
            case "apple":
                new AppleFactory().produceComputer();
                break;
            case "thinkpad":
                new ThinkPadFactory().produceComputer();
                break;
            case "asus":
                new AsusFactory().produceComputer();
                break;
            default:
                System.out.println("生产不了");
        }
    }
}
package com.xh.pattern.factory.abstr;

/**
 * Created by root on 3/15/18.
 */
public class AppleFactory extends Factory {
    @Override
    public void produceComputer() {
        System.out.println("MAC COMPUTER");
    }
}

...

package com.xh.pattern.factory.abstr;

/**
 * Created by root on 3/15/18.
 */
public class DefaultFactory extends Factory {
    @Override
    void produceComputer() {
        new AppleFactory().produceComputer();
    }
}
package com.xh.pattern.factory.abstr;

/**
 * Created by root on 3/15/18.
 */
public class FuncFactoryTest {
    public static void main(String[] args) {
        DefaultFactory factory = new DefaultFactory();
        factory.produceComputer();
        factory.produceComputer("asus");
    }
}

设计模式笔记--工厂

标签:logs   default   test   nts   stat   笔记   需要   简单   pac   

原文地址:https://www.cnblogs.com/lanqie/p/8572684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!