設計モード---抽象工場モード

11663 ワード

参照:http://blog.csdn.net/ipqxiang/article/details/1955677
       程傑《大話設計モード》清華大学出版社
一、抽象工場モデル
          特定のクラスを指定することなく、関連オブジェクトまたは相互依存オブジェクトを作成するインタフェースを提供します.
2、4つの重要な役割(クラス)
   抽象工場、具体工場、抽象製品、具体製品
三、プログラム例


抽象ファクトリインタフェース
//      



package com;



//      :  2 product: IUser  IDepartment

public interface IFactory 

{

    IUser createUser();

    IDepartment createDepartment();



}

 


具体的な工場
//    : StudentFactory

class StudentFactory implements IFactory

{

    public IUser createUser()

    {

        return new StudentUser();

    }

    

    public IDepartment createDepartment()

    {

        return new StudentDepartment();

    }

}



//    :  TeacherFactory

class TeacherFactory implements IFactory

{

    public IUser createUser()

    {

        return new TeacherUser();

    }

    

    public IDepartment createDepartment()

    {

        return new TeacherDepartment();

    }

}

 


抽象製品1
//            



//  user  :         Name,  Name

public interface IUser

{

    void setName(String name);



    String getName();

}

 


具体的な製品
//   user  : student,teacher

class StudentUser implements IUser 

{

    private String name;



    public void setName(String name) 

    {

        this.name = "Student: " + name;

    }



    public String getName() 

    {

        return name;

    }

}



class TeacherUser implements IUser 

{

    private String name;



    public void setName(String name) 

    {

        this.name = "Teacher: " + name;

    }



    public String getName() 

    {

        return name;

    }

}

 


抽象製品2
//            



//  Department  :         Department,  Department

public interface IDepartment 

{

    void setDepartment(String department);

    

    String getDepartment();

}

 


具体的な製品2
//  Department  : student,teacher

class StudentDepartment implements IDepartment

{

    private String department = null;

    

    public void setDepartment(String department)

    {

        this.department = " Student: "+department;

    }

    

    public String getDepartment()

    {

        return department;

    }

}



class TeacherDepartment implements IDepartment

{

  private String department = null;

    

    public void setDepartment(String department)

    {

        this.department = " Teacher: "+department;

    }

    

    public String getDepartment()

    {

        return department;

    }

}

 


テストプログラム(クライアント)
//       



package com;



public class ClientMain 

{

    public static void main(String[] args)

    {

        String name = "gqy";

        String department ="CSU";

        

        //IFactory student = new StudentFactory();

        IFactory student = new TeacherFactory();

        

        IUser user = student.createUser();

        user.setName(name);

        System.out.println(user.getName());

        

        IDepartment id = student.createDepartment();

        id.setDepartment(department);

        System.out.println(id.getDepartment());        



    }



}