十月二十九日

yblll發表於2024-10-30

[實驗任務一]:人與膚色

使用抽象工廠模式,完成下述產品等級結構:

複製程式碼
package test4;

public class BlackFactory implements MWFactory
{
    public Man produceMan()
    {
        return new BlackSkinMan();
    }

    public Woman produceWomen()
    {
        return new BlackSkinWomen();
    }
}
複製程式碼
複製程式碼
package test4;

public class BlackSkinMan implements Man
{
    public void play()
    {
        System.out.println("我是黑種男人!");
    }
}
複製程式碼
複製程式碼
package test4;

public class BlackSkinWomen implements Woman
{
    public void play()
    {
        System.out.println("我是黑種女人!");
    }
}
複製程式碼
複製程式碼
package test4;

public class Client
{
    public static void main(String args[])
    {
        try
        {
            MWFactory factory;
            Man m;
            Woman w;
            factory=(MWFactory)XMLUtil.getBean();
            m=factory.produceMan();
            m.play();
            w=factory.produceWomen();
            w.play();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
//run此檔案
複製程式碼
複製程式碼
package test4;

public interface Man
{
    public void play();
}
複製程式碼
複製程式碼
package test4;

public interface MWFactory
{
    public Man produceMan();
    public Woman produceWomen();
}
複製程式碼
複製程式碼
package test4;

public class WhiteFactory implements MWFactory
{
    public Man produceMan()
    {
        return new WhiteSkinMan();
    }

    public Woman produceWomen()
    {
        return new WhiteSkinWomen();
    }
}
複製程式碼
複製程式碼
package test4;

public class WhiteSkinMan implements Man{
    public void play()
    {
        System.out.println("我是白種男人!");
    }
}
複製程式碼
複製程式碼
package test4;

public class WhiteSkinWomen implements Woman{
    public void play()
    {
        System.out.println("我是白種女人!");
    }
}
複製程式碼
複製程式碼
package test4;

public interface Woman
{
    public void play();
}
複製程式碼
複製程式碼
package test4;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
public class XMLUtil
{
    //該方法用於從XML配置檔案中提取具體類類名,並返回一個例項物件
    public static Object getBean()
    {
        try
        {
            //建立文件物件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc;
            doc = builder.parse(new File("Cainiao.iml"));

            //獲取包含類名的文字節點
            NodeList nl = doc.getElementsByTagName("className");
            Node classNode=nl.item(0).getFirstChild();
            String cName=classNode.getNodeValue();

            //透過類名生成例項物件並將其返回
            Class c=Class.forName(cName);
            Object obj=c.newInstance();
            return obj;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
複製程式碼
複製程式碼
package test4;

public class YellowFactory {

        public Man produceMan()
        {
            return new YellowSkinMan();
        }

        public Woman produceWomen()
        {
            return new YellowSkinWomen();
        }
    }
複製程式碼
複製程式碼
package test4;

public class YellowSkinMan implements Man{
    public void play()
    {
        System.out.println("我是黃種男人!");
    }
}
複製程式碼
複製程式碼
package test4;

public class YellowSkinWomen implements Woman{
    public void play()
    {
        System.out.println("我是黃種女人!");
    }
}

相關文章