JAVA實現選擇排序
阿新 • • 發佈:2022-03-13
如果沒有工廠模式我們寫的程式碼
場景
現在有一個課程,裡面有Python、Java、Go語言等課程,並且每個課程都有以下業務
- 觀看
- 錄播
package start.service;
public interface CourseService {
// 觀看
void view();
// 錄屏
void record();
}
package start.service.impl; import start.service.CourseService; public class JavaCourseServiceImpl implements CourseService { @Override public void view() { System.out.println("觀看Java"); } @Override public void record() { System.out.println("錄屏Java"); } }
package start.service.impl; import start.service.CourseService; public class PythonCourseServiceImpl implements CourseService { @Override public void view() { System.out.println("觀看Python"); } @Override public void record() { System.out.println("錄屏Python"); } }
package start; import start.service.CourseService; import start.service.impl.JavaCourseServiceImpl; import start.service.impl.PythonCourseServiceImpl; public class Test { public static void main(String[] args) { // Java、Python、Go // 他們都有這些業務:可以觀看、可以錄屏 CourseService courseService = new JavaCourseServiceImpl(); courseService.view(); courseService.record(); CourseService courseService2 = new PythonCourseServiceImpl(); courseService2.view(); courseService2.record(); } }
如果後面課程業務加了很多個課程,比如Go、C#,他們都屬於課程,但是每次都要new一次物件,我們可不可以用什麼方式給管理起來呢?這個時候工廠模式出現了!
簡單工廠
package simple.service;
public interface CourseService {
// 觀看
void view();
// 錄屏
void record();
}
package simple.service.impl;
import simple.service.CourseService;
public class PythonCourseServiceImpl implements CourseService {
@Override
public void view() {
System.out.println("觀看Python");
}
@Override
public void record() {
System.out.println("錄屏Python");
}
}
package simple.service.impl;
import simple.service.CourseService;
public class JavaCourseServiceImpl implements CourseService
{
@Override
public void view() {
System.out.println("觀看Java");
}
@Override
public void record() {
System.out.println("錄屏Java");
}
}
package simple.factory;
import simple.service.CourseService;
import simple.service.impl.GoCourseServiceImpl;
import simple.service.impl.JavaCourseServiceImpl;
import simple.service.impl.PythonCourseServiceImpl;
public class CourseServiceFactory {
public static CourseService get(String name){
if("Java".equals(name)){
return new JavaCourseServiceImpl();
}else if("Python".equals(name)){
return new PythonCourseServiceImpl();
}else if("Go".equals(name)){
return new GoCourseServiceImpl();
}
return null;
}
}
package simple.factory;
import simple.service.CourseService;
public class Test {
public static void main(String[] args) {
CourseService courseService = CourseServiceFactory.get("Go");
courseService.record();
courseService.view();
}
}
雖然用簡單工廠管理起來了,但是也有缺點,缺點就是每次加課程,都需要在CourseServiceFactory裡新增大量的邏輯程式碼,所以工廠方法模式出現了!
工廠方法
package function.service;
public interface CourseService {
// 觀看
void view();
// 錄屏
void record();
// 編寫筆記
void edit();
// 下載筆記
void download();
}
package function.service.impl;
import function.service.CourseService;
public class PythonCourseServiceImpl implements CourseService {
@Override
public void view() {
System.out.println("觀看Python");
}
@Override
public void record() {
System.out.println("錄屏Python");
}
@Override
public void edit() {
}
@Override
public void download() {
}
}
package function.service.impl;
import function.service.CourseService;
public class JavaCourseServiceImpl implements CourseService
{
@Override
public void view() {
System.out.println("觀看Java");
}
@Override
public void record() {
System.out.println("錄屏Java");
}
@Override
public void edit() {
}
@Override
public void download() {
}
}
package function.service.impl;
import function.service.CourseService;
public class GoCourseServiceImpl implements CourseService
{
@Override
public void view() {
System.out.println("觀看Go");
}
@Override
public void record() {
System.out.println("錄屏Go");
}
@Override
public void edit() {
}
@Override
public void download() {
}
}
package function.factory;
import function.service.CourseService;
public interface CourseServiceFactory {
CourseService create();
}
package function.factory;
import function.service.CourseService;
import function.service.impl.GoCourseServiceImpl;
public class GoCourseServiceFactory implements CourseServiceFactory{
@Override
public CourseService create() {
return new GoCourseServiceImpl();
}
}
package function.factory;
import function.service.CourseService;
import function.service.impl.JavaCourseServiceImpl;
public class JavaCourseServiceFactory implements CourseServiceFactory{
@Override
public CourseService create() {
return new JavaCourseServiceImpl();
}
}
package function.factory;
import function.service.CourseService;
import function.service.impl.PythonCourseServiceImpl;
public class PythonCourseServiceFactory implements CourseServiceFactory{
@Override
public CourseService create() {
return new PythonCourseServiceImpl();
}
}
package function.factory;
import function.service.CourseService;
public class Test {
public static void main(String[] args) {
CourseServiceFactory courseServiceFactory = new JavaCourseServiceFactory();
CourseService courseService = courseServiceFactory.create();
courseService.record();
courseService.view();
// Java、Python、Go
// 他們都有這些業務:可以觀看、可以錄屏、編寫筆記、下載筆記
}
}
雖然每個課程都用不同的工廠去進行建立,但是他也有缺點,就是每個課程有可能有不同的業務,比如新增了編寫筆記、下載筆記等等,我們都需要去進行每個課程的方法實現。並且呢,如果課程多,我們也需要建立大量的Java類,建立好多個課程共產類,越來越負責,所以這個時候抽象工廠出現了!
抽象工廠
我們給觀看、錄屏分組,分為視訊組。編寫筆記和下載筆記分為筆記組
package abstracts.service;
public interface VideoCourseService {
// 觀看
void view();
// 錄屏
void record();
}
package abstracts.service;
public interface NoteCourseService {
// 編寫筆記
void edit();
// 下載筆記
void download();
}
package abstracts.service.Impl;
import abstracts.service.VideoCourseService;
public class PythonVideoCourseServiceImpl implements VideoCourseService
{
@Override
public void view() {
System.out.println("觀看Python");
}
@Override
public void record() {
System.out.println("錄製Python");
}
}
package abstracts.service.Impl;
import abstracts.service.NoteCourseService;
public class PythonNoteCourseServiceImpl implements NoteCourseService
{
@Override
public void edit() {
System.out.println("編寫Python筆記");
}
@Override
public void download() {
System.out.println("下載Python筆記");
}
}
package abstracts.service.Impl;
import abstracts.service.VideoCourseService;
public class JavaVideoCourseServiceImpl implements VideoCourseService
{
@Override
public void view() {
System.out.println("觀看Java");
}
@Override
public void record() {
System.out.println("錄製Java");
}
}
package abstracts.service.Impl;
import abstracts.service.NoteCourseService;
public class JavaNoteCourseServiceImpl implements NoteCourseService
{
@Override
public void edit() {
System.out.println("編寫Java筆記");
}
@Override
public void download() {
System.out.println("下載Java筆記");
}
}
package abstracts.factory;
import abstracts.service.NoteCourseService;
import abstracts.service.VideoCourseService;
public abstract class CourseFactory {
protected abstract NoteCourseService createNoteService();
protected abstract VideoCourseService createVideoService();
}
package abstracts.factory;
import abstracts.service.Impl.JavaNoteCourseServiceImpl;
import abstracts.service.Impl.JavaVideoCourseServiceImpl;
import abstracts.service.NoteCourseService;
import abstracts.service.VideoCourseService;
public class JavaCourseFactory extends CourseFactory{
@Override
protected NoteCourseService createNoteService() {
return new JavaNoteCourseServiceImpl();
}
@Override
protected VideoCourseService createVideoService() {
return new JavaVideoCourseServiceImpl();
}
}
package abstracts.factory;
import abstracts.service.Impl.PythonNoteCourseServiceImpl;
import abstracts.service.Impl.PythonVideoCourseServiceImpl;
import abstracts.service.NoteCourseService;
import abstracts.service.VideoCourseService;
public class PythonCourseFactory extends CourseFactory{
@Override
protected NoteCourseService createNoteService() {
return new PythonNoteCourseServiceImpl();
}
@Override
protected VideoCourseService createVideoService() {
return new PythonVideoCourseServiceImpl();
}
}
package abstracts.factory;
import abstracts.factory.JavaCourseFactory;
public class Test {
public static void main(String[] args) {
// Java、Python、Go
// 他們都有這些業務:可以觀看、可以錄屏、編寫筆記、下載筆記
// 視訊:可以觀看、可以錄屏
// 筆記:編寫筆記、下載筆記
JavaCourseFactory javaCourseFactory = new JavaCourseFactory();
javaCourseFactory.createNoteService().edit();
javaCourseFactory.createVideoService().record();
}
}