设计模式之工厂模式

2022-01-10 16:34:43 浏览数 (1)

Factory

代码语言:javascript复制
抽象工厂设计模式简单理解;1个公共行为有不同的实现,这些实现的创建就由对应的工厂来完成操作
比如说有两种行为 INote 和 IBook 两种行为有java和go两种实现 同样有javaFactory 和 gofactory
package com.sync.juc.factory.inter;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:35
 */
public interface INote {
    void write();
}

package com.sync.juc.factory.inter;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:36
 */
public interface IBook {
    void read();
}

package com.sync.juc.factory.java;

import com.sync.juc.factory.inter.IBook;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:36
 */
public class JavaBook implements IBook {
    @Override
    public void read() {
        System.out.println("阅读java书籍");
    }
}

package com.sync.juc.factory.golang;

import com.sync.juc.factory.inter.IBook;
import com.sync.juc.factory.inter.INote;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:36
 */
public class GOBook implements IBook {
    @Override
    public void read() {
        System.out.println("阅读Go书籍");
    }
}
package com.sync.juc.factory.impl;

import com.sync.juc.factory.IFactory;
import com.sync.juc.factory.inter.IBook;
import com.sync.juc.factory.inter.INote;
import com.sync.juc.factory.java.JavaBook;
import com.sync.juc.factory.java.JavaNote;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:42
 */
public class JavaFactory implements IFactory {
    @Override
    public IBook createBook() {
        return new JavaBook();
    }

    @Override
    public INote createNote() {
        return new JavaNote();
    }
}
package com.sync.juc.factory.impl;

import com.sync.juc.factory.IFactory;
import com.sync.juc.factory.golang.GOBook;
import com.sync.juc.factory.golang.GoNote;
import com.sync.juc.factory.inter.IBook;
import com.sync.juc.factory.inter.INote;
import com.sync.juc.factory.java.JavaBook;
import com.sync.juc.factory.java.JavaNote;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:42
 */
public class GoFactory implements IFactory {
    @Override
    public IBook createBook() {
        return new GOBook();
    }

    @Override
    public INote createNote() {
        return new GoNote();
    }
}
package com.sync.juc.factory;

import com.sync.juc.factory.inter.IBook;
import com.sync.juc.factory.inter.INote;

/**
 * @author http://zouyujie.top
 * @date 2022/1/4 19:41
 */
public interface IFactory {

    IBook createBook();

    INote createNote();
}

本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名,转载请标明出处 最后编辑时间为: 2022/01/04 13:05

0 人点赞