Factory pattern, abstract factory pattern

Purpose and task

Objective: familiar with the UML, familiar with the factory method pattern, abstract factory pattern.

Tasks: in accordance with the requirements of the experiment content, done using UML and java realize experimental content.

Content preview

Review the contents of the course, familiar with the factory method pattern, abstract factory pattern.

Experiment contents and requirements

  • Using the factory pattern:

Existing computer products, windows and Apple, try the factory model of the scene description, claims and drawing the class Coding FIG.

  • Abstract Factory pattern:

Conventional windows-based computer products and accessories manufacturers produce different types of apple CPU and RAM, try abstract factory model of the scene description, claims and drawing the class Coding FIG.

The results (available Continued)

First, use the factory pattern:

Existing computer products, windows and Apple, try the factory model of the scene description, claims and drawing the class Coding FIG.

Apple.java:

class apple implements computer{

    public String getTypeName() {

        // TODO Auto-generated method stub

        . The System OUT .print ( " instantiated Apple" );

        return "apple";

    }

}

Windows.java:

class windows implements computer {

    public String getTypeName() {

        // TODO Auto-generated method stub

        System.out.print("实例化了windows");

        return "windows";

    }

}

Computer.java:
public interface computer {

    String getTypeName();

}

computerFactory.java:

public interface computerFactory {

        //静态工厂方法

        abstract computer createComputer();

}

windowsFactory.java:

class windowsFactory implements computerFactory{

    public computer createComputer() {

        // TODO Auto-generated method stub

        return new windows();

    }

}

appleFactory.java:

class appleFactory implements computerFactory{

    public computer createComputer() {

        // TODO Auto-generated method stub

        return new apple();

    }

}

 

二,使用抽象工厂模式:

现有电脑产品配件厂商基于windows和apple生产不同类型的CPU和RAM,请试用抽象工厂模式描述该场景,要求绘制类图并编码实现。

 

Apple.java:

package abstractFactory;

class apple implements computer{

    public String getTypeName() {return "apple";}

}

appleFactory.java:

package abstractFactory;

class appleFactory implements computerFactory{

    public computer createComputer() {return new apple();}

    public CPU createCPU() {return null;}

    public RAM createRAM() {return null;}

}
computer.java:

package abstractFactory;

public interface computer {String getTypeName();}

computerFactory.java:

package abstractFactory;

public interface computerFactory {

abstract computer createComputer();

        CPU createCPU();

        RAM createRAM();

}

CPU.java:

package abstractFactory;

public abstract class CPU {}

class WindowsCPU extends CPU{}

class AppleCPU extends CPU{}

 

RAM.java:

package abstractFactory;

public abstract class RAM {}

class WindowsRAM extends RAM{}

class AppleRAM extends RAM{}

windows.java:

package abstractFactory;

class windows implements computer {

    public String getTypeName() {return "windows";}

}

windowsFactory.java:

package abstractFactory;

class windowsFactory implements computerFactory{

    public computer createComputer() {return new windows();}

    public CPU createCPU() {return null;}

    public RAM createRAM() {return null;}

}

思考题:

  1. Java语言中的toString()方法和clone()方法会返回新的java对象,请问他们是否是工厂模式?

不是,他们定义了一个用于创建对象的接口,但是不是让子类去实例化

 

2、工厂方法模式中:一般一个工厂方法返还了一个新的对象,请问是否必须是新的对象?

不是,比如单例的情况每次都返回同一个对象

 

 

  1. 请参考课件,分别绘制出简单工厂模式、工厂模式、抽象工厂模式的相图,并给出三种模式的不同点?

简单工厂模式:

工厂模式:

 

抽象工厂模式:

 

简单工厂模式:工厂类负责创建的对象比较少,客户只知道传入了工厂类的参数,对于始何创建对象不关心。                     

工厂方法模式:当一个类不知道它所必须创建对象的类或一个类希望由子类来指定它所创建的对象时,当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候,可以使用工厂法。                     

抽象工厂模式:一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的。这个系统有多于一个的产品族,而系统只消费其中某一产品族。同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。               

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/yszbrzdd/article/details/93520678