美文网首页
设计模式 (二) 工厂模式

设计模式 (二) 工厂模式

作者: Lost_Robot | 来源:发表于2017-07-11 11:46 被阅读18次

    工厂模式:属于创建型模式,它提供了一种创建对象的最佳方式。
    在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

    主要解决接口选择的问题

    一个工厂可以生产带有各种颜色的各种形状:
    首先由颜色接口和形状接口:

    //图像接口
    public interface Shape {
     enum Type {
            Circle, Rectangle, Square
        }
        String Name();
        void Draw();
    }
    
    //颜色接口
    public interface Colour {
        void Upper();
    }
    
    

    下面有实体类有圆形,三角形,正方形

    //圆形
    public class Circle implements Shape {
    
    
        @Override
        public String Name() {
            return "Circle";
        }
    
        @Override
        public void Draw() {
    
            Log.e("Shape", "--------------" + Name() + "------------");
        }
    }
    
    //正方形
    public class Square implements Shape {
    
        @Override
        public void Draw() {
    
            Log.e("Shape", "--------------" + Name() + "------------");
        }
    
        @Override
        public String Name() {
            return "Square";
        }
    }
    
    //三角形
    public class Rectangle implements Shape {
    
        @Override
        public String Name() {
            return "Rectangle";
        }
    
        @Override
        public void Draw() {
    
            Log.e("Shape", "--------------" + Name() + "------------");
        }
    }
    
    

    颜色实体类:

    //灰色
    public class Gray implements Colour {
    
    
        @Override
        public void Upper() {
            Log.e("Colour","--------- Colour is Gray ---------");
        }
    }
    
    //红色
    public class Red implements Colour {
    
    
        @Override
        public void Upper() {
            Log.e("Colour","--------- Colour is Red---------");
        }
    }
    
    //绿色
    public class Green implements Colour {
    
    
        @Override
        public void Upper() {
            Log.e("Colour","--------- Colour is Green---------");
        }
    }
    

    下面是我们的工厂,我们的形状工厂(标准工厂模式的实现):

    public class ShapeFactory {
    
       public Shape getShape(Shape.Type type) {
    
            if (type == Shape.Type.Circle) {
    
                return new Circle();
            } else if (type == Shape.Type.Rectangle) {
                return new Rectangle();
            } else {
                return new Square();
            }
    
        }
    }
    

    我们还可以这样用(偏抽象工厂的实现)

    
    
    public class Factory {
    
        private Shape shape = null;
        private Colour colour = null;
    
        public Factory(Shape shape, Colour colour) {
            this.shape = shape;
            this.colour = colour;
        }
    
        public Shape getShape() {
            return shape;
        }
    
    
        public Colour getColour() {
            return colour;
        }
    
        public void showThis() {
            getShape().Draw();
            getColour().Upper();
        }
    }
    

    如何使用:

            Factory shapeFactory = new Factory();
            Shape  sq=shapeFactory.getShape(Shape.Type.Square);
            Shape  cc=shapeFactory.getShape(Shape.Type.Circle);
            Shape  rt=shapeFactory.getShape(Shape.Type.Rectangle);
    

    或者

     private Factory ff = new Factory(new Rectangle(),new Green());
     ff.showThis();
    
    

    抽象工厂模式Kotlin 实现:
    接口

    interface ktShape {
        fun getName(): String
        fun Draw()
    
    }
    
    enum class ShapeType {
        Circle, Rectangle, Square
    
    }
    
    enum class ColorType {
        Bule, Red, Wite
    
    }
    
    interface ktColor {
        fun getName(): String
        fun Upper()
    }
    
    interface ShapeFactory {
        fun MakeShape(type: ShapeType): ktShape
    }
    
    interface ColorFactory {
    
        fun MakeColor(type: ColorType): ktColor
    }
    

    颜色类,实现:

    class RedC : ktColor {
        override fun getName(): String {
            return "Red Color"
        }
    
        override fun Upper() {
            System.out.print(getName())
        }
    
    }
    
    class BuleC : ktColor {
        override fun getName(): String {
            return "Bule Color"
        }
    
        override fun Upper() {
            System.out.print(getName())
        }
    
    }
    
    class WiteC : ktColor {
        override fun getName(): String {
            return "Wite Color"
        }
    
        override fun Upper() {
            System.out.print(getName())
        }
    
    }
    

    形状类

    
    class Squarekt : ktShape {
        override fun getName(): String {
            return "Square  Shape"
        }
    
        override fun Draw() {
            System.out.print(getName())
        }
    
    }
    
    class Circlekt : ktShape {
        override fun getName(): String {
            return "Circle  Shape"
        }
    
        override fun Draw() {
            System.out.print(getName())
        }
    
    }
    
    class Rectanglekt : ktShape {
        override fun getName(): String {
            return "Rectangle  Shape"
        }
    
        override fun Draw() {
            System.out.print(getName())
        }
    
    }
    

    工厂类:

    class AbsFactory : ShapeFactory, ColorFactory {
    
        var ktsp: ktShape? = null
        var ktco: ktColor? = null
    
        constructor ()
        override fun MakeShape(type: ShapeType): ktShape {
    
            when (type) {
                ShapeType.Circle -> ktsp = Circlekt()
                ShapeType.Rectangle -> ktsp = Rectanglekt()
                ShapeType.Square -> ktsp = Squarekt()
    
            }
            return ktsp as ktShape
    
        }
    
        override fun MakeColor(type: ColorType): ktColor {
            when (type) {
                ColorType.Red -> ktco = RedC()
                ColorType.Bule -> ktco = BuleC()
                ColorType.Wite -> ktco = WiteC()
            }
    
            return ktco as ktColor
    
        }
    }
    

    如何使用:

    class main {
        var absF: AbsFactory = AbsFactory()
    
        fun Test() {
            absF.MakeShape(ShapeType.Square)
            absF.MakeColor(ColorType.Red)
        }
        
    }
    

    相关文章

      网友评论

          本文标题:设计模式 (二) 工厂模式

          本文链接:https://www.haomeiwen.com/subject/cbxtqxtx.html